Thread: Do not understand this infinite loop and why it'z used

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    13

    Do not understand this infinite loop and why it'z used

    Can someone please explain to me in laymanz termz what'z going on with this snippet of code? I understand it'z a function used to get the first character then discard the remaining input but i am confused with the last while loop. When i googled it i found very complicated explanationz which went way over my head. There was one explanation that kind of made sense but it was an assembly code explanation dealing with the input being on the stack in memory and then my pea brain went numb. 😋
    Code:
        int get_First(void)
        {
             int ch, garbage;
    
    
             do
             {
                  ch = getchar();
             }while(isspace(ch));
    
    
            while((garbage = getchar()) != '\n' && garbage != EOF)
                        ;
        return ch;
        }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mad_hatter69
    I understand it'z a function used to get the first character then discard the remaining input but i am confused with the last while loop.
    Not quite: it is used to read from standard input and return the first non-whitespace character, or EOF if no such character is in the input. That's the point of controlling the do while loop with the call to isspace rather than just making a single call to getchar() without the loop.

    The second loop discards the remaining input on the line entered (hence the check for '\n'), or all the remaining input (hence the check for EOF).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    13
    Thank you @laserlight... much appreciated!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop in insertNode function is an infinite loop
    By blongnec in forum C Programming
    Replies: 8
    Last Post: 03-19-2016, 09:57 PM
  2. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  3. 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
  4. HELP! infinite loop
    By _JjC:: in forum C Programming
    Replies: 9
    Last Post: 03-02-2003, 03:31 PM
  5. Infinite loop?
    By ER in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2001, 05:47 PM

Tags for this Thread