How to code a loop that keep asking user to input until the user input a correct number ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to code a loop that keep asking user to input until the user input a correct number ?

30th Nov 2017, 11:09 PM
S.K
5 Answers
+ 3
while ( 1 ) { cin>>num; if ( num == correct_num ) break; } here correct_num is variable on which the correct value is assigned already
2nd Dec 2017, 2:11 PM
Я. ♨
Я. ♨ - avatar
+ 2
do{ // ask for input: cin>>input; } while(input ! = correctInput);
30th Nov 2017, 11:21 PM
Echo One
Echo One - avatar
+ 1
It is wrong. the cin>>num; needs to be in the do while loop.
1st Dec 2017, 5:29 AM
Paul
Paul - avatar
0
#include <iostream> using namespace std; int main() { int num; cin>>num; do { cout<<"enter again"; } while(num != num); return 0; } correct me if this is wrong. thanks
1st Dec 2017, 12:31 AM
S.K
0
And the num != num is wrong too. the second num needs to be an other variabele or a value.
1st Dec 2017, 5:33 AM
Paul
Paul - avatar