Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Error Trapping (http://www.go4expert.com/forums/error-trapping-t28799/)

arholub 28Jul2012 02:12

Error Trapping
 
Windows 7, Visual Studio 2010, C++

int main()
{
float x,y;
do{
cout<<endl;
cout<<"Please enter a number greater than 3 and less than 9 ";cin>>x;
if(x<3 || x>9)
cout<<"ERROR"<<endl;
}while(x<3 || x>9);

more code for y....

***********************
Above code works fine for float x ; Want to trap error if x is ALPHA.
How???? (I haven't coded in a while)

xpi0t0s 29Jul2012 13:35

Re: Error Trapping
 
You would have to read the input into a string variable, then check if it's a valid number with a function like atoi(), and if not, throw a suitable error.
Code:

char buf[32];
fgets(buf,30,stdin); // order of params might be wrong
int num=atoi(buf);
if (num<3 || num>9) printf("Wrong\n");


arholub 30Jul2012 06:19

Re: Error Trapping
 
Thank you...good start...

need: float num=ATOF(buf);.....

Now working....

Really like this forum...Thanks again...


All times are GMT +5.5. The time now is 08:45.