vendredi 27 novembre 2015

Strange Errors. Using rand() and arrays within a loop

I really can not explain what is going on with my program. I really can't spot what may be causing the problems. It occurs within this function

void test()
{
    srand(time(NULL));

    questionNo = 1;

    for (testLoop = 0; testLoop < 10; ++testLoop)
    {
        const char opArray[3] = { 'x', '+', '-' };
        int pickValue = rand() % 3;
        char Operator = opArray[pickValue];

        int n1 = rand() % 20 + 1;
        int n2 = rand() % 20 + 1;

        if (Operator = '+')
            answer = n1 + n2;
        else if (Operator = '-')
            answer = n1 - n2;
        else
            answer = n1 * n2;

        cout << "Question " << questionNo << "\n";
        ++questionNo;
        cout << n1 + " " << Operator + " " << n2 << "?";
        userAnswer = cin.get();

        if (userAnswer = answer)
        {
            cout << "\n\n Correct!\n";
            system("cls");
            ++score;
        }
        else
        {
        cout << "\n\n Incorrect!\n";
        system("cls");
        }
        Sleep(500);
    }
    cout << "You scored " << score << " out of 10!";
    fileWrite(score);
}

An example of what occurs is the following. This outputs to console

Question 1
@Øÿ×╝׳×0ƒ11?

What even is that???
It also skips two questions every time. For example it goes

"Question 1"
"Question 3"
"Question 5"




Aucun commentaire:

Enregistrer un commentaire