do{}while loop trouble

This is a discussion on do{}while loop trouble within the C Programming forums, part of the General Programming Boards category; Where did I go wrong with this code for my do while loop? Code: int main() { char response; do ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    32

    Cool do{}while loop trouble

    Where did I go wrong with this code for my do while loop?

    Code:
    int main()
    {
    	char response;	
    
    	do
    	{
    		/* My main code would be here */
    
    		printf("\n\n\nDo you wish to convert another number? (y) or (n): ");
    		scanf("%c", &response);
    
    		while(!(response == 'y' || response == 'Y' || response == 'n' || response == 'N'))
    		{
    			printf("\nPlease enter (y) or (n): ");
    			scanf("%c", &response);			
    		}
    	} while(response == 'y' || response == 'Y');
    
    	return 0;
    }
    The output when the program goes to the do while loop is:
    Code:
    Do you wish to convert another number? (y) or (n):
    Please enter (y) or (n):
    For some reason that I do not see the program skips the first scanf() and enters the while loop going instead to the second scanf(). What am I doing wrong?

    Also does C have and equal to the C++ ignore function? I would normally code the response input as:
    Code:
    cin >> response;
    cin.ignore(100, '\n');
    I do not know if C has something similar to this for garbage clean-up.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, it doesn't have a cleanup method. You can do one of a few things:

    1) Use fgets, read into a buffer, and get it from there.
    2) Use something like:
    Code:
    void clean( void )
    {
        int c;
        while( (c=fgetc()) != '\n' && c != EOF )
            ;
    }
    3)You can modify parameters to scanf to ignore the newline.
    Code:
    scanf("%c*[^\n]*%c", &response );
    There are a few others, but those should do.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282
    Originally posted by quzah
    3)You can modify parameters to scanf to ignore the newline.
    Code:
    scanf("%c*[^\n]*%c", &response );
    cool!! Is there a place where I can learn more about these parameters? (i.e. on the web/any book ?)

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    quzah

    I really like the C++ ignore...oh well. As for the parameter combo:
    Code:
    scanf("%c*[^\n]*%c", &response );
    I never would have thought that one up, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble with creating a loop to read certain number of inputs
    By import tuner650 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2008, 07:28 PM
  2. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  3. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  4. trouble with a for loop with an if statement nested in
    By phoenix-47 in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2005, 03:24 PM
  5. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 04:49 PM

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