How to Read Text Input
-
One of the toughest problems in reading input, either from the keyboard or from an input file stream,
arises when the input mixes numerical and text data in some not very well-defined fashion.
In the worst situation, one may need to read everything one character at a time and process the information using
many tricks and techniques, such as atoi, to extract and unravel the data. (This topic was covered previously.)
One may even want to put what one has read back into the stream, etc. (This we will not covered here.)
We will not deal with these complicated situations here.
-
The easiest problem is to read a sequence of numbers (all integers or floating points) separated by white space
(a blank, a tab, or a newline character) but with no other characters.
One can simply declare variables of the correct type for the numbers, and read the numbers in from the appropriate
input stream with the insertion operator >>.
For example,
int num1, num2, num3;
cin >> num1 >> num2 >> num3;
-
When trying to read text from input data that contain only text separated by white space,
one has to make an important decision.
Is the input structured as a sequence of characters, words, or lines?
A word is defined here as a sequence of characters separated by white space.
A line is a sequence of characters separated by spaces and tabs that ends with a newline character.
-
In a program that counts or analyzes the words in a file,
one wants to ignore the whitespace characters. The easiest way is to use
input stream with the insertion operator >> as shown below:
string word;
while (cin >> word)
{
PROCESS word
}
Here one takes advantage of the fact that the expression
cin >> word
has a boolean value
that can be used to test to see if it is in a fail state or not.
-
However sometimes it does not make sense to process input a word at a time. For example,
we may want to input a sequence of student names, each on a separate line
King, Martin Luther, Jr.
King, Larry
...
In this case one wants to process the input one line at a time using the getline function.
string line;
while (getline(cin, line))
{
PROCESS line
}
Again this loop takes advantage of the fact that the getline function returns cin,
and therefore one can test that cin has not yet failed.
-
In case neither the words nor line boundaries are meaningful for processing,
one can read the input one character at a time. For example:
char ch;
while (cin.get(ch))
{
PROCESS ch
}
For example, the following loop counts the number of sentences typed from the keyboard
(assuming that a sentence ends in a period, an exclamation point of a question mark)
int numberSentences(0);
char ch;
while (cin.get(ch))
{
if ( ch == '.' || ch == '!' || ch == '?' )
numberSentences++
}
-
Note that in all of the above examples, one is reading input from cin.
If one wants to read from a file, all one needs to do is to replace cin by the name of the input file stream object that has been declared and connected to the file in question by using its open member function.