Looping problem

48 views
Skip to first unread message

Anmol Shrestha

unread,
Jul 24, 2013, 2:29:56 PM7/24/13
to std-dis...@isocpp.org

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; }

Xeo

unread,
Jul 24, 2013, 3:16:22 PM7/24/13
to std-dis...@isocpp.org
This is the wrong forum for a general C++ debugging question. This forum is for discussing the C++ Standard.

Ville Voutilainen

unread,
Jul 24, 2013, 3:37:37 PM7/24/13
to std-dis...@isocpp.org
On 24 July 2013 22:16, Xeo <hivem...@hotmail.de> wrote:
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/.
 
 

Vlad from Moscow

unread,
Jul 24, 2013, 3:55:05 PM7/24/13
to std-dis...@isocpp.org
The appropriate forum where you can ask such questions is www.cplusplus.com.

среда, 24 июля 2013 г., 22:29:56 UTC+4 пользователь Anmol Shrestha написал:
Reply all
Reply to author
Forward
0 new messages