C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 12th, 2013, 12:17 PM
chopficaro chopficaro is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 81 chopficaro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
  #2  
Old February 12th, 2013, 12:48 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,124 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 47 m 13 sec
Reputation Power: 1949
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.

Reply With Quote
  #3  
Old February 12th, 2013, 12:50 PM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,835 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 16 h 3 m 48 sec
Reputation Power: 1774
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

Reply With Quote
  #4  
Old March 20th, 2013, 11:59 AM
chopficaro chopficaro is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 81 chopficaro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old March 20th, 2013, 12:10 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,124 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 47 m 13 sec
Reputation Power: 1949
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Error checking scanf

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap