Hi, I have an assignment where I create a program that prompts user to select which math game they would like to play. I have successfully created a loop in which 5 questions are displayed and user inputs answer, and computer displays incorrect/correct. However, I would like to include a grade counter that allows the user to see their grade after they answer a question.
Thanks so much!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
for (int count=1;count<6;count++)
{
int x1,x2,s,result,x3,sum=0;
srand(time(0));
x1=rand()%9+1;
x2=rand()%9+1;
x3=(x1+x2);
cout<<"\n"<<x1<<"+"<<x2<<"=?"<<endl;
cin>>result;
if (result==x3)
{cout<<"Correct. "<<x1<<"+"<<x2<<"="<<x3<<endl;
sum++;
cout<<"Your grade: "<<sum<<"/5"<<endl;}
if (result!=x3)
{cout<<"Incorrect. "<<x1<<"+"<<x2<<"="<<x3<<endl;
}
}
Line 13: You're doing integer division. Any number (sum) less than 5 divided by 5 is going to be 0.
Line 4: Do not call srand() within a loop or a random number function. srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main(). http://www.cplusplus.com/reference/cstdlib/srand/
line 14: No need to test result!= x3. Just use an else