Are you stuck with an infinite loop while using fail() in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Are you stuck with an infinite loop while using fail() in C++?

Lesson for today: don't forget to add: cin.sync(); OR for Linux users: cin.ignore(numeric_limits < streamsize >::max(), '\n'); after clear(); Example: while(true){ cout << "The limit is: "; cin >> limit; if (cin.fail()){ cout << "I don't know what to do with this. Please enter numeric limit greater than 0: " << endl << endl; limit = 0; cin.clear(); cin.sync(); //for Linux users: //cin.ignore(1000, '\n'); //cin.ignore(numeric_limits < streamsize >::max(), '\n'); } else{ break; } }

1st Aug 2017, 10:08 PM
Justyna Bejgrowicz
Justyna Bejgrowicz - avatar
3 Answers
+ 3
@Justyna Thank you for sharing!
1st Aug 2017, 10:26 PM
Manual
Manual - avatar
+ 1
hi you have a FACE BOOK
2nd Aug 2017, 9:36 PM
saman luqman
saman luqman - avatar
0
well done
2nd Aug 2017, 9:35 PM
saman luqman
saman luqman - avatar