How can I terminate an input expression? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can I terminate an input expression?

Example, if I have a while loop that starts with while(cin)...

11th Jul 2017, 10:08 PM
jigoku_tenshi
3 Answers
+ 1
That wont work in code playground due to to the nature of sending code to a remote server you can't send input more than once. however put an if clause in that breaks out of the while loop in the body or if its a pre set number of iterations use for(int i = 0, i < 10, i++) { cin... (more code) } alternatively while(n == null) { n=cin; } syntax may be slightly off :) hope this helps
11th Jul 2017, 10:19 PM
Steven Kennedy
Steven Kennedy - avatar
0
But I've written a code from Stroustrup's book that includes a while (cin) to fill a vector. The book doesn't explain well, but once I run it, I write a series of numbers (pressing enter after every number) and I don't know how to terminate that for to fill the vector and proceed with the rest of the code
11th Jul 2017, 11:42 PM
jigoku_tenshi
0
If you consider if from the perspective that a while loop is going to repeat until the arguement returns false you then need to consider in what circumstance does cin return false. So to terminate in the following code while(cin) { cin >> y; //code } this will continue while all previous operations on cin have been successful, on each iteration the value will be piped to y if cin has been successful or not a better way to write the code would be while(cin >> y) { //some code } however in summary the answer is that the loop will continue while valid data is passed to cin. In the latter is y is defined as an int it will exit when cin is a double for example in the former example it will try to assign the double to y then exit.
12th Jul 2017, 10:27 AM
Steven Kennedy
Steven Kennedy - avatar