scanf() returns the number of fields successfully converted, and it aborts on first failure. So for example:
int check = scanf("%f", &number);
if( check == 0 )
{
// not a number
}
else
{
// ok
}
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2008-02-07
From the C Runtime Library Reference: (extract)
There must be one format specifier and address for each input field.
scanf might stop scanning a particular field before it reaches the normal end-of-field (whitespace) character, or it might terminate entirely. For details about why this might happen, see When ...scanf Stops Scanning.
Warning: scanf often leads to unexpected results if you diverge from an expected pattern. You must provide information that tells scanf how to synchronize at the end of a line.
The combination of gets or fgets followed by sscanf is safe and easy, and therefore recommended over scanf.
<snip>
When ...scanf Functions Stop Scanning
A ...scanf function might stop scanning a particular input field before reaching the normal field-end character (whitespace), or it might terminate entirely.
Stop and Skip to Next Input Field
...scanf functions stop scanning and storing the current input field and proceed to the next one if any of the following occurs:
An assignment-suppression character (*) appears after the % in the format specifier. The current input field is scanned but not stored.
width characters have been read.
The next character read can't be converted under the current format (for example, an A when the format is decimal).
The next character in the input field does not appear in the search set (or does appear in an inverted search set).
When scanf stops scanning the current input field for one of these reasons, it assumes that the next character is unread and is either
the first character of the following input field, or
the first character in a subsequent read operation on the input.
Terminate
...scanf functions will terminate under the following circumstances:
The next character in the input field conflicts with a corresponding non-whitespace character in the format string.
The next character in the input field is EOF.
The format string has been exhausted.
If a character sequence that is not part of a format specifier occurs in the format string, it must match the current sequence of characters in the input field.
...scanf functions will scan but not store the matched characters.
When a conflicting character occurs, it remains in the input field as if the ...scanf function never read it.
HTH but RTFM
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
hi, i have some n00b question here.
doing a simple program, i'd like a condition that if a result from scanf("%f", &x) is not a number to give me an error message.
i tried something like below code, but didn't work:
[b] .........
float number;
[/b] .........
i guess it is easy to do it, but can't figure out how.
any help please :)
scanf() returns the number of fields successfully converted, and it aborts on first failure. So for example:
int check = scanf("%f", &number);
if( check == 0 )
{
// not a number
}
else
{
// ok
}
Clifford
From the C Runtime Library Reference: (extract)
There must be one format specifier and address for each input field.
scanf might stop scanning a particular field before it reaches the normal end-of-field (whitespace) character, or it might terminate entirely. For details about why this might happen, see When ...scanf Stops Scanning.
Warning: scanf often leads to unexpected results if you diverge from an expected pattern. You must provide information that tells scanf how to synchronize at the end of a line.
The combination of gets or fgets followed by sscanf is safe and easy, and therefore recommended over scanf.
<snip>
When ...scanf Functions Stop Scanning
A ...scanf function might stop scanning a particular input field before reaching the normal field-end character (whitespace), or it might terminate entirely.
Stop and Skip to Next Input Field
...scanf functions stop scanning and storing the current input field and proceed to the next one if any of the following occurs:
When scanf stops scanning the current input field for one of these reasons, it assumes that the next character is unread and is either
Terminate
...scanf functions will terminate under the following circumstances:
If a character sequence that is not part of a format specifier occurs in the format string, it must match the current sequence of characters in the input field.
...scanf functions will scan but not store the matched characters.
When a conflicting character occurs, it remains in the input field as if the ...scanf function never read it.
HTH but RTFM