//* Fail-safe reading with range checking *

#include <iostream> using namespace std; double ReadDouble(double min, double max) { double a; for (;;) { cin >> a; if ( cin.fail() ) { cin.clear(); cin.ignore(256, '\n'); cout << "I can't read it!\nplease reenter the number "; } else { if (a>=min && a<=max) break; else cout << "It's out of range!\nplease reenter the number "; } } return(a); } int main() { double x ; cout << "Please enter a decimal numbers between 0 and 4." << endl; cout << "x="; x=ReadDouble(0, 4); cout << "You have entered: " << "x=" << x << ", thank you." << endl; return(0); }