1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <iostream> #include <string> #include <limits> using namespace std; int main() { cout << "Please, enter a number\n"; int n; // n is a number, it cannot be anything else // cin >> n reads a number. If it fails, it returns false, // so we check that condition in a loop: while( !(cin >> n) ) { cout << "You need to enter a number\n"; cin.clear(); // reset the error flags // here you still have unprocessed input. You have options: // 1. ignore: cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 2. read into a string // string str; // getline(cin, str); // 3. read into char (in a loop, since there may be many) // char c; // while(cin.get(c) && c != '\n') // { // } } cout << "Thank you for entering the number " << n << '\n'; } |
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8bGltaXRzPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwppbnQgbWFpbigpCnsKICAgIGNvdXQgPDwgIlBsZWFzZSwgZW50ZXIgYSBudW1iZXJcbiI7CiAgICBpbnQgbjsgLy8gbiBpcyBhIG51bWJlciwgaXQgY2Fubm90IGJlIGFueXRoaW5nIGVsc2UKICAgIC8vIGNpbiA+PiBuIHJlYWRzIGEgbnVtYmVyLiBJZiBpdCBmYWlscywgaXQgcmV0dXJucyBmYWxzZSwKICAgIC8vIHNvIHdlIGNoZWNrIHRoYXQgY29uZGl0aW9uIGluIGEgbG9vcDoKICAgIHdoaWxlKCAhKGNpbiA+PiBuKSApCiAgICB7CiAgICAgICAgY291dCA8PCAiWW91IG5lZWQgdG8gZW50ZXIgYSBudW1iZXJcbiI7CiAgICAgICAgY2luLmNsZWFyKCk7IC8vIHJlc2V0IHRoZSBlcnJvciBmbGFncwogICAgICAgIC8vIGhlcmUgeW91IHN0aWxsIGhhdmUgdW5wcm9jZXNzZWQgaW5wdXQuIFlvdSBoYXZlIG9wdGlvbnM6CiAgICAgICAgLy8gMS4gaWdub3JlOgogICAgICBjaW4uaWdub3JlKG51bWVyaWNfbGltaXRzPHN0cmVhbXNpemU+OjptYXgoKSwgJ1xuJyk7CiAgICAgICAgLy8gMi4gcmVhZCBpbnRvIGEgc3RyaW5nCi8vICAgICAgICBzdHJpbmcgc3RyOwovLyAgICAgICAgZ2V0bGluZShjaW4sIHN0cik7CiAgICAgICAgLy8gMy4gcmVhZCBpbnRvIGNoYXIgKGluIGEgbG9vcCwgc2luY2UgdGhlcmUgbWF5IGJlIG1hbnkpCi8vICAgICAgICBjaGFyIGM7Ci8vICAgICAgICB3aGlsZShjaW4uZ2V0KGMpICYmIGMgIT0gJ1xuJykKLy8gICAgICAgIHsKLy8gICAgICAgIH0KICAgIH0KICAgIGNvdXQgPDwgIlRoYW5rIHlvdSBmb3IgZW50ZXJpbmcgdGhlIG51bWJlciAiIDw8IG4gPDwgJ1xuJzsKfQo=
-
upload with new input
-
result: Success time: 0.01s memory: 2684 kB returned value: 0
a b string 123
Please, enter a number You need to enter a number You need to enter a number You need to enter a number Thank you for entering the number 123


