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:

123456789

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.

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.

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.

@ 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!