cin

Am i correct in saying that you can only use cin when inputting from a key board.

as in when you say:


cin >> x;


you are essentially calling the input operator >> (which is a function) with the cin object, which ONLY takes it's input from the keyboard?
Yes and no. Someone else can elaborate, but for all intensive purposes, whatever characters are typed into the console are passed into the variable. There is some magic that happens behind the scenes that if you don't know exactly what's going on can cause you to ask questions.

One specific incident is inputting strings. A string can contain a space, but typing a space in cin will move the input to the next variable. There is ways around this like getline().

There is also other things to understand. Cin is standard, meaning that it accepts any standard characters, whereas a keyboard has many nonstandard keys on it including F buttons, arrow keys, alt, etc.

So again, yes and no.
Last edited on
Cin is the standard input, and is defined from the command prompt. I would imagine the "compile and run" button of most IDE's default to the standard input being from the keyboard.

~I guess it's not too much of a stretch to say that if you don't kow the answer you use Windows and an IDE~

Here's a quick program:
1
2
3
4
5
6
7
8
9
#include <iostream>

int main(void)
{
	char str[256];
	std::cin.get(str, 255); 
	std::cout << str;
	return 0;
}

Get it? :)

Say the project is named "fun", so you compile it and you get fun.exe.

Enter the command prompt and find the folder that fun.exe is located. If you type "fun" and hit enter, your program uses the standard input/output, which has defaulted to the keyboard and console.

Type "fun >fun.txt" and your standard output becomes the file fun.txt (which is overwritten if it exists). In that case, standard input was still the keyboard.

Type "fun <fun.txt" and, if fun.txt exists, the input will come from fun.txt, and it's contents will go to the console.

"fun <fun.txt >copyoffun.txt" would make a copy of fun (at least, the first 255 characters of the first line).

Last edited on
cin is a dynasty of Chinese imperators.:)
you are essentially calling the input operator >> (which is a function) with the cin object, which ONLY takes it's input from the keyboard?


You got it half right, until "ONLY". It takes input from Standard Input, which is usually set by the Operating System as the keyboard, but can be changed by the user. (LowestOne's post shows how to do this.)
Ok thanks.
Topic archived. No new replies allowed.