Alternate ways to break a WHILE loop

This is from a homework assignment I already turned in. My programmed worked flawlessly but I was frustrated by one thing. We were working on linked lists and I wanted the user to be able to enter as many inputs are they wanted. I am using integers for the input. I used a while loop to capture the input. My code:

1
2
3
4
5
6
7
8
9
cout << "Input numbers to form a list.  You may input as many as you want.\n"
     << "Type -999 when you are finished entering your list." << endl;
cin >> num;

while (num != -999)
{
     list1.insert(num);
     cin >> num;
}


For future reference, is there a way to break the loop or use a different loop to accept indefinite input? This worked it and it met my requirements but it felt clumsy to use -999 (or any end trigger). I was hoping to use return (\n) somehow to end it.
while ( std::cin >> num ) list.insert( num );
The >> operator ignores white space, and then reads the expected format (int/string/etc) until it hits white space.

You can use cin.peek() to look at whatever is next in the buffer:
1
2
3
int x;
while (cin.peek() == ' ' && cin >> x)
  list.insert(x);


I thank my intro teacher every day for being a PIA about buffers.
LowestOne - Thanks! I will use that! That makes sense to me. "As long as there is a space or an int, keep using the function." Learned peek in teh first week but I never really utilize it. Thanks.

vlad - Not sure how that one will work. Will that still ignore spaces in between numbers? I will have to play with that one.
closed account (3qX21hU5)
Another way to break out of a loops is the break; statement which can be used with if conditions. Like if the input is equal to a certain word, or your calculation hits a certain number.

1
2
3
4
5
6
while (true)
{
    // Do some calculation
    if (you want to exit)
        break;
}


or with your above code

1
2
3
4
5
6
7
8
9
10
11
12
cout << "Input numbers to form a list.  You may input as many as you want.\n"
     << "Type -999 when you are finished entering your list." << endl;
cin >> num;

while (true)
{
     list1.insert(num);
     cin >> num;
     
     if (num == -999)
         break;
}


another useful command while in loops is the continue; statement which tells the loop to skip whatever is left of the loop and start over at the top of the loop.
Zereo - Thanks. I don't think that is accomplishing what I want though. I played with break a bit. The goal I was trying to accomplish was not entering in a awkward trigger like "-999" or "x" or anything like that. I want to use something like the enter key to stop the while loop. I think LowestOne had the best solution using cin.peek to look for a space and the condition being cin.peek = " " or a valid cin for int. I haven't applied that yet but I want to play with that so I can use it for future programs.

Can you think of a way to condition the break to look for '\n'? I like using break, looks clean in the coding and is easy to understand while debugging.
@pojster
vlad - Not sure how that one will work. Will that still ignore spaces in between numbers? I will have to play with that one.


Values will be entered until end of stream will be encountered or some error will occur. To generate the situation with end of stream you should enter Ctrl+Z in a Windows console applicarion or Ctrl+d in s Unix console application.
Last edited on
Vlad's version is the best version, it is simple, error-proof, and it works as intended. cin automatically interprets spaces and new lines as a delimiter.
Thanks guys! You've been really helpful! I know what to do now if I come across this again (which I know I will). I just hope I don't need to do linked lists anymore in this class!! I hate that you can't random access items within the list without writing some complicated function!
Linked lists aren't used when you need random access; there are cases where it is much easier and far more efficient to use a linked list. Obviously, though, an assignment involving random-access of a linked list is a poor assignment unless it aims to teach that you shouldn't use linked listswhen you need random access.

And, the good news, the C++ standard library has std::list which is a linked list all ready for you, and std::vector which is a random access list all ready for you.
Last edited on
@ L B - I am sure that was precisely the point of the exercise - to show the pitfalls of linked lists. Very easy to add and delete things from the list, difficult if you need to access from the list. Even our discussion questions asked us to explain what difficulties we encountered with accessing the list. It was very informative! But it's also the reason I am hoping we don't use them anymore this class!
Topic archived. No new replies allowed.