Ideas for error checking from command line

This is a discussion on Ideas for error checking from command line within the C Programming forums, part of the General Programming Boards category; I am trying to read three doubles from a user's input on the command line. However, my error checking is ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    96

    Ideas for error checking from command line

    I am trying to read three doubles from a user's input on the command line. However, my error checking is something to be desired. If I don't enter the correct number of doubles or don't enter a double the program goes into an infinite loop. Here is the main which is handling the I/O:

    Code:
    int main() {
    	double a, b, c, x1, x2;
    	int qret, count;
    
    	while(1) {
    		printf("Enter variables for quadratic equation:  ");
    		
    		if( (count = scanf("%lf %lf %lf", &a, &b, &c)) != 3) {
    			printf("Invalid Input: Format Is <double> <double> <double>\n");
    			printf("%d \n", count);			
    		}			
    
    		else if((qret = qsolver(a, b, c, &x1, &x2)) == 3) {
    			printf("Error Value:  %d\n", qret);		
    			printf("X1 = %lf   X2 = %lf\n\n", x1, x2);
    		}
    	}
    	return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you enter nonsense (e.g. "abc" instead of a number) then you need to "clean up" in the input buffer, as scanf() happens to work in such a way that it reads everything up until it finds something it doesn't like/want. So if you have entered "abc", and scanf() expects a number, it will look at the 'a', say "I don't need that now", and go back to your function with nothing extracted from the input buffer. It is fairly easy to solve by using a loop like this [you may want to make it into a function]:
    Code:
    int ch;
    while((ch = getchar()) != '\n' && ch != EOF);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Also, you might want to look for EOF, and if you find it, quit the program. You could check scanf()'s return value against EOF, or just use feof(). (Or ch after executing matsp's code.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    Thank you that helped for the infinite loop issue, is there a way to make scanf() stop waiting if not enough inputs were entered and if there are too many inputs scanf() just ignore's the extras correct?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by NuNn View Post
    Thank you that helped for the infinite loop issue, is there a way to make scanf() stop waiting if not enough inputs were entered and if there are too many inputs scanf() just ignore's the extras correct?
    My code works also to clean out extraneous "valid" input. It does not solve the problem of only one or two inputs when you expect 3. To solve that, you will need to read a string (for example fgets()) and then use sscanf() to read from the string - that ALSO solves the problem of dealing with "bad" input nicely, so you don't actually need any loops or such).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    Thank you very much I will give that a try.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    I have updated my file and it seemed to be working fine, however there is a minor glitch:

    Code:
    int main() {
    	double a, b, c, x1, x2;
    	int qret, count;
    	char line[30];
    
    	while(1) {
    		count=0;
    		printf("Enter variables for quadratic equation (Enter 'q' or 'quit' to exit):  ");
    		fgets(line, 30, stdin);
    	
    		if((strncmp(line, "quit", 4) > 0) || (strncmp(line, "q", 4) > 0))
    			return 0;
    		else
    			count = sscanf(line, "%lf %lf %lf", &a, &b, &c);
    
    		if(count != 3) 
    			printf("Invalid Input: Format Is <double> <double> <double>\n\n");		
    					
    
    		else if(count == 3) {
    			qret = qsolver(a, b, c, &x1, &x2);
    			printf("Error Value:  %d\n", qret);		
    			printf("X1 = %lf   X2 = %lf\n\n", x1, x2);
    		}
    	}
    	return 0;
    }
    for some reason the count returned by sscanf is 30 some of the time and im unsure as to why this is. I see that my array size is 30, but shouldn't sscanf() return the number of correctly read items?

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    Hah nevermind, one of my error checking items didn't have a new line character in it so it was appending making it appear so, disregard and thanks.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    strncmp(line, "q", 4) - meaningless - the second string is only 1 char long so comparison will never be longer than 2 chars...

    better check the return value of fgets
    for example

    Code:
    while(fgets(line, 30, stdin))
    {
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Project ideas.
    By Wraithan in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 04-25-2009, 03:29 PM
  2. Ideas, how do you get them? xD
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-22-2009, 02:53 AM
  3. cool ideas for a game
    By Shadow12345 in forum Game Programming
    Replies: 7
    Last Post: 05-18-2004, 08:37 PM
  4. idea's idea's idea's
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-29-2002, 12:30 AM
  5. Small app ideas
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-15-2002, 07:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21