Repeat a question until user answers correctly
Sep 21, 2012 at 7:06pm UTC
I am writing a program that generates random numbers for elementary student to practice their multiplication. If they answer incorrectly I want to them to try again.
My code informs them that they are incorrect but it moves on to the next question. I am not sure what to write from here. Nested if statements, loops, ?
This is a class assignment and I am not looking for someone to do my work I just need a little push.
Thanks.
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
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <ctime>
using namespace std;
//#include "GradeBook.h"
int counter;
int main(int argc, const char * argv[])
{
for (int counter = 1; counter<=3; counter++)
{
srand( (unsigned )time(0) );
double x = rand() % 9 +1;//randomly generated number between 1 and 10
double y = rand() % 9 +1;//randomly generated number between 1 and 10
double product = x * y;//the correct answer
int answer;//student's answer input
cout << "What is " << x << " times " << y << endl;
cin >> setprecision(3) >> answer;
if (answer == product)
cout << "Very Good!\n\n" ;
else
cout <<"No. Please try again.\n\n" ;
}
}
Last edited on Sep 21, 2012 at 7:16pm UTC
Sep 21, 2012 at 7:12pm UTC
There is only one question I see here???
Sep 21, 2012 at 7:27pm UTC
Make a while loop that keeps asking for the answer
Sep 21, 2012 at 10:00pm UTC
@ccsdude
Your hint worked. I enventually got it to work. At first I kept on getting an infinite loop until I realized I had to assign a new value to
answer
This is what I came up with:
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
int counter;
int main(int argc, const char * argv[])
{
for (int counter = 1; counter<=3; counter++)
{
srand( (unsigned )time(0) );
double x = rand() % 9 +1;//randomly generated number between 1 and 10
double y = rand() % 9 +1;//randomly generated number between 1 and 10
double product = x * y;//the correct answer
int answer;//student's answer input
cout << "What is " << x << " times " << y << endl;
cin >> answer;
if (answer == product)
cout << "Very Good!\n\n" ;
else
cout <<"No. Please try again.\n\n" << endl;
while (answer!= product) {
cout << "What is " << x << " times " << y << endl;
cin >> answer;
}
}
}
Topic archived. No new replies allowed.