Thread: Error Checking problem

  1. #1
    Registered User S0n1C's Avatar
    Join Date
    Oct 2006
    Posts
    12

    Error Checking problem

    In the following program, it checks to see what the user enters is a number or not:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void clear_input(void);
    int get_an_int(void);
    double get_a_double(void);
    
    // ************************************************************************** //
    int main()
    {
    	int qty, counter = 0;
    	double price, total = 0;
    
    	do {
    		printf("Enter quantitiy (0 to stop): ");
    		qty = get_an_int();
    
    		if(qty != 0) {
    			printf("Enter unit price: ");
    			price = get_a_double();
    			total += qty * price;
    			counter += qty;
    		}
    	} while (qty != 0);
    	printf("The total is &#37;.2lf\n", total);
    
    	if (counter != 0) {
    		printf(" for %d items at an average price of %.2lf\n", counter,
    			total/counter);
    	}
    	system("PAUSE");
    	return 0;
    }	/* End Main */
    
    // ************************************************************************** //
    /* Clears out any input data remaining unprocessed. This function simply 	  //
     * reads characters until it encounters the new-line that terminates the 	  //
     * input line.																  //
     */
    void clear_input(void)
    {
    	char junk;
    	//do {
    		//scanf("%c", &junk);
    	//} while(junk != '\n');
    	while (getchar() != '\n')
        	;	/* loop body is intentionally empty! */
    }	/* End clear_input
    
    // ************************************************************************** //
    /* Gets an int value from the user and clears out the rest of the input line. //
     * The value entered is returned. If no int data is entered, the user is 	  //
     * given an error message and a change to re-enter.							  //
     */
    int get_an_int(void)
    {
    	int n;
    
    	while(0 == scanf("%d", &n)) {
    	//while((0 == n) || ('\n' != n)) {		/* Almost works, but thinks that everything is an error */
    		clear_input();			// throw away the bad data
    		printf(" Error! Please enter an integer: ");
    	}
    	clear_input();		// throw away any extra data entered
    	return n;
    }	/* End get_an_int */
    
    // ************************************************************************** //
    /* Just like get_an_int, only gets a double rather than an int.				  //
     */
     double get_a_double(void)
     {
    	 double n;
    
    	 while (0 == scanf("%1f", &n)) {
    		 clear_input();		// throw away the bad data
    		 printf(" Error! Please enter a number: ");
    	 }
    	 clear_input();		// throw away any extra data entered
    	 return n;
    }	/* End get_a_double */
    
    /******************************************************************************/
    The problem is that when you run the program it it only checks what the first inputted value is. If you input "123" it is ok "asb" gives an error, "123abc" gives an error, but if you enter "abc123" it givens an error. I have already asked, and i'm not allowed to use isdigit. So can somebody help me with this? I'd really like to know what the solution is.

    Thanks in advance;
    S0n1C!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Reading the data as a string, and checking for valid content is of course one solution - checking that an integer is only digits [using a "myisdigit" function for example]. Floating point is a bit more complex, because you need to [potentially] check for exponent, sign and decimal point, and even then, it's quite hard to check the input RANGE of the number [which I'm not entirely surt if scanf does either].

    Also, I think you mean
    "123abc" gives no error
    ?

    It wouldn't, because according to scanf, that's a fine input - it just has some garbage at the end, but scanf "handles" that by ending the number of any non-numeric input.

    --
    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
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    If you want to read a double, then you have to use the "%ld" format code. Also, when using scanf with something else than string, i wouldn't recommend putting any number next to the format code. Not sure what it does for things like double or int, maybe i could check...

    And, i'm not sure to understand what you are saying. Indeed, if you enter "123abc", it doesn't give an error; if you enter "abc123", it gives an error. This does look correct.

    But if your goal is to reject something like "123abc", well, start by trying to read an integer with scanf, and then check the input buffer if the next character isn't the '\n' you'll usually find in normal situations.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Or rather "%lf" instead of "%1f". The number restricts the number of characters that can be read (in this case, to 1 -- or at least, that's what gcc does); so typing in (for instance) 5.5 only reads 1 character, gets a float of 5.000000 and leaves ".5" behind in the input buffer for the next time. (The second read will fail, since it only sees ".", and third will be another 5.)

    (Unfortunately, you can't do something like %.2f to make sure there's only two decimals behind, drat the luck.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM