When you specify the iostream.h header file as a source file for your project, four streams are automatically defined for I/O use: cin, cout, cerr, and clog. The cin stream is the standard input stream. Input to cin comes from the C standard input stream, stdin, unless cin has been redirected by the user. The remaining streams can be used for output. You can receive standard input using the predefined input stream and the input operator (operator>>) for the type being read.
An input operator must exist for the type being read in. The I/O Stream Class Library defines input operators for all C++ built-in types. For types you define yourself, you need to provide your own input operators. If you attempt to read input into a variable and no input operator is defined for the type of that variable, the compiler displays an error message with text similar to the following:
Call does not match any parameter list for "operator>>".
You can use the same techniques for input from other input streams as for input from cin. The only difference is that, for other input streams, your program must define the stream.
You can receive input from a stream into a succession of variables with a single input statement, by repeating the input operator (>>) after each input, and then specifying the next variable to read in. You can combine variables of multiple types in an input statement, without having to specify the types of those variables in the input statement:
If you want to read input into a character array (a string), you should declare the character array using array notation, with a length large enough to hold the largest string being entered. If you declare the character array using pointer notation, you must allocate storage to the pointer, for example by using new or malloc. The following example shows a correct and an incorrect way of placing input in a character array:
char goodText[40]; char* badText; cin >> goodText; // works as long as input is less than 40 chars cin >> badText; // may cause a runtime error because no storage // is allocated to *badText
In the above example, the input to badText can be made to work by inserting the following code before the input:
badText=new char[40];
This guideline applies to input to any pointer-to-type. Storage must be allocated to the pointer before input occurs.
The input operator uses white space to delineate items in the input stream, including strings. If you want an entire line of input to be read in as a single string, you should use the getline() function of istream:
// String input using operator << and getline()
#include <iostream.h>
void main() { char text1[100], text2[100];
// prompt and get input for text arrays cout << "Enter two words:\n"; cin >> text1 >> text2;
// display the text arrays cout << "<" << text1 << ">\n" << "<" << text2 << ">\n" << "Enter two lines of text:\n";
// ignore the next character if it is a newline if (cin.peek()=='\n') cin.ignore(1,'\n');
// get a line of text into array text1 cin.getline(text1, sizeof(text1), '\n');
// get a line of text into array text2 cin.getline(text2, sizeof(text2), '\n');
// display the text arrays cout << "<" << text1 << ">\n" << "<" << text2 << ">" << endl; }
The first argument of getline() is a pointer to the character array in which to store the input. The second argument specifies the maximum number of bytes of input to read and the third argument is the delimiter, which the library uses to determine when the string input is complete. If you do not specify a delimiter, the default is the new-line character.
Here are two samples of the input and output from this program. Input is shown in bold type, and output is shown in regular type:
Enter two words: Word1 Word2 <Word1> <Word2> Enter two lines of text: First line of text Second line of text <First line of text> <Second line of text>
For the above input, the program works as expected. For the input in the sample below, the first input statement reads two white-space-delimited words from the first line. The check for a new-line character does not find one at the next position (because the next character in the input stream is the space following "happens"), so the first getline() call reads in the remainder of the first line of input. The second line of input is read by the second getline() call, and the program ends before any further input can be read.
Enter two words: What happens if I enter more words than it asks for? <What> <happens> Enter two lines of text: I suppose it will skip over the extra ones <if I enter more words than it asks for?> <I suppose it will skip over the extra ones>
When your program requests input through the input operator and the input provided is incorrect or of the wrong type, the error state may be set in the input stream and further input from that input stream may fail. One runtime symptom of such a failure is that your program's prompts for further input display without pausing to wait for the input.
Correcting Input
Stream Errors
Opening a File for
Input and Reading From the File
Displaying Output on
Standard Output or Standard Error
Defining an Input
Operator for a Class Type