Thread: help with infinite loop please

  1. #1
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77

    help with infinite loop please

    I cannot see why this should not work - it just stuck in the 'while' loop. Any suggestions? I'm sure I'll feel stupid....

    Code:
        char response[10];
    
        printf("\nType, 1,2 or 3:   ");
        fgets(response, 10, stdin);
    
        while (response[0] != '1' || response[0] != '2' || response[0] != '3' )
        {
            printf("\nResponse = ..%s.. \nTry again:  ", response);
            fgets(response, 10, stdin);    
        }
        
        char c = response[0];
        
        if (c == '1')
        {
            printf("\nSelection 1");
        }
        else if (c=='2')
        {
            printf("\nSelection 2");
        }
        else if (c == '3')
        {
            printf("\nSelection 3");
        }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    response[0] != '1' || response[0] != '2'
    Think about it a bit: If response[0] is '1', then what's the result of the whole expression?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    Thanks for that, I didn't realise what I was asking it

    So, if I type '1', expression evaluates to
    (0 || 1 || 1) == true

    and all other combinations will always yield 'true'

    Back to the drawing board then!
    Thanks again

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and now that you have realized what is wrong, you may want to consider the use of AND instead of OR. I just didn't want to plate up the solution without you thinking about it, because if I do that, you'd just fix the code and not think about the problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    You're right, the question was very helpful
    I've been pondering the solution all afternoon.. and came up with:

    while ( !(response[0] == '1' || response[0] == '2' || response[0] == '3') )

    but I see '&&' is the leaner solution

    Another small step on the learning curve....
    I appreciate the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM