you can try to use an exception if you know how they work
Basic C help!
Im about 1/4 through a Herbert Schildt book thats all the C know
I know more Pascal and VB
I know more Pascal and VB
Create a small loop.
Within the loop prompt for numeric input.
If they enter numeric, exit loop.
If they enter bad stuff continue the loop.
Before the end of the loop, call function flush (might have been fflush, but I think this was for files). This will clear out the garbage associated with input that was not gotten.
If you code it like this, you should always work. Even for bad data entered.
Within the loop prompt for numeric input.
If they enter numeric, exit loop.
If they enter bad stuff continue the loop.
Before the end of the loop, call function flush (might have been fflush, but I think this was for files). This will clear out the garbage associated with input that was not gotten.
If you code it like this, you should always work. Even for bad data entered.
If someone enters a string when you ask for a float, then you will need to catch it and error, either that or you could write a complex parser to read out the string and make a number from it.
Lets discard option 2, and concentrate on catching the failure.
You can do this (as above) by placing your input request in a loop - probably best to use a do-while for that, you can then test for correct input and continue if it''s ok, otherwise send them back to enter it again.
-Mezz
Lets discard option 2, and concentrate on catching the failure.
You can do this (as above) by placing your input request in a loop - probably best to use a do-while for that, you can then test for correct input and continue if it''s ok, otherwise send them back to enter it again.
-Mezz
June 08, 2000 02:06 PM
Or, if you want a simple way (if your compiler can handle it), just read in all the data as a string and use atof.
This will automatically convert the string into a float, and get rid of invalid data.
This will automatically convert the string into a float, and get rid of invalid data.
June 08, 2000 02:49 PM
Hey coin, why dont u say what functions u are using for i/o and then i can help u a bit mroe cause i check my response before just typing in bullshit like i think the rest did cause it seems like i cant understand wth they are writing
with cout(c++ function) after u scanf data which was supposed to be an integer and instead was entered text u will print out on the screen the number ''0'', if a float was entered only the part before the decimal will be displayed, but with printf:
if u enter a float, only part before decimal will appear, if u enter text a value will appear which i am not sure what it is probably an error value (mine was something like -29600), so that allowes u to get "invalid" data unless u use cprintf in which case u might want to do what u said u did in pascal.
with cout(c++ function) after u scanf data which was supposed to be an integer and instead was entered text u will print out on the screen the number ''0'', if a float was entered only the part before the decimal will be displayed, but with printf:
if u enter a float, only part before decimal will appear, if u enter text a value will appear which i am not sure what it is probably an error value (mine was something like -29600), so that allowes u to get "invalid" data unless u use cprintf in which case u might want to do what u said u did in pascal.
Ignoring input? Thats easy.
do {
myfloat = 0.0;
scanf("%.2f", &myfloat);
} while (!(int)myfloat);
that may work, not sure, i have never tested it out, and i usually don''t use scanf.
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
do {
myfloat = 0.0;
scanf("%.2f", &myfloat);
} while (!(int)myfloat);
that may work, not sure, i have never tested it out, and i usually don''t use scanf.
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement