funny-looking while loop

This is a discussion on funny-looking while loop within the C++ Programming forums, part of the General Programming Boards category; This is from Prata, C++ Primer Plus, 5th ed., p. 588. He's showing here how to implement a user-defined class ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    242

    funny-looking while loop

    This is from Prata, C++ Primer Plus, 5th ed., p. 588.

    He's showing here how to implement a user-defined class "String", which is supposed to be a functional but oversimplified version of the library string class. The private static const int CINLIM has been set to 80 in the header file. Here's Prata's implementation of >>, where the part I would definitely not thought of putting in is the while loop:

    Code:
    istream & operator>>(istream & is, String & st)
    {
      char temp[String::CINLIM];
      is.get(temp, String::CINLIM);
      if (is)
        st = temp;
      while (is && is.get() != '\n')
        continue;
      return is;
    }
    Ok, so the while loop runs if
    1) The input operation defined by "is" is successful, and
    2) The value of is.get() is not the newline character.

    On this implementation, if "is" fails, then >> just returns the failed "is" and the String st doesn't undergo any changes.

    And if "is" succeeds, then the String st gets set to the value entered, and then we get to the while loop. is.get() should have the value '\n' as long as the input didn't exceed 80 characters (right?), so the loop won't run in this case.

    But if "is" is cin, for example, and the user entered 120 characters before pressing enter, then cin.get() is going to have the value of some junk character (not captured in st or temp), and the loop should move the input location up to the point where '\n' was entered (?). So, is the function of this while loop just to clear out the junk characters from is.get()?

    If so, couldn't he just have substituted is.clear(); for the whole while loop? I'm assuming that if get() is a member function of all istream objects, then clear() also is.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    242
    Sorry, with regard to my last question, I just thought of the case of an input file, call it inFile, where inFile.clear() doesn't make much sense.

    So the remaining question is just whether I've understood correctly why he puts this loop there in the first place.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You understand it correctly.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    242
    tx again, Dave!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

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