Thread: Logical Operator not working?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    Post Logical Operator not working?

    For some reason, the below code will not work (ie, the loop will not break out if either a 'Q' or 'q' is entered for line).

    Code:
    numelms = 81;
    char line[numelms];
    
    while (*line != 'Q' || * line != 'q')
    {
    cin.getline(line, numelms);
    }
    Help!

  2. #2
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    [edit]
    oops nevermind
    [/edit]

    nevermind just change it to and
    try
    change to &&
    Code:
    while(*line!='Q'&& *line !='q')
    [explanation]

    the test you have states that
    it will be true if either
    *line!='Q'
    OR
    *line!='q'

    problem is that when you enter a Q or q one is true and the other is false
    1 OR 0 =1; so it alwasy evaluates to be true;

    if you change it to AND 1 AND 0 is 0 and the loop exits;
    [/explanation]
    Last edited by manofsteel972; 09-27-2004 at 05:22 AM.
    "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/

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    28
    Man, I can't believe I didn't see that... I did this kind of loop all the time and Java... it's too early to be programming man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM