dimanche 3 janvier 2016

How to phrase the if statement for this C++ code? (How to prevent infinite looping in this case?) [duplicate]

This question already has an answer here:

I'm writing a casino game on C++ turbo (latest version for windows). So in one particular snippet of the program, the user is required to enter an initial amount strictly between 0 dollars and 100000 dollars.

I created a do-while loop with an embedded if statement:

do{
cout << "\n\nEnter a deposit amount (between $0 and $100000) to play game : $";
cin >> amount;
if(amount<=0||amount>=100000)
cout<<"Please re-enter your amount";
}while(amount<=0||amount>=100000);


The issue arises when the user (i.e. me) enters a character or a decimal; the program then loses control and it keeps looping indefinitely.

Question: How can I phrase an if-statement which requests the user to re-enter the amount if something other than an integer is entered? How can I, subsequently, prevent the program from going out of control?

Aucun commentaire:

Enregistrer un commentaire