LOOP HELP

I am trying to figure out how to use a while loop so that my program continues to ask the user different addition problems. When done the user should type -1 when finished. Can anyone help code this for me? Here is my code so far.

#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <random>

using namespace std;



int main()

{
int userGuess, answer, num1, num2, totalCorrect;
cout << "Welcome to the addition game!" << endl;
cout << "Calculate the problems below, when finished type -1 to view results.\n\n";


unsigned seed = time(0);
srand(seed);
num1 = 1 + rand() % 10; // first random number for the adding problem
num2 = 1 + rand() % 10; // second random number for the adding problem
const char* possibleanswers[4] = { // produces random feedback when getting it correct
"Good Job!", "Well Done!", "Excellent!", "Correct!", };
const char* randWrong[4] = { "WRONG!", "TERRIBLE!", "INCORRECT!", "IDIOT", }; // produces random feedback when getting it incorrect




cout << " What is " << num1 << "+" << num2 << "?";
cin >> userGuess;


if (userGuess == num1 + num2)
cout << possibleanswers[rand() % 4] << endl;

else
cout << randWrong[rand() % 3] << endl;



return 0;
}

Last edited on
Hi,

here's an pseudo-code to get you started

1
2
3
4
5
6
7
8
//initialise a var i to 0

while (i!=-1)
{
// code logic

//get input of i from user
}  


LINKS :
http://www.cplusplus.com/doc/tutorial/control/
https://www.cprogramming.com/tutorial/lesson3.html

PS. Looks like your random no. generator code will generate same random number again and again

PPS. Please use code tags
http://www.cplusplus.com/articles/jEywvCM9/

Heres what I got so far, I just saw your reply. I still need to make it to where -1 stops it. But now on my code the program doesnt show random numbers in the while loop. It shows the same adding problems it suppose to be different.




#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <random>

using namespace std;



int main()

{
int userGuess, answer, num1, num2, totalCorrect;
char i = 0;
cout << "Welcome to the addition game!" << endl;
cout << "Calculate the problems below, when finished type -1 to view results.\n\n";


unsigned seed = time(0);
srand(seed);
num1 = 1 + rand() % 10; // first random number for the adding problem
num2 = 1 + rand() % 10; // second random number for the adding problem
const char* possibleanswers[4] = { // produces random feedback when getting it correct
"Good Job!", "Well Done!", "Excellent!", "Correct!", };
const char* randWrong[4] = { "WRONG!", "TERRIBLE!", "INCORRECT!", "IDIOT", }; // produces random feedback when getting it incorrect



while (userGuess = num1 + num2)



{
cout << " What is " << num1 << "+" << num2 << "?";
cin >> userGuess;




if (userGuess == num1 + num2)
cout << possibleanswers[rand() % 4] << endl;

else
cout << randWrong[rand() % 3] << endl;


}





return 0;
}
It shows the same adding problems it suppose to be different.

The thing is you have to generate the random number every time in the loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <random>

using namespace std;



int main()

{ 
int userGuess, answer, num1, num2, totalCorrect;
char i = 0;
cout << "Welcome to the addition game!" << endl;
cout << "Calculate the problems below, when finished type -1 to view results.\n\n";


unsigned seed = time(0);
srand(seed);
num1 = 1 + rand() % 10; // first random number for the adding problem
num2 = 1 + rand() % 10; // second random number for the adding problem
const char* possibleanswers[4] = { // produces random feedback when getting it correct
"Good Job!", "Well Done!", "Excellent!", "Correct!", };
const char* randWrong[4] = { "WRONG!", "TERRIBLE!", "INCORRECT!", "IDIOT", }; // produces random feedback when getting it incorrect



while (userGuess = num1 + num2)



{
    num1 = 1 + rand() % 10; // first random number for the adding problem
num2 = 1 + rand() % 10; // second random number for the adding problem
cout << " What is " << num1 << "+" << num2 << "?";
cin >> userGuess;




if (userGuess == num1 + num2)
cout << possibleanswers[rand() % 4] << endl;

else
cout << randWrong[rand() % 3] << endl;


}





return 0;
}


I hope you will be able to solve your loop problem now... if not just ask :)

