Loop problem: Asks a question twice!!!

This is a discussion on Loop problem: Asks a question twice!!! within the C Programming forums, part of the General Programming Boards category; Hi everyone, could someone please tell me WHY THE 'Are you sure you want to quit?' question gets asked twice, ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    24

    Loop problem: Asks a question twice!!!

    Hi everyone,
    could someone please tell me WHY THE 'Are you sure you want to quit?' question gets asked twice, I've trid to put every possible condition on it but no matter what it is asked twice:
    Code:
    case 3:
    				fflush(stdin);
    				do
    				{
    			       puts("Are you sure you want to quit ?");
    					ch = getc(stdin);
    					if (ch == 'y')
    					{
    						printf("Thank you for using\n\n");
    						exit(0);
    					}
    					else if (ch == 'n')
    					{
    						puts("That's great!!!");
    						break;
    					}
    				}while(ch != 'n' || ch != 'y');
    				break;
    }
    this is the third 'case' of a switch statement.
    Any help would be appreciated.
    Thanx
    Rhodium

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    To extend on Salem's educated guess....

    Let me guess that this:
    >>case 3:
    is a result of doing something like:

    Code:
    printf ("Enter your menu option:");
    scanf("%d", &option);
    switch (option)
    {
      ...
       case 3:
       ...
    }
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    161
    Also, this will never be false:

    Code:
    while(ch != 'n' || ch != 'y')
    Try using && instead.

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Originally posted by thefroggy
    Also, this will never be false:

    Code:
    while(ch != 'n' || ch != 'y')
    Try using && instead.
    --edited out previous reply--

    It wont matter. they will both exit if he enters invalid data. And if it is correct data, it will exit, or break.
    Last edited by stumon; 07-03-2003 at 11:41 AM.
    The keyboard is the standard device used to cause computer errors!

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    24

    You are right.

    Originally posted by Hammer
    To extend on Salem's educated guess....

    Let me guess that this:
    >>case 3:
    is a result of doing something like:

    Code:
    printf ("Enter your menu option:");
    scanf("%d", &option);
    switch (option)
    {
      ...
       case 3:
       ...
    }
    Yes Hammer, you are right case 3 is a menu option.
    For everyone who is confused, this is the output I get when I choose option 3.
    Code:
    3 enter
    Are you sure you want to quit?
    Are you sure you want to quit?
    waits for input
    I think the rest of it works fine but I don't remember exactly. I'll see if there's any more errors on the output when I go back to Linux.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    I see a lot of guesses, assumptions, and confusion based on what people think you are doing to get to this "case 3:". Because of that, might it not be best to post all the code that get's you into this case?

    And, if Hammer is correct, search the FAQ for info on scanf() because that's most likely your problem. Why it's a problem is answered on this board 2-4 times a week, and in the FAQ. A quick search gives you your answer quicker, and we don't have to explain the problem for the 234th time.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 12:08 PM
  2. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 06:19 AM
  3. do - while loop problem
    By estranged in forum C Programming
    Replies: 8
    Last Post: 12-10-2003, 06:11 PM
  4. do while loop problem
    By nelinda in forum C Programming
    Replies: 1
    Last Post: 11-30-2003, 08:29 AM
  5. Problem with while loop
    By Fyodorox in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2002, 11:02 PM

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