Topic 3: Input & Output using Standard Devices

Getting Data from Keyboard

The cin object is used to get data from the keyboard. The program pauses while the user enters data. The extraction operator (>>) takes information out of the cin object and stores it in internal memory.

Syntax:
cin >> variableName;

Example: Getting a number from the user
  • int num;
    cout << "Please enter a number: ";
    cin >> num;  


A prompt (message) must be used to let the user know what data is to be entered.

Displaying the information to the Screen

The cout object sends data to a standard output display device. It is used with the insertion operator (<<) to display information on the screen. You can print any combination of literal constants, named constants, and variables.

Syntax:
cout << item1 [<< item2 << ... << itemN];

Example: Displaying "Hello, World!"
  • cout << "Hello, World!";


Figure 3.1 shows the complete program using cout statement to display Hello There world!

...
Figure 3.1

Using endl

A stream manipulator is used to manipulate (manage) the characters in an input or output string. endl is a stream manipulator that moves the cursor to the next line on the screen. It's the same as pressing the Enter key on the keyboard, as shown in Figure 3.2.

Figure 3.3 shows combining cout statements (the cout from line 7 and the semicolon at the end of line 6 from Figure 3.2 is removed). Note that both will have the same output.

...
Figure 3.2
...
Figure 3.3
Output:
  • Hello World!
    This is my first program.

Escape Sequences

Escape sequences are special non-printing characters that control the printing behavior of the output stream objects like cout.

Table 3.1: Escape Sequences
Escape Sequence Character Represented
\a Alert (bell, alarm)
\b Backspace
\f Form feed (new page)
\n New-line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\' Single quotation mark
\" Double quotation mark
\? Question mark
\\ Backslash

... Output: ...
Figure 3.4
... Output: ...
Figure 3.5

Formatting Numeric Output

Real numbers can be displayed in either fixed-point or scientific notation:

  • Fixed-point notation for small numbers (six or fewer digits before the decimal point)
    1. Example: 1,234.56 displayed as 1234.560000
  • Scientific notation for large numbers (more than six digits before the decimal point)
    1. Example: 1,225,000.00 displayed as 1.225e+006

Stream manipulators allow control over formatting:

  • fixed displays numbers in fixed-point notation
  • scientific displays numbers in scientific notation

Stream manipulator must appear in a cout statement before the numbers you want formatted

Table 3.2: Formatting Examples
Example Result (Display)
double total = 20535.75;
cout << fixed << total << endl;
20535.750000
double conversion = 4.9018436;
cout << fixed << conversion << endl;
4.901844
double total = 20535.75;
cout << scientific << total << endl;
2.053575e+04

Practice Questions

  1. Explain the difference between cin and cout.
  2. What is the difference between the endl manipulator and the newline character \n?
  3. What are the benefits of using the setw() manipulator in C++ when printing data?
  4. What happens when you use cin to input a string with spaces? How can you handle such input correctly?
  5. What is the difference between cin and cin.get()? In what scenarios would you use cin.get()?
  6. What happens if the user inputs a value of the wrong data type (e.g., entering a string when cin expects an integer)? How can this be handled in a program?
  1. Write a C++ program that uses escape sequences to print the following output:
    Hello, World!
    Welcome to C++ programming. 
  2. Write a program that displays the inventory of books in a library. The program should display information about Title, Author, Genre, ISBN, and Number of Copies for 5 different books in a tabular format given below:
    Book inventory
  3. Write a program that takes an integer input from the user and prints it on the screen.
  4. Modify the programe in Question 3 to validate that the user has entered an integer value.
Coming Soon...