Pretoript

readinput

Feb 13th, 2021
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <limits>
  3.  
  4. int main()
  5. {
  6.     int x;
  7.     int y;
  8.  
  9.     bool badInput = false;
  10.  
  11.     do
  12.     {
  13.         std::cout << "Please give the first number: ";
  14.         std::cin>>x;
  15.         if(std::cin.fail())
  16.         {
  17.             std::cout << "Wrong input! Please give the number again!" << std::endl;
  18.             badInput = true;
  19.             std::cin.clear();
  20.             std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  21.         }
  22.         else
  23.         {
  24.             badInput = std::cin.get() != '\n';
  25.             if(badInput)
  26.             {
  27.                 std::cout << "Wrong input! Please give the number again!" << std::endl;
  28.                 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  29.             }
  30.         }
  31.     }while(badInput);
  32.  
  33.     do
  34.     {
  35.         std::cout << "Please give the second number: ";
  36.         std::cin>>y;
  37.         if(std::cin.fail())
  38.         {
  39.             std::cout << "Wrong input! Please give the number again!" << std::endl;
  40.             badInput = true;
  41.             std::cin.clear();
  42.             std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  43.         }
  44.         else
  45.         {
  46.             badInput = std::cin.get() != '\n';
  47.             if(badInput)
  48.             {
  49.                 std::cout << "Wrong input! Please give the number again!" << std::endl;
  50.                 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  51.             }
  52.         }
  53.     }while(badInput);
  54.  
  55.     std::cout << "x: " << x << ", y: " << y << std::endl;
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment