Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <limits>
- int main()
- {
- int x;
- int y;
- bool badInput = false;
- do
- {
- std::cout << "Please give the first number: ";
- std::cin>>x;
- if(std::cin.fail())
- {
- std::cout << "Wrong input! Please give the number again!" << std::endl;
- badInput = true;
- std::cin.clear();
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- }
- else
- {
- badInput = std::cin.get() != '\n';
- if(badInput)
- {
- std::cout << "Wrong input! Please give the number again!" << std::endl;
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- }
- }
- }while(badInput);
- do
- {
- std::cout << "Please give the second number: ";
- std::cin>>y;
- if(std::cin.fail())
- {
- std::cout << "Wrong input! Please give the number again!" << std::endl;
- badInput = true;
- std::cin.clear();
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- }
- else
- {
- badInput = std::cin.get() != '\n';
- if(badInput)
- {
- std::cout << "Wrong input! Please give the number again!" << std::endl;
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- }
- }
- }while(badInput);
- std::cout << "x: " << x << ", y: " << y << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment