Thread: Can't exit a do/while loop - please help

  1. #1
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26

    Question Can't exit a do/while loop - please help

    Hello again,

    I was wondering if anyone might be able to tell me why the program won't exit the loop when response equals 'q' or 'Q'. When I select a Q or a q, the program just loops contiuously. Any help would be appreciated.

    Thanks
    Vireyda



    [CODE]
    do{
    printf("Please select a transaction:\n");
    printf("Cheque - C or c\nDeposit - D or d\nPrint - P or p\nQuit - Q or q\n");
    fflush(stdin);
    scanf("%c", &response);
    switch(response)
    {case'c':case'C':
    Cheque(balance);
    break;
    case 'd': case'D':
    Deposit(balance);
    break;
    case 'p': case'P':
    Print(balance);
    break;
    case 'q': case'Q':
    break;
    default:
    printf("That is an invalid selection. Please try again.\n ");
    }
    }while(response!='q'||response!='Q');
    [\CODE]

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    You want to quit the loop when
    (response == 'q') || (response == 'Q')

    That means you want to continue as long as

    (response != 'q') && (response != 'Q')

    Fundamental boolean logic.

    Try it.

    Dave

  3. #3
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    I once had this problem a while back, and i also posted it here. The answer i got was instead of using the OR ( || ) operator, i should have used the AND ( && ) operator. I sat down thinking how ilogical this sounds, and coudn't come up with an answer. It still annoys me today whenever i think about it ( So if anyone would like to answer this question, i would be gratfull).

    Anyways, use the && operator instead of the || operator in your test expression and it should work fine.

  4. #4
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    Woohoo! Listen to Evans! thanks Evans! answered my quetison too

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Look at the logic in your while condition

    Code:
    while(response!='q'||response!='Q');
    if you look at the logic of the statment

    (response!='q') is true(1) when any other letter is entered and false(0) when you enter 'q'

    (response!='Q') is true(1) when any other letter is entered and false(0) when you enter 'Q'

    one is always going to be true and the other false when you try to quit

    when you OR 1 and 0 you get 1 which is what you are doing so it evaluates to while(1) which is an infinite l oop

    when you AND 1 and 0 you get 0 which will break the loop and exit so just change the || to && and you should be alright.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  6. #6
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    ?? Wait....i still don't get it. Why won't
    response=='q' || response 'Q'
    exit the loop?

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    Originally posted by Dave Evans
    You want to quit the loop when
    (response == 'q') || (response == 'Q')

    That means you want to continue as long as

    (response != 'q') && (response != 'Q')

    Fundamental boolean logic.

    Try it.

    Dave
    I dont think it's a matter of boolean logic because it is not logic at all that he will write q and Q simultaneously

    but i think he wrote on his code
    Code:
    while(response!='q'||response!='Q');
    i suggest that he will replace it by
    Code:
    while((response!='q')||(response!='Q'));
    or
    Code:
    while(!((response=='q')||(response=='Q')));
    i am not sure anyway .. u might be right

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Originally posted by jrahhali
    ?? Wait....i still don't get it. Why won't
    response=='q' || response 'Q'
    exit the loop?
    Because the loop continues as long as the stuff in the parentheses is true. (Therefore the exit condition is when the stuff is not true.)

    Code:
    do {
    
    } while(ThisStuffIsTrue);

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Originally posted by M_Ghani
    I dont think it's a matter of boolean logic because it is not logic at all that he will write q and Q simultaneously

    but i think he wrote on his code
    Code:
    while(response!='q'||response!='Q');
    i suggest that he will replace it by
    Code:
    while((response!='q')||(response!='Q'));
    or
    Code:
    while(!((response=='q')||(response=='Q')));
    i am not sure anyway .. u might be right
    Well, let's simplify the notation:

    I didn't mean to confuse anyone with jargon, like "Boolean logic", but one the things you learn is (in C-language notation):

    The following is always true:


    !(x || y) == (!x && !y)

    It's not a "real" proof, but maybe you can convince yourself it's worth considering if you try to evaluate each with all possible values of x and y. You can do this in your head, on pencil and paper, or in a C program:

    x = false, y = false.
    x = false, y = true.
    x = true, y = false.
    x = true, y = true.

    This topic comes up all of the time. Once you "get it", it's yours for life.



    Regards,

    Dave

  10. #10
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    Originally posted by Dave Evans
    Well, let's simplify the notation:

    I didn't mean to confuse anyone with jargon, like "Boolean logic", but one the things you learn is (in C-language notation):

    The following is always true:


    !(x || y) == (!x && !y)

    It's not a "real" proof, but maybe you can convince yourself it's worth considering if you try to evaluate each with all possible values of x and y. You can do this in your head, on pencil and paper, or in a C program:

    x = false, y = false.
    x = false, y = true.
    x = true, y = false.
    x = true, y = true.

    This topic comes up all of the time. Once you "get it", it's yours for life.



    Regards,

    Dave
    yeah .. I got it

    so that means that
    Code:
    while(!((response=='q')||(response=='Q')));
    is correct and is the same as
    Code:
    while((response!='q')&&(response!='Q'));

  11. #11
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    Thanks everyone. Your help was invaluable. The program works for me now and is a big relief off my shoulders.

    Vireyda

  12. #12
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    Thankyou so much for the logic!! That IS logical when you use bool- it also helped me with a program!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  2. Stopping an Infinite Loop
    By linuxpyro in forum C Programming
    Replies: 4
    Last Post: 11-30-2006, 12:21 PM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  4. I can't figure out why this doesn't exit the loop, MAN!
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2002, 08:47 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM