Elementary Programming in C++: Reading Input from the Keyboard

Reading input from the keyboard enables the program to accept input from the user.

In Listing 2.1, the radius is fixed in the source code. To use a different radius, you have to modify the source code and recompile it. Obviously, this is not convenient. You can use the cin object to read input from the keyboard, as shown in Listing 2.2.

Listing 2.2 ComputeAreaWithConsoleInput.cpp

1 #include <iostream>

2 using namespace std;


4 int main()

5 {

6    // Step 1: Read in radius

7    double radius;

8    cout << “Enter a radius: “;

9    cin >> radius;


11   // Step 2: Compute area

12   double area = radius * radius * 3.14159;


14   // Step 3: Display the area

15   cout << “The area is ” << area << endl;


17   return 0;

18 }

Line 8 displays a string “Enter a radius: ” to the console. This is known as a prompt because it directs the user to input something. Your program should always tell the user what to enter when expecting input from the keyboard.

Line 9 uses the cin object to read a value from the keyboard.

Note that cin (pronounced see-in) stands for console input. The >> symbol, referred to as the stream extraction operator, assigns an input to a variable. As shown in the sample run, the program displays the prompting message “Enter a radius: “; the user then enters number 2, which is assigned to variable radius. The cin object causes a program to wait until data is entered at the keyboard and the Enter key is pressed. C++ automatically converts the data read from the keyboard to the data type of the variable.

cin >> variable; // cin —> variable;

cout << “Welcome “; // cout <— “Welcome”;

You can use a single statement to read multiple input. For example, the following state­ment reads three values into variable x1, x2, and x3:

Listing 2.3 gives an example of reading multiple input from the keyboard. The example reads three numbers and displays their average.

Listing 2.3  ComputeAverage.cpp

1 #include <iostream>

2 using namespace std;


4 int main()

5 {

6    // Prompt the user to enter three numbers

7    double number1, number2, number3;

8    cout << “Enter three numbers: “;

9    cin >> number1 >> number2 >> number3;


11    // Compute average

12    double average = (number1 + number2 + number3) / 3;


14    // Display result

15    cout << “The average of ” << number1 << ” ” << number2

16    << ” ” << number3 << ” is ” << average << endl;


18    return 0;

19 }

Line 8 prompts the user to enter three numbers. The numbers are read in line 9. You may enter three numbers separated by spaces, then press the Enter key, or enter each number fol­lowed by the Enter key, as shown in the sample run of this program.


Most of the programs in the early chapters of this book perform three steps: input, proc­ess, and output, called IPO. Input is receiving input from the user; process is producing results using the input; and output is displaying the results.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *