Niby piszesz coś o good i fail, ale myślę, że to też dobry wybór.
#include <string>
#include <sstream>
#include <iostream>
int main()
{
std::string line;
double number;
while (std::getline(std::cin, line))
{
std::stringstream ss(line);
if (ss >> number)
{
if (ss.eof())
{ // Success
break;
}
}
std::cout << "Error!" << std::endl;
}
std::cout << "Finally: " << number << std::endl;
}
A jak chcesz koniecznie z tym
#include<iostream>
#include<limits>
int main()
{
std::cout << "Enter an int: ";
int x = 0;
while(!(std::cin >> x)){
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid input. Try again: ";
}
std::cout << "You enterd: " << x << std::endl;
}