Section 5 - cin with String and Numbers
3A.5.1 Strings and cin
There is an alternative to string input called cin. Cin is the input counterpart to the cout output statement. It works like this:
// ask the question and get the answer cout << "What's your name? "; cin >> strUserAnswer; cout << "Hello " + strUserAnswer + "\n";
There are three important things to consider when deciding whether to use getline() or cin:
- cin can be used to read numeric strings like "453.02" directly into numeric variables as we shall see next. getline() always reads the input into a string variable.
- cin only reads one non-blank string at a time. If you type in three words separated by spaces, you will have to use cin three times to get all of the information.
- cin can be used to receive several words at once, but you have to use distinct variables, as in cin >> str1 >> str2 >> str3; Each variable will get the next item the user types with no spaces in it.
Here is an example that demonstrates the second point. We run the modified program using cin, as shown above, and get the following result:

Notice how everything after the first space in my input string was ignored. The words are still there, however. Consider this variation:
cout << "What's your name? "; cin >> strUserAnswer; cout << "First word " + strUserAnswer + "\n"; cin >> strUserAnswer; cout << "Second word " + strUserAnswer + "\n"; cin >> strUserAnswer; cout << "Third word " + strUserAnswer + "\n";

Notice how the program seems to receive everything from the user in the first cin statement, but actually does not place the words into our string objects until the latter cin statements are reached. Also note that the latter cin statements do not block the program (i.e., waiting for user input) since the input is already waiting for us, left over from the first cin statement.
And there is the following variation, that demonstrates the third point above, if we want to use several string variables instead of just one:
// Declare string references to hold user's answers string str1, str2, str3; // ask the question and get the answer cout << "What's your name? "; cin >> str1 >> str2 >> str3; cout << "First word " + str1 + "\n"; cout << "Second word " + str2 + "\n"; cout << "Third word " + str3 + "\n";
What do you think happens when we run this one?
3A.5.2 Numeric Types and cin
Because cin can convert string input from the user's keyboard into numeric data, it is very useful for simple number crunching. Here's an example:
#include <iostream> using namespace std; int main() { double x, y, z, avg; cout << "Enter 3 numbers and I'll tell you their average: "; cin >> x >> y >> z; avg = (x + y + z) / 3.; cout << "The average of " << x << ", " << y << " and " << z << " is \n (drumroll, please) ... " << avg << endl; return (0); }
The output is:
Enter 3 numbers and I'll tell you their average: 20 50 99.3 The average of 20, 50 and 99.3 is (drumroll, please) ... 56.4333 Press any key to continue . . .
As you can see, this program asks the user for three floating point numbers and cins them into the three variables x, y and z. Note that the syntax for cin is almost identical to cout,
cin >> x >> y >> z;
but, as I said, you use the >> operator, called the extraction operator. The operator that cout uses, <<, is called the insertion operator. The items that appear interleaved between the extraction operator are collectively referred to as the input stream (analogous to cout's output stream). It is an error to place a string literal or constant into the source code's input stream. It is also an error to use << with cin or >> with cout. Here is an example of these errors:
cin >> "Can't do this!!"; // ERROR!! cin << x; // ERROR!! cout >> y; // ERROR!!
3A.5.3 Numeric vs. String Input
The distinction between reading user input as string data and reading it as numeric data is very important. Be sure you understand it. While it is true that you can enter both numeric and character information into a string ("dog 123 -99xyz"), the numerals that are entered there are not computable. You cannot compute with the string "123". The number 123 when stored in an int or long is computable. So if you want to do computations you have to get the data entered by the user into a numeric variable such as long, float, double etc.
As an alternative, you can receive input and store it into a string object, then later convert that into a number that can be stored in numeric variable. This, however, is a technique you will not learn for a few weeks.
3A.5.4 Making a Choice of Input Modality
Rule of Thumb
When writing console programs, you will find that you can get input either through invocation of getline( ... ) or directly using cin >> ... . In general, getline(cin, someVar) and cin >> someVar don't mix. Decide which method of console input you are going to use, and stick with that throughout your program.
Caveat:
This page notwithstanding, you should still only use getline() in all of your labs in this course. Reading directly from cin using "cin >> var;" is beset with all kinds of problems (discussed separately in the FAQ and announcements. You would only read directly from a stream into a var when you know for sure that the stream contains data in exactly the form you expect
If you fail to obey this guideline, you will probably notice very odd "run-away" behavior when you run your program. You might attempt to type in one value when prompted by your program only to find that as soon as you hit the enter key the program skips the input for the next value, or worse, enters an infinite loop, printing wildly. There is a technical reason relating to something called input buffering that accounts for this effect, as well as a technical solution. However, it is best to avoid the problem altogether and not mix these two forms of input. Select one form and use it throughout your program.