Thread: Trouble with error checking on input from user.

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

    Trouble with error checking on input from user.

    Hello,

    I am trying to parse a users input correctly and for the most part I have done so. However, the only issue which still remains is that when a user enters an input of "1.9 3.4 3.h", the '3.h' is automatically converted to 3.0 which I don't want. I would like to prompt the user to let them know that the input was invalid. Here is my code for the error check so far:

    Code:
    	
    		while(1) {
    			/*Prompt user for input and process the information*/
    			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", 1) == 0) 
    				return 0;
    			else dataScanned = sscanf(line, "%lf %lf %lf", &a, &b, &c);
    		
    			/*The data read in was invalid*/
    			if(dataScanned != 3) 
    				printf("Invalid Input: Format Is <double> <double> <double>\n\n");		
    						
    			/*Send the data off to qsolver for processing*/
    			else if(dataScanned == 3) {
    				qret = qsolver(a, b, c, &x1, &x2);
    				printf("Error Value:  %d\n", qret);		
    				printf("X1 = %f   X2 = %f\n\n", x1, x2);
    			}
    		}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > dataScanned = sscanf(line, "%lf %lf %lf", &a, &b, &c);
    Try
    Code:
    int pos;
    dataScanned = sscanf(line, "%lf %lf %lf%n", &a, &b, &c, &pos);

    Then look at line[pos] to work out whether it was a valid end to your float, or just trailing garbage.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    I tried what you suggested by when I try to see what 'pos' equals I'm getting nothing from the input

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by NuNn View Post
    I tried what you suggested by when I try to see what 'pos' equals I'm getting nothing from the input
    Show your code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    I added a printf after the line to see what value 'pos' was assigned.

    Code:
    		
    			if(strncmp(line, "quit", 4) == 0 || strncmp(line, "q", 1) == 0) 
    				return 0;
    			else dataScanned = sscanf(line, "%lf %lf %lf%n", &a, &b, &c, &pos);
                            printf("pos = %c\n", pos);

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by NuNn View Post
    I added a printf after the line to see what value 'pos' was assigned.

    Code:
    		
    			if(strncmp(line, "quit", 4) == 0 || strncmp(line, "q", 1) == 0) 
    				return 0;
    			else dataScanned = sscanf(line, "%lf %lf %lf%n", &a, &b, &c, &pos);
                            printf("pos = %c\n", pos);
    pos is an int, not a char. It is the index of the first character of the string which was NOT converted. In your case, the input is valid only if this character is the end of the string. You want to check if:

    Code:
    dataScanned == 4 && line[pos] == '\0'
    If so, the input was valid. (It's 4 instead of 3 because the %n counts as a conversion)

    I think it's overly stringent, since it doesn't allow trailing whitespace on the line. If it were me, I'd treat the example as valid input -- you'd go through your loop, then pick up the "h" the next time through and discard it as unrecognized. In other words, I think your code is fine as it is, but it's an aesthetic choice.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    Alrighty thank you once again brewbuck

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it also could be a whitespace like \n
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by vart View Post
    it also could be a whitespace like \n
    True. So you should check for '\n' as well as '\0'.

    If it were me I'd trim all leading and trailing whitespace before conversion.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  3. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  4. Trouble with receiving input
    By BellosX in forum C Programming
    Replies: 4
    Last Post: 09-20-2001, 11:58 PM