Need Help With Error Input Dealing

This is a discussion on Need Help With Error Input Dealing within the C Programming forums, part of the General Programming Boards category; Hi all. This is a part of my program, and I have been experiencing some problems. The program works as ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    Need Help With Error Input Dealing

    Hi all. This is a part of my program, and I have been experiencing some problems.

    The program works as intended, but then I thought I should add in some error checking into it. When I prompt the user to input numbers, I would like the program to ask the user to input the number again if he/she entered a non-number. However, the way I've written the code right now just seems to print the error message infinate times.

    Can someone give some advice? Thanks.

    Code:
    int readlist(int arr[], int siz)
    {
    	int i;
    	int status;
    	int ch;
    
    	printf("\nEnter %i numbers: \n", siz);
    
    	for(i = 1; i < siz + 1; i++)
    	{
    		printf("%i) ", i);
    
    		status = scanf("%i", arr++);
    	
    		while(status != 1)
    		{
    			printf("What? Naw... Again pls.. \n");
    			status = scanf("%i", arr);
    		}
    	}
    
    	return *arr;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    The problem is, you increment arr before you know whether you were successful or not.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    Quote Originally Posted by Salem View Post
    The problem is, you increment arr before you know whether you were successful or not.
    So.. like this?

    Code:
    for(i = 1; i < siz + 1; i++)
    	{
    		printf("%i) ", i);
    
    		status = scanf("%i", arr);
    	
    		while(status != 1)
    		{
    			printf("What? Naw... Again pls.. \n");
    			status = scanf("%i", arr);
    		}
    		arr++;
    	}
    But it's still printing infinate "What? Naw... Again pls.."s..

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Well if you type "hello" and scanf is waiting for an integer, then it will loop forever.

    If scanf fails, you should remove some erroneous input, typically up to the next newline.

    Ways of "flushing" the input NOT involving fflush(stdin) are in the FAQ.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    Quote Originally Posted by Salem View Post
    Well if you type "hello" and scanf is waiting for an integer, then it will loop forever.

    If scanf fails, you should remove some erroneous input, typically up to the next newline.

    Ways of "flushing" the input NOT involving fflush(stdin) are in the FAQ.

    Thanks a lot. Found the line

    Code:
    while ((ch = getchar()) != '\n' && ch != EOF);
    and it fixed the problem.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547

    Smile

    You might like to play around with this...
    The trick is to check the loop AFTER the entry...
    Code:
         status = 0;
         do 
            { printf("Enter a number : ");
               status = scanf("%i", &arr);  
               If (status != 1)
                 Printf("\nError... ");  }
        while(status != 1)
    A couple of hints...
    Always tell your operator what is expected... they're not mind readers.
    Always inform them of errors but in the most innocuous way possible.
    Last edited by CommonTater; 02-18-2011 at 12:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dealing math in string with C.
    By procs in forum C Programming
    Replies: 3
    Last Post: 05-09-2010, 09:41 PM
  2. Replies: 3
    Last Post: 06-23-2005, 06:39 PM
  3. dealing with copyrights
    By JOsephPataki in forum Game Programming
    Replies: 2
    Last Post: 06-21-2003, 03:08 PM
  4. Dealing with directx units
    By Frog22 in forum Game Programming
    Replies: 2
    Last Post: 10-23-2002, 10:42 PM
  5. card shuffling dealing question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2002, 07:37 PM

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