PS : please use code tags
http://www.cplusplus.com/articles/jEywvCM9/
Wow, that was easy haha. I appreciate the help alot!! Now I just need to figure out how at the end of my program show what the users results were after entering -1. Like you got 10/12 problems correct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    std::cout << "Welcome to the addition game!\n"
              << "Calculate the problems below, when finished type -1 to view results.\n\n";

    std::srand( std::time(nullptr) ) ;

    const char* const correct_msg[] = { "Good Job!", "Well Done!", "Excellent!", "Correct!", };
    const int N_CORRECT = sizeof(correct_msg) / sizeof( correct_msg[0] ) ;

    const char* const wrong_msg[] = { "WRONG!", "TERRIBLE!", "INCORRECT!", "IDIOT", };
    const int N_WRONG = sizeof(wrong_msg) / sizeof( wrong_msg[0] ) ; // **** corrected

    int num_correct = 0 ;
    int num_wrong = 0 ;

    int answer = 0 ;
    while( answer != -1 )
    {
        const int a = 1 + std::rand() % 10;
        const int b = 1 + rand() % 10;

        std::cout << "what is " << a << " + " << b << "? " ;
        std::cin >> answer ;

        if( answer == (a+b) )
        {
            std::cout << correct_msg[ std::rand()%N_CORRECT ] << '\n' ;
            ++num_correct ;
        }

        else if( answer != -1 )
        {
            std::cout << wrong_msg[ std::rand()%N_WRONG ] << '\n' ;
            ++num_wrong ;
        }

        else std::cout << "bye!\n" ;
    }

    std::cout << "         #questions: " << num_correct + num_wrong << '\n'
              << "#correctly answered: " << num_correct << '\n' ;
}
Last edited on
Here is my program updated, how do I make it to where when the answer is wrong to repeat it until it is correct then ask for a new adding problem. Can anyone adjust my program for me?
Sorry for no tags Idk how to do it.

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
cout << "Welcome to the addition game!\n"
<< "Calculate the problems below, when finished type -1 to view results.\n\n";

srand(time(nullptr));

const char* const randCorrect[4] = { "Good Job!", "Well Done!", "Excellent!", "Correct!", };


const char* const randWrong [4] = { "WRONG!", "TERRIBLE!", "INCORRECT!", "IDIOT", };


int num_correct = 0;
int num_wrong = 0;

int answer = 0;
while (answer != -1)
{
const int a = 1 + rand() % 10;
const int b = 1 + rand() % 10;

cout << "what is " << a << " + " << b << "? ";
cin >> answer;

if (answer == (a + b))
{
cout << randCorrect [rand() % 4] << '\n';
++num_correct;
}

else if (answer != -1)
{
cout << randWrong [rand() % 4] << '\n';
++num_wrong;
}

else cout << "Here are your results!\n";
}


cout << "You got " << num_correct << " correct out of " << num_correct + num_wrong << endl;

return 0;


}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    std::cout << "Welcome to the addition game!\n"
              << "Calculate the problems below, when finished type -1 to view results.\n\n";

    std::srand( std::time(nullptr) ) ;

    const char* const correct_msg[] = { "Good Job!", "Well Done!", "Excellent!", "Correct!", };
    const int N_CORRECT = sizeof(correct_msg) / sizeof( correct_msg[0] ) ;

    const char* const wrong_msg[] = { "WRONG!", "TERRIBLE!", "INCORRECT!", "IDIOT", };
    const int N_WRONG = sizeof(correct_msg) / sizeof( correct_msg[0] ) ;

    int num_correct = 0 ;
    int num_wrong = 0 ;

    int answer = 0 ;
    while( answer != -1 )
    {
        const int a = 1 + std::rand() % 10;
        const int b = 1 + rand() % 10;

        try_again:
        {
            std::cout << "what is " << a << " + " << b << "? " ;
            std::cin >> answer ;

            if( answer == (a+b) )
            {
                std::cout << correct_msg[ std::rand()%N_CORRECT ] << '\n' ;
                ++num_correct ;
            }

            else if( answer != -1 )
            {
                std::cout << wrong_msg[ std::rand()%N_WRONG ] << " try again\n" ;
                ++num_wrong ;
                
                // note: there are people who believe that this is unmitigated 'evil'. see:
                // https://duckduckgo.com/?q=goto+evil+site%3Acplusplus.com&atb=v83-2__&ia=web
                goto try_again ;
            }

            else std::cout << "bye!\n" ;
        }
    }

    std::cout << "         #questions: " << num_correct + num_wrong << '\n'
              << "#correctly answered: " << num_correct << '\n' ;
}
Topic archived. No new replies allowed.