#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    103
    Rep Power
    5

    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;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    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.
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    103
    Rep Power
    5
    im not allowed to use fgets i am only allowed to use scanf
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    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.

IMN logo majestic logo threadwatch logo seochat tools logo