So I've been pulling my hair out about this since yesterday, I've checked multiple posts on here trying to figure this out, basically, I'm trying to read 1 or more lines from standard input into a string variable then use an istringstream to get integer values. This is what i have:
string line;
int num;
while(getline(cin, line)){
istringstream data(line);
while(data >> num){
do stuff...
}
}
However, the outer loop never exits, if there is no input in the standard input it just sits there waiting and never actually exits the loop, so the program basically pauses until something is entered, and then just continues the loop once more. Can someone tell me why getline doesn't just cause an exit condition when there is nothing on stdin, and can someone help me to fix this issue, your help will be greatly appreciated.
if there is no input in the standard input it just sits there waiting and never actually exits the loop, so the program basically pauses until something is entered, and then just continues the loop once more. Can someone tell me why getline doesn't just cause an exit condition when there is nothing on stdin
It just behaves as expected. What is "nothing on stdin" actually? Did you mean an empty input? In that case you might want to change your loop condition to
while(getline(cin, line) && !line.empty()){
Also as mentioned in the comments CTRL-Z or CTRL-D (depends on OS) followed with ENTER input may end the loop.