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.
{
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;
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.
{
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;
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <random>
usingnamespace 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
constchar* possibleanswers[4] = { // produces random feedback when getting it correct
"Good Job!", "Well Done!", "Excellent!", "Correct!", };
constchar* 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 :)
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
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.