Why insert to stdin you ask?
the line - while(cin >> var) {...}
would be an infinite loop unless cin goes into fail state.
while(getline(cin, var)) {}
would also be an infinite loop.
But we can write: while(getline(cin, var) && var != "") {}
But we cannot apply the same logic to cin because cin ignores whitespaces.
So you can't end input by just clicking entering, you have to type a specific keyword.
Otherwise you would have to use getline and then parse that getline. But if we were just simply able to add something to the stream (later remove it) then it would be much simpler I thought.
the line - while(cin >> var) {...}
would be an infinite loop unless cin goes into fail state.
Not necessarily. std::cin can reach EOF. For example, if a file was passed as stdin (program < file.txt), or if the user hit the EOF key combination (CTRL+Z on Windows, CTRL+D on Linux).
fun times
int main()
{
cin.putback('x');
char c;
cin >> c;
cout << c;
}
just dig around in the documents.
what is cin? Its an istream.
what does an istream have? documentation says it has putback(char)
what does it say about it? It may not work, and can throw exceptions, but I didn't dig into why, I would guess you can't put back if the buffer is too full or some systems may prevent it?
Thanks a lot dutch. Wouldn't have ever known about noskipws if it weren't for you.
Problem is that your snippet assumes '\n' is used to terminate input. But if that were the case then I would just use getline(). Why I wanted to see whether I can use cin is because I want to read in words and do not want to have to read in lines and parse them to find words because that's a bit more complex.
So how to work around this? Need to terminate on whitespace characters, ignore all whitespace characters.
Goal is something like this: while(cin>>var && var is not empty) {}
We cannot just change dutch's snippet to terminate on any other whitespace character instead of '\n' because the while loop would stop on the first word itself.
But in this snippet above it seems var never contains trailing whitespaces and when there is any whitespace preceding, var gets '\0' null regardless of whether there are characters following the whitespace.
Use peek() to look at the next character. But to do this you have to manually skip the newline at the end of the current line. Suppose you want to read two numbers. Consider that the input file looks like this:
1\n
2\n
\n
When you do cin >> num, the file pointer is positioned at the newline right after the number. So you have to skip that one to get to the beginning of the next line. Then you can peek():
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<iostream>
using std::cin;
using std::cout;
int main()
{
int num;
while (cin.peek() != '\n' && cin >> num) {
cout << num << '\n';
cin.ignore(1000000, '\n'); // skip newline after number
}
}
Using 1000000 as the limit on ignore isn't technically correct. The right value is std::numeric_limits::max<std::streamsize>() but I didn't want to distract you from the problem at hand with that monstrous expression.