Trouble with input validation

This is a discussion on Trouble with input validation within the C Programming forums, part of the General Programming Boards category; I can't seem to control this loop the way I want. I want to scan and validate input, if input ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39

    Trouble with input validation

    I can't seem to control this loop the way I want. I want to scan and validate input, if input incorrect, then print an error message and scan input again. The input is validated correctly but instead of returning control to the keyboard, it just loops back to the beginning of the while().
    Code:
    void A_getScores(student* stuArray)
    {
    	int i = 0;
    	int inputChecker;
    	
    	while (i < sizeof(stuArray))
    	{
    		printf("Enter the three scores for %s %s: ", 
    			   stuArray[i].studentName.firstName,
    			   stuArray[i].studentName.lastName);
    		inputChecker = scanf("%g %g %g", stuArray[i].grade, 
    						  stuArray[i].grade+1, stuArray[i].grade+2);
    		stuArray[i].average = (stuArray[i].grade[0] + 
    							   stuArray[i].grade[1] +  
    							   stuArray[i].grade[2])/3;
    		i++;
    		
    		if (inputChecker < 3)
    		{
    			puts("You entered incorrect data, please try again");
    			
    			 scanf("%g %g %g", stuArray[i].grade, /* expected - return control to stdin. actual - loops to beginning of while() */
    								 stuArray[i].grade+1,             
    								 stuArray[i].grade+2);
    		}
    	}
    	B_calculateEachStudentAverage(stuArray);
    	C_printStudentInfo(stuArray);
    	D_printClassAverage(stuArray);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So the "fun" part of scanf is this: if it doesn't like the input you give it, it doesn't throw it away. So the next scanf will see the exact same input that caused it to fail the first time, at which point it will fail again, but leave the input alone, so the next scanf will see the exact same input..........

    What you want to do, I feel confident saying, is to use fgets() to get the input, and then sscanf to do the input validation (on the assumption you are willing to throw away extra numbers if they type four, etc.).

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    sizeof(stuArray)

    stuArray is a pointer, do you know the sizeof pointer? It is 4 or 8

    you have to pass the array length as a second parameter
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Quote Originally Posted by tabstop View Post
    So the "fun" part of scanf is this: if it doesn't like the input you give it, it doesn't throw it away. So the next scanf will see the exact same input that caused it to fail the first time, at which point it will fail again, but leave the input alone, so the next scanf will see the exact same input..........

    What you want to do, I feel confident saying, is to use fgets() to get the input, and then sscanf to do the input validation (on the assumption you are willing to throw away extra numbers if they type four, etc.).
    Thanks! I'll give that a shot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble with getting input from files.
    By Matsuya in forum C++ Programming
    Replies: 4
    Last Post: 05-26-2009, 10:56 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. c++ strings. input validation
    By Crashgr in forum C++ Programming
    Replies: 2
    Last Post: 08-24-2004, 06:54 PM
  5. Trouble with receiving input
    By BellosX in forum C Programming
    Replies: 4
    Last Post: 09-20-2001, 11:58 PM

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