Inserting to stdin?

Dec 26, 2018 at 12:28pm
Is there anyway that I can insert to stdin?

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.

Dec 26, 2018 at 12:54pm
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).
Dec 26, 2018 at 1:02pm
Thanks a lot I didn't know that!

(but still want to know if inserting to stdin is possible)
Last edited on Dec 26, 2018 at 1:14pm
Dec 29, 2018 at 1:39am
1
2
3
4
5
6
7
8
9
10
11
12
13

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?
Last edited on Dec 29, 2018 at 1:42am
Dec 29, 2018 at 2:37am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <limits>

int main()
{
    constexpr auto IgnoreLimit = std::numeric_limits<std::streamsize>::max();
    std::cin >> std::noskipws;
    for (char ch; std::cin >> ch && ch != '\n'; )
    {
        if (ch == ' ' || ch == '\t') continue; // skip spaces and tabs
        std::cout << '[' << ch << "]\n";
        std::cin.ignore(IgnoreLimit, '\n'); // eat rest of line (incl. newline)
    }
}

Dec 29, 2018 at 7:29am
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.

Too much to ask for?

Also,

1
2
3
4
5
6
	cin >> noskipws;
	
	string var;
	
	cin>> var;
	cout << "[" << var << "]";


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.

Hmm can't noskipws on cin?

Dec 29, 2018 at 3:26pm
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.
Dec 29, 2018 at 7:06pm
the line - while(cin >> var) {...}
would be an infinite loop unless cin goes into fail state.

In most cases a simple exit value can do the trick:
1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
    int num;
    do {
        std::cout << "Please a number; enter 0 to exit: ";
    } while(std::cin >> num && num !=0);
}

Or you can break after having evaluated the input…
Honestly, it doesn’t look such a drama.
Dec 30, 2018 at 2:21am
Enoizat, here the exit value is whitespace :-p

Thanks dhayden
Last edited on Dec 30, 2018 at 2:22am
Topic archived. No new replies allowed.