Clueless for a portion of code.

This is a discussion on Clueless for a portion of code. within the C Programming forums, part of the General Programming Boards category; It's a portion of code for a way to track DVDs which includes the title, comment and the rating. The ...

  1. #1
    Registered User shazwi's Avatar
    Join Date
    May 2010
    Posts
    4

    Clueless for a portion of code.

    It's a portion of code for a way to track DVDs which includes the title, comment and the rating. The one I'm wondering is the rating part.

    Code:
    do {
    		num = 0;
            printf( "Enter DVD Rating (1-10):  " );
    		scanf( "%d", &num );
    		Flush();
    	}
    	while ( ( num < 1 ) || ( num > 10 ) );
    
    void	Flush( void )
    {
    	while ( getchar() != '\n' )
    		;
    }
    Without Flush(), if I do not enter a number, and I hit enter, it goes through an endless loop repeating "Enter DVD Rating". But with the flush, it will prompt again till it gets the number.

    I don't get how this Flush() function prevents the infinite loop and keeps prompting for the number.

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    scanf() leaves everything it cannot assign to a variable in the input buffer.

    Suppose user enters A. scanf() will fail to assign that to num (mismatched type?) and leave it in the buffer. On the next iteration of the loop, that A (okay, "A\n") is still in the buffer and scanf() will fail to assign it to num yet again, and so on and so forth, ad nauseum.

    Flush() simple retrieves characters from the buffer until the character retrieved is a newline. The effect is that you get a clear buffer. But it can fail if there nothing in the buffer when you call it.

    The do-while loop is not the best way to fetch input. Consider:
    Code:
    printf("Enter DVD rating (1 - 10): ");
    while ( scanf("%d", &num) != 1 || (num < 1 || num > 10)) {
      printf("Enter DVD rating: ");
      Flush();
    }
    Last edited by msh; 05-28-2010 at 03:29 AM.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Nice condition for the loop...... NOT .
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by claudiu View Post
    Nice condition for the loop...... NOT .
    Mine?

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I want to enter EOF.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by msh View Post
    Mine?
    No, the OP's. It is doing the exact opposite of what I suspect he is trying to do. Hence the Borat "NOT" joke

    EDIT: Nevermind, I'm an idiot. I should remember to sleep at least a few hours before posting advice.
    Last edited by claudiu; 05-28-2010 at 07:40 AM.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 08:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

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