0% found this document useful (0 votes)
48 views37 pages

Chap02 - Input Output

Uploaded by

lop12138lol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views37 pages

Chap02 - Input Output

Uploaded by

lop12138lol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

C++ Programming

Problem Solving and


Programming

Chapter 2
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
cin >>

cout <<
Input ( cin , get , getline )
cin and Extraction Operator >>
🠶 Input received from keyboard
🠶 Note: cin skips the whitespace characters like
spaces(‘ ’), newlines(‘\n’), tabs(‘\t’), etc.
🠶 Syntax: cin>> variable1 >> variable2 ...;
e.g. cin>>payRate;
cin>>hoursWorked;
OR
// more than 1 input
cin>>payRate>>hoursWorked;
Input Data Type
cin and get Function
Scenario 1:
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
🠶 Read next character (including blank space)
🠶 The syntax of cin and get function combined:
cin.get(variable);
🠶 Example:
Input : A 25
Statements: cin.get(ch1);
cin.get(ch2);
cin>>num;
=> Read ‘A’ into ch1, blank space into ch2 ,
25 into num
cin and get Function
Scenario 2:
char gender;
int age;

cout << “Enter age: ”;


cin >> age;
cout << “Enter gender: ”;
cin >> gender;
cout << "Age is " << age << endl;
cout << "Gender is " << gender << endl;
cin and get Function
Scenario 3:
char gender;
int age;

cout << “Enter age: ”;


cin >> age;
cout << “Enter gender: ”; cin.get(..) reads the
‘\n’ character left
cin.get(gender); behind by the
previous cin in the
cout << "Age is " << age << endl;
memory buffer
cout << "Gender is " << gender << endl;
cin and get Function
Scenario 3:
cin.ignore() is used
char gender; to skip (ignore)
leftover character in
int age; the memory buffer.
cout << “Enterage:Note:
”; Can use
cin.get() also
cin >> age;
cin.ignore();
cout << “Enter gender: ”;
cin.get(gender);
cout << "Age is " << age << endl;
cout << "Gender is " << gender << endl;
cin and getline Function
🠶 The difference of reading strings of these
functions
🠶 cin can only read single word, it stops
before whitespace character is detected
🠶 getline(cin, <string variable>) can read
the whole string before the newline
character is detected
cin and getline Function
Scenario 1:
string name;
char gender;

cout << "Enter name: ";


First cin reads only the
cin >> name; first word of name,
cout << "Enter gender: "; second cin reads first
character of second word
cin >> gender; after skipping a space
cout << "Name is " << name << endl;
cout << "Gender is " << gender << endl;
cin and getline Function
Scenario 2:
string name;
char gender;

cout << "Enter name: ";


getline(cin, name);
cout << "Enter gender: ";
cin >> gender;
cout << "Name is " << name << endl;
cout << "Gender is " << gender << endl;
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;
}
Output ( cout )
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;
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!
Formatting Output by Manipulator

🠶 Two types of manipulators:


🠶 Parameterized (With parameters)
🠶 Nonparameterized (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>
cout << setfill(' ');
#include <iomanip>
cout << setw(5) << x <<
using namespace std; setw(7) << y
int main( ) { << setw(8) << "End" <<
endl << endl;
int x = 22, y = 8899;
return 0;
}
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" <<
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
:
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
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 << endl;

cout.unsetf(ios::left); // Or Use cout << right


cout << setw(10) << x << endl;
cout << setw(10) << y << endl;
cout << setw(10) << z << endl;
:
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; 123

cout << showpoint << n << endl; 123.000


cout << noshowpoint << n << endl; 123
cout << fixed << n << endl; 123.000000
cout << showpos << n << endl; +123.000000
cout << noshowpos << n << endl; 123.000000
Other I/O Manipulators -
Examples
🠶 dec, octal, hex
cout << "Enter hexadecimal no: ";
cin >> hex >> n; Enter hexadecimal no: 12
cout << n << endl; 18
cout << dec << n << endl; 18
cout << oct << n << endl; 22
cout << n << endl; 22
cout << dec << n << endl; 18
cout << hex << n << endl; 12
Exercise
Generate the following output:

Note: Declare meaningful variable


Staff ID : 1234
names for each of the figures
Dept Code : 99 shown
Year : 2010
Hint : Use manipulators setw(n),
setprecision(n), setfill(ch), fixed,
Annual Salary (RM) left, right
--------------------------
Income : 65000.00
Bonus : 195000.00
Minus: Insurance : 2000
Income Tax : 18000
-------------------------
Total Net Income : * * * * 240000.00

13 spaces

You might also like