MeehoweCK

Untitled

Oct 15th, 2020
2,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. /* WHILE loop
  6.  
  7. while(condition)
  8.     command to be executed while the condition is satisfied
  9.  
  10. */
  11.  
  12. int main()
  13. {
  14.     int age;
  15.     cout << "Please enter your age: ";
  16.     cin >> age;
  17.  
  18.     while(cin.fail())      // it returns true if the input buffer has been given wrong data
  19.     {
  20.         cout << "You've entered the wrong data. Please enter your age again: ";
  21.         cin.clear();                    // hides the error flag
  22.         cin.ignore(99999, '\n');        // empties the buffer
  23.         cin >> age;
  24.     }
  25.  
  26.     cout << "You are " << age << " years old. ";
  27.     if(age > 17)
  28.         cout << "You are an adult.\n";
  29.     else
  30.         cout << "You are not an adult.\n";
  31.  
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment