I know this is a common question. I've looked through the FAQ's, done countless searches on the net, looked in my MS Visual C++ .NET book, and yet I still am having a hard time doing what "should" be simple, and that is check to see that a numeric entry is valid. I want the input to be a float type. If the entry is a char, then return to the input and keep looping back until a valid entry is made.
I've tried a do while, while and goto, yet all I get is an infinate loop when I enter a char. Below is the code that has come closest to what I want. If I can just get it to loop, then I'd be ok.
I also tried the ctype functions like isdigit, but so far no luck. *g* This is sure easier in Java, Perl and JavaScript.
Any guidence would be most appreciated.
tia...mike
I've tried a do while, while and goto, yet all I get is an infinate loop when I enter a char. Below is the code that has come closest to what I want. If I can just get it to loop, then I'd be ok.
I also tried the ctype functions like isdigit, but so far no luck. *g* This is sure easier in Java, Perl and JavaScript.
Any guidence would be most appreciated.
tia...mike
Code:
#include <iostream>
using namespace std;
int main(void)
{
float number = 0;
cout << "Enter a number: ";
fflush(stdout);
if(!(cin >> number))
{
cerr << "Sorry, that is a non-valid entery" << endl;
}
return 0;
}