Skip to main content

Section 3.14 Basic Input

The programs we have written so far are pretty predictable; they do the same thing every time they run. Most of the time, though, we want programs that take input from the user and respond accordingly.
There are many ways to get input, including keyboard input, mouse movements and button clicks, as well as more exotic mechanisms like voice control and retinal scanning. In this text we will consider only keyboard input.
In the header file iostream, C++ defines an object named cin (short for โ€œconsole inputโ€ that handles input in much the same way that cout handles output. To get an integer value from the user we can write:
cin >> x;
The >> operator causes the program to stop executing and wait for the user to type something. If the user types a valid integer, the program converts it into an integer value and stores it in x.

Note 3.14.1.

Make sure to use << for output and >> for input.. It can be useful to remember that the symbols point in the direction data is going. In cout << x the value of x is going to the console. In cin >> x a value is going from the console to the variable x .
Here is a simple program that tells a person their age in โ€œdog yearsโ€ (it is often said that one human year is like 7 dog years):
Listing 3.14.1.
The prompt is a message displayed to the user before input is requested. It is not part of the input statement, but it is an important part of the program. It tells the user that it is time to type something and what it is they should type.
Programs in this book are not run on your computer - they are sent to a server to be run. So when you run a program in this book, you must provide all the input you want to give the program before running it. You can do this by typing the input into the textbox below it.
If you run the program on your own computer, the importance of the prompt will be more obvious. First you will see the prompt message, then the program will pause and let you type whatever you like. It will not resume running until you press Enter.
The >> operator stops reading when it hits a space or input that does not make sense given the type of data being read. The program above is trying to read an integer. That means only the characters 0-9 make sense to read. If you type 21 15 and then press Enter, only the 21 is read (it stops at the space). If you type 21.5, the program will stop reading at the . (which does not make sense as part of an integer) and only the 21 will be read. Bad input will not be skipped to reach good input. If you enter something like twenty 8, the >> operator will see the t and immediately give up.

Warning 3.14.2.

Input with cin is very brittle. A program designed for long-term use or use by non-programmers should be robust. If there is an issue with the input, the program should try to catch the problem, alert the user, and allow them to correct it. We will learn about tools to do that later. For now, we will assume (dangerously) the user never makes a mistake or tries to break the program with bad input.
You can get as many inputs as you like in a program. This program will read in four values and print their sum:
Listing 3.14.2.
Note that reading in two value in one statement (like num1 and num2) is the reading them each in different statements (as is done for 3 and 4). The program would behave the same if we split line 10 into two lines.
Also note that it does not matter that the inputs are all on one line, as long as they are separated by spaces. cin >> num1 reads the first number, and then stops at the space after it. The next use of >> from cin picks up where the last >> operation ended. If we wanted, we could place the inputs each on their own line. Try modifying the input by hitting enter after each number and then rerun the program. The behavior should not change.

Insight 3.14.3.

The >> operator looks like a โ€œfast forwardโ€ button. Think of part of >>โ€™s job as being to โ€œfast forwardโ€ past any spaces or newlines before trying to read in the next value.
Donโ€™t worry too much about all the possible ways your programโ€™s input could be formatted, for now you will always be given very clear instructions on how to get any needed inputs.

Checkpoint 3.14.1.

You have attempted of activities on this page.