C++ Programming:
Problem Solving and
Programming
Chapter 3
Input Output
iostream header file
• Use iostream to extract (receive) data from
keyboard and send output to the screen
• iostream has two standard variables
− cin - stands for common input
− cout - stands for common output
• To use cin and cout, the preprocessor
directive #include <iostream> must be
used
3.1 cin and Extraction Operator >>
• Input received from keyboard
• Syntax: cin>> variable1 >> variable2 ...;
e.g. cin>>payRate;
cin>>hoursWorked;
OR
cin>>payRate>>hoursWorked; // more than 1
input
Input Data Type
cin and get Function
Consider the variable declarations:
char ch1, ch2;
int num;
Input : A 25
Statement : cin>>ch1>>ch2>>num;
Reads ‘A’ into ch1, ‘2’ into ch2, 5 into num
Blank space was not read
cin and get Function
• The get function
− Inputs next character (including blank
space)
• The syntax of cin and the get function:
cin.get(varChar);
• Example:
Input : A 25
Statements: cin.get(ch1);
cin.get(ch2);
cin>>num;
=> Read ‘A’ into ch1, blank space into ch2 ,
25 into num
Input Failure
• Things can go wrong during execution
If input data does not match the
corresponding variables, the program may
run into problems
Read a letter into an int or double variable
Program continue to run but result is not
correct
Input Failure - Example
#include <iostream>
using namespace std;
int main() {
int num1, num2, total=0;
cout<<“Enter num1 = “;
cin>>num1;
cout<<“Enter num2 = “;
cin>>num2;
total = num1 + num2;
cout<<“Total = “<<total<<endl;
return 0;
}
3.2 cout and insertion operator <<
• Output sent to screen for display
• Syntax: cout<<expression/manipulator
<<expression/ manipulator...;
• Example
cout << “I am 18!”;
cout << “I am " << “18!";
cout << “I am ";
cout << “18!";
int age = 18;
cout << “I am “ << age << “!”;
The endl Manipulator
• You can use the endl manipulator to start a
new line of output. This will produce two
lines of output:
cout << "Programming is" << endl;
cout << "fun!";
Programming is
fun!
The \n Escape Sequence
• You can also use the \n escape sequence
to start a new line of output. This will
produce two lines of output:
cout << "Programming is\n";
cout << "fun!";
Programming is
fun!
3.3 Formatting Output by Manipulator
• Two types of manipulators:
− With parameters
− Without parameters
Parameterized: use iomanip header file
- setw(n), setprecision(n), and setfill(ch)
Nonparameterized: use iostream header file
- endl, fixed, showpoint, left and right
The setw(n) Manipulator
• set the width for the output using n columns
• If the width / number of columns exceeds the
number of columns required by the output
− Output will be right-justified
− Unused columns to the left are filled with
spaces
The setw(n) Manipulator - Example
The setfill(ch) Manipulator
• Used to fill the unused columns with a
character other than a space
• Note: ch = character
The setfill(ch) Manipulator - Example
#include <iostream>
#include <iomanip>
using namespace std;
int main( ) {
int x = 22, y = 8899;
cout << "12345678901234567890" << endl;
cout << setw(5) << x << setw(7) << y
<< setw(8) << "End" << endl;
cout << setfill('-');
cout << setw(5) << x << setw(7) << y
<< setw(8) << "End" << endl;
cout << setw(5) << x << setw(7) << setfill(‘$')
<< y << setw(8) << "End" << endl;
cout << setfill(' ');
cout << setw(5) << x << setw(7) << y
<< setw(8) << "End" << endl << endl;
return 0;
}
The setprecision(n) Manipulator
• Display floating-point value up to n significant
digits
• Display floating-point value up to n decimal
places when it is used with fixed manipulator
• Use the following statement to disable the
fixed manipulator:
cout.unsetf(ios::fixed);
The setprecision(n) Manipulator -
Example
#include <iostream>
#include <iomanip>
using namespace std;
int main( ) {
double num = 5.6789;
cout << num << endl;
cout << setprecision(4) << num << endl;
cout << setprecision(3) << num << endl;
cout << setprecision(2) << num << endl;
cout << setprecision(1) << num << endl;
return 0;
The fixed Manipulator - Example
#include <iostream>
#include <iomanip>
using namespace std;
int main( ) {
double num = 5.6789;
cout << num << endl << endl;
cout << fixed;
cout << "With fixed manipulatior" << endl;
cout << setprecision(3) << num << endl;
cout << setprecision(2) << num << endl << endl;
cout.unsetf(ios::fixed);
cout << "Without fixed manipulatior" << endl;
cout << setprecision(3) << num << endl;
cout << setprecision(2) << num << endl << endl;
return 0;
}
The showpoint Manipulator
• Causes a decimal point and trailing zeroes to
be displayed, even if there is no fractional part
Example:
#include <iostream>
#include <iomanip>
using namespace std;
int main( ) {
double x= 123.4, y = 456.0;
cout << setprecision(6) << x << endl;
cout << y << endl << endl;
return 0;
}
The showpoint Manipulator - Example
#include <iostream>
#include <iomanip>
using namespace std;
int main( ) {
double x= 123.4, y = 456.0;
cout << setprecision(6) << showpoint << x << endl;
cout << y << endl<< endl;
return 0;
}
The left & right Manipulator
• Output is right justified by default
• cout<<left;
to left justify the output
disable left by using - cout.unsetf(ios::left);
• cout<<right;
to right justify the output
The left & right Manipulator -
Example
#include <iostream>
#include <iomanip>
using namespace std;
int main( ) {
double x = 123.456, y = 78.9, z = 0.321;
cout << "1234567890" << endl << endl;
cout << setw(10) << x << endl;
cout << setw(10) << y << endl;
cout << setw(10) << z << endl;
cout << left << endl;
cout << setw(10) << x << endl;
cout << setw(10) << y << endl;
cout << setw(10) << z << endl;
cout << endl;
cout.unsetf(ios::left); // Or Use cout << right
cout << setw(10) << x << endl;
cout << setw(10) << y << endl;
cout << setw(10) << z << endl;
return 0;
}
Other I/O Manipulators
− showpoint - display decimal point and trailing zeros
− noshowpoint - drop decimal point and trailing zeros
− showpos - prints a ‘+’ sign for positive no.
− noshowpos - prints no ‘+’ sign for positive no.
− scientific - output as scientific notation
− dec - displays subsequent no. in decimal format
− hex - inputs or outputs in hexadecimal format
− oct – inputs or outputs in octal format
Other I/O Manipulators - Examples
• fixed or scientific
double n=123.2345;
cout << n << endl;
cout << fixed << n << endl;
cout << scientific << n << endl;
cout << fixed << setprecision(4) << n <<
endl;
123.234
123.234500
1.232345e+002
123.2345
Other I/O Manipulators - Examples
• showpoint & showpos
double n=123;
cout << n << endl;
cout << showpoint << n << endl;
cout << noshowpoint << n << endl;
cout << fixed << n << endl; 123
123.000
cout << showpos << n << endl; 123
cout << noshowpos << n << endl; 123.000000
+123.000000
123.000000
Other I/O Manipulators - Examples
• dec, octal, hex
cout << "Enter hexadecimal no: ";
cin >> hex >> n;
cout << n << endl; Enter hexadecimal no: 12
18
cout << dec << n << endl; 18
cout << oct << n << endl; 22
cout << n << endl; 22
cout << dec << n << endl; 18
12
cout << hex << n << endl;
Exercise
Generate the following output:
Note: Declare meaningful variable
names for each of the figures
Staff ID : 1234 shown
Dept Code : 99
Hint : Use manipulators setw(n),
Year : 2010
setprecision(n), setfill(ch), fixed,
left, right
Annual Salary (RM)
--------------------------
Income : 65000.00
Bonus : 195000.00
Minus: Insurance : 2000
Income Tax : 18000
-------------------------
Total Net Income : * * * * 240000.00
13 spaces