-
Not a float
What i am trying to do is to set a value for a. the value should be a number (float).
but i need to make it so that if a char is entered, then the program will end, or say something like "this is incorrect. please try again".
this is a sample of my code. if u need more please ask.
Code:
cout << "Please enter the value for a, the lower bound ";
cin >> a;
now, if i put a number in there it'll carry on doing whatever it does. but if i put a letter in, i'd like it to stop. so what's the way of saying
"if a is not a flaot then.....
-
-
Code:
#include <limits>
/* *** CODE HERE *** */
std::cout << "Please enter the value for a, the lower bound ";
while(!(cin >> a)) // If the stream fails from bad input
{
std::cin.clear(); // Clear the fail state
std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n'); // Clear the buffer
std::cout << "Invalid statement, please re-enter: "; // Reprompt
}
-