I have a question regarding the following program. This program uses
random variables and asks the user to enter the correct sum for 5 times.
Until, we enter an integer the program runs well but as soon as we
enter any double value or a character, the program gives incorrect
message for the whole loop and comes out of the loop automatically. Why
does this happen? Can anyone help me?
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
int num1=0;
int num2=0;
int correctanswer=0;
int useranswer=0;
srand(static_cast<int>(time(0)));
for (int x=1; x<6; x++)
{
num1=1+rand()%(100-1+1);
num2=1+rand()%(10-1+1);
correctanswer= num1+num2;
cout<<"What is the sum of "<<num1<<" and "<<num2<<"?"; cin>>useranswer;
if (useranswer==correctanswer)
cout<<"Excellent! Correct Answer";
else
cout<<"Sorry!the correct answer is: "<<correctanswer; cout<<endl<<endl;
}
system("pause");
return 0;
}This is the wrong forum for a general C++ debugging question. This forum is for discussing the C++ Standard.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-discussion/.