#include <iostream>
#include <string>
#include <sstream>
double ask_for_number(std::string const &prompt)
{
double x;
std::string line;
while((std::cout << prompt << ": ") //prompt for input
&& std::getline(std::cin, line) //read whole line (almost never fails)
&& !(std::istringstream{line} >> x)) //validate input
{
std::cout << "Invalid input, please try again." << std::endl;
}
return x;
}
int main()
{
int n1 = ask_for_number("Enter a number");
int n2 = ask_for_number("Enter another number");
std::cout << "n1 + n1 = " << n1 + n2 << std::endl;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8c3N0cmVhbT4KCmRvdWJsZSBhc2tfZm9yX251bWJlcihzdGQ6OnN0cmluZyBjb25zdCAmcHJvbXB0KQp7CiAgICBkb3VibGUgeDsKICAgIHN0ZDo6c3RyaW5nIGxpbmU7CiAgICB3aGlsZSgoc3RkOjpjb3V0IDw8IHByb21wdCA8PCAiOiAiKSAvL3Byb21wdCBmb3IgaW5wdXQKICAgICAgICYmIHN0ZDo6Z2V0bGluZShzdGQ6OmNpbiwgbGluZSkgLy9yZWFkIHdob2xlIGxpbmUgKGFsbW9zdCBuZXZlciBmYWlscykKICAgICAgICYmICEoc3RkOjppc3RyaW5nc3RyZWFte2xpbmV9ID4+IHgpKSAvL3ZhbGlkYXRlIGlucHV0CiAgICB7CiAgICAgICAgc3RkOjpjb3V0IDw8ICJJbnZhbGlkIGlucHV0LCBwbGVhc2UgdHJ5IGFnYWluLiIgPDwgc3RkOjplbmRsOwogICAgfQogICAgcmV0dXJuIHg7Cn0KCmludCBtYWluKCkKewoJaW50IG4xID0gYXNrX2Zvcl9udW1iZXIoIkVudGVyIGEgbnVtYmVyIik7CglpbnQgbjIgPSBhc2tfZm9yX251bWJlcigiRW50ZXIgYW5vdGhlciBudW1iZXIiKTsKCglzdGQ6OmNvdXQgPDwgIm4xICsgbjEgPSAiIDw8IG4xICsgbjIgPDwgc3RkOjplbmRsOwp9Cg==