The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Error checking scanf
Discuss Error checking scanf in the C Programming forum on Dev Shed. Error checking scanf C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 12th, 2013, 12:17 PM
|
|
Contributing User
|
|
Join Date: Dec 2009
Posts: 81
Time spent in forums: 15 h 4 m
Warnings Level: 1
Reputation Power: 4
|
|
|
Error checking scanf
i have to use scanf to make a math equation
i have to use the return type to error check it
but
scanf always returns the correct amount of variables entered
even if i try to enter the wrong amount of variables
if i enter 1 variable and i need 3, after i press enter, it is still scanning and waits for 2 more variables
even if i enter 4 or more variables, scanf still returns "valid expression"
please help
Code:
#include<stdio.h>
void main()
{
int precision, numVars;
double a, b;
char c;
printf("Enter Precision");
scanf("%i",&precision);
printf("Enter Expression");
numVars=scanf("%lf %c %lf", &a, &c, &b);
if(numVars!=3)
{
printf("Invalid Expression");
}
else
{
printf("Valid Expression");
}
for(;;);
return;
}
|

February 12th, 2013, 12:48 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | Originally Posted by chopficaro even if i enter 4 or more variables, scanf still returns "valid expression" |
Yes, that's correct and exactly what we would expect.
You told scanf to read three arguments and you supplied it with three arguments, so it's satisfied. In fact, it stopped reading from the input buffer as soon as it had found that third valid argument. That means that the extra arguments you had entered are still in the input buffer. When you call scanf again, it will immediately start with that fourth argument, that extra one, that you had entered.
That is how scanf works.
|

February 12th, 2013, 12:50 PM
|
 |
Contributed User
|
|
|
|
You've described how scanf works.
All whitespace (including newlines) are the same to scanf.
So whether you type in
1 + 2
or
1
+
2
it's all the same to scanf.
If you want to make a line the unit of input, then you need to use fgets() and sscanf.
Eg.
Code:
char buff[100];
printf("Enter Precision");
fgets(buff,sizeof(buff),stdin);
sscanf(buff,"%i",&precision);
printf("Enter Expression");
fgets(buff,sizeof(buff),stdin);
numVars=sscanf("%lf %c %lf", &a, &c, &b);
Oh, and main returns int, not void.
|

March 20th, 2013, 11:59 AM
|
|
Contributing User
|
|
Join Date: Dec 2009
Posts: 81
Time spent in forums: 15 h 4 m
Warnings Level: 1
Reputation Power: 4
|
|
|
im not allowed to use fgets i am only allowed to use scanf
|

March 20th, 2013, 12:10 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
OK, so break that single scanf up into separate scanf's. That way, you can check the return value of each one for being valid. Just be sure to leave a space in front of the character input (ie, " %c") so that it doesn't just read in the space character or newline.
Seems kind of odd that your homework assignment would still not be due more than a month later.
BTW, this:
for(;;);
is an infinite loop. Your program will never end on its own. You will need to Ctrl-C out of it or worse.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|