I am writing a basic program for class that requires me to validate the input for the user. In the code segment below I need to be sure that he user enters an integer. The code should only loop if the user enters something other than an integer. A "do-while" loop was my idea but I am not sure what to put for the while statement. I am not even sure if a "do-while" statement is the best idea.
1 2 3 4 5
do{
int dice = 0;
cout << "Please enter the amount of dice: ";
cin >> dice;
} while (???);
I am only a beginner looking for some expert advice. Thanks ahead!
you can use 'cin.get()' to retrieve the first character in the stream after the 'cin>>dice. Also declare 'dice' outside the do...while so you can use this variable also in other part of code
1 2 3 4 5 6
int dice;
do{
cout << "Please enter the amount of dice: ";
cin >> dice;
cin.clear(); //
} while (cin.get() != (int)'\n' ); // the condition