Input and Output: October 27, 2021 Prof. Abdelaziz Khamis 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Lecture 3:

Input and Output

October 27, 2021 Prof. Abdelaziz Khamis 1


Lecture 2 Topics
 Input / Output Streams
 Reading from Standard Input
 Using Predefined Functions
 Input Failure
 Writing to Standard Output
 I/O and the string Type
 File Input/Output
 Programming Example
 Exercises

October 27, 2021 Prof. Abdelaziz Khamis 2


Input/output Streams
 Stream: sequence of characters from source to destination
 Input Stream: sequence of characters from an input device
to the computer
 Output Stream: sequence of characters from the computer
to an output device
 Use iostream to extract (receive) data from the standard
input (keyboard) and send output to the standard output
(screen)
 iostream has two variables
 cin - stands for common input
 cout - stands for common output

October 27, 2021 Prof. Abdelaziz Khamis 3


Reading from Standard Input
 Every occurrence of >> extracts the next data item from
the input stream

 No difference between a single cin with multiple variables


and multiple cin statements with one variable
 >> distinguishes between character 2 and number 2 by
the right hand operand of >>
 If it is of type char, the 2 is treated as character 2
 If it is of the type int (or double) the 2 is treated as the
number 2

October 27, 2021 Prof. Abdelaziz Khamis 4


Reading from Standard Input (Continued)
 When reading data into a char variable
 Extraction operator >> skips leading whitespace, finds and
stores only the next character (whitespace includes blanks
and the newline character)
 Reading stops after a single character

 To read data into an int or double variable


 Extraction operator >> skips leading whitespace, reads plus
or minus sign (if any), reads the digits (including decimal)
 Reading stops on whitespace or non-digit character

October 27, 2021 Prof. Abdelaziz Khamis 5


Reading from Standard Input (Continued)
 Example 1: Suppose you have the following variable declarations:
int a, b;
double z;
char ch;

The following statements show how the extraction operator >> works.

Statement
Statement Input
Input Values
Values stored
stored in
in memory
memory
cin
cin >>
>> ch;
ch; AB
AB ch
ch == 'A',
'A',
cin
cin >>
>> a;
a; 46.35
46.35 aa == 46
46
cin
cin >>
>> z;
z; 39
39 zz == 39.0
39.0
cin
cin >>
>> zz >>
>> a;
a; 65.78
65.78 38
38 zz == 65.78,
65.78, aa == 38
38

October 27, 2021 Prof. Abdelaziz Khamis 6


Reading from Standard Input (Continued)
 Example 2: Suppose you have the following variable declarations:
int a;
double z;
char ch;
The following statements show how the extraction operator >> works.

Statement Input Values stored in memory


cin >> a >> ch >> z; 57 A 26.9 a = 57, ch = 'A', z = 26.9
cin >> a >> ch >> z; 57 A a = 57, ch = 'A', z = 26.9
26.9
cin >> a >> ch >> z; 57A26.9 a = 57, ch = 'A', z = 26.9
cin >> a >> ch >> z; 57 a = 57, ch = 'A', z = 26.9
A
26.9
October 27, 2021 Prof. Abdelaziz Khamis 7
Using Predefined Functions
 A function (subprogram): set of instructions, when
executed, accomplishes a task
 main executes when a program is run
 Other functions execute only when called
 Predefined functions are organized as a collection of
libraries called header files
 To use a predefined function, you need to know:
 Name of the appropriate header file
 Function name
 Number of parameters required
 Type of each parameter
 What the function is going to do
October 27, 2021 Prof. Abdelaziz Khamis 8
Using Predefined Functions (Continued)
 The pow Function:
 To use the predefined function pow, include cmath
 pow has two numeric parameters
The syntax is: pow(x,y)
 It returns the value of xy
 cin and the get Function:
 get has one character parameter
The syntax is: cin.get(varChar); // dot is the member access operator
 It inputs the next character (including whitespace) from the
input stream, and stores it in the memory location indicated
by its argument.

October 27, 2021 Prof. Abdelaziz Khamis 9


Using Predefined Functions (Continued)
 cin and the ignore Function:
 When you want to process only partial data (say, within a
line), you can use the stream function ignore to discard a
portion of the input.
The syntax is: cin.ignore(intExp, chExp);
 If intExp has the value m, the statement, when executed,
ignores either the next m characters or all characters until the
character specified by chExp, whichever comes first.
 When the function ignore is used without any arguments,
then it only skips the very next character. For example, the
following statement will skip the very next character:
cin.ignore();

October 27, 2021 Prof. Abdelaziz Khamis 10


Using Predefined Functions (Continued)
 Example 3: Consider the declaration:
int a, b;
and the input:
25 67 89 43 72
12 78 34
Now consider the following statements:
cin >> a;
cin.ignore(100, '\n');
cin >> b;
Then, the values stored in memory are:
a = 25 & b = 12

October 27, 2021 Prof. Abdelaziz Khamis 11


Using Predefined Functions (Continued)
 Example 4: Consider the declaration:
char ch1, ch2;
and the input:
Hello there. My name is Mickey.
Now consider the following statements:
cin >> ch1;
cin.ignore(5, '.');
cin >> ch2;
Then, the values stored in memory are:
ch1 = ‘H’ & ch2 = ‘t’

October 27, 2021 Prof. Abdelaziz Khamis 12


Input Failure
 Trying to read data that does not match the corresponding
variables would result in an input failure (An example:
Trying to read a letter into an int or double variable)
 Once in a fail state, all further I/O statements using that
stream are ignored. The program continues to execute
with whatever values are stored in variables.

October 27, 2021 Prof. Abdelaziz Khamis 13


Writing to Standard Output
 Recall that the syntax of cout when used together with the
insertion operator << is:
cout << expression or manipulator
<< expression or manipulator...;
 Expression is evaluated
 Value is printed
 Manipulator is used to format the output
 The simplest manipulator that we have used so far is endl,
which is used to move the insertion point to the beginning
of the next line.
 Other output manipulators that are of interest include
setprecision, setw, setfill, left and right.

October 27, 2021 Prof. Abdelaziz Khamis 14


Writing to Standard Output (Continued)
 setprecision Manipulator
 setprecision(n) outputs decimal numbers with up to n
decimal places
Notice that the number of decimal places, or the precision
value, is passed as an argument to setprecision
 For example, the statement
cout << setprecision(2);
formats the output of decimal numbers to two decimal places
until a similar subsequent statement changes the precision.
 To use the manipulator setprecision, the program must
include the header file iomanip

October 27, 2021 Prof. Abdelaziz Khamis 15


Writing to Standard Output (Continued)
 fixed Manipulator
 The following statement sets the output of floating-point
numbers in a fixed decimal format on the standard output
device:
cout << fixed;
 The manipulator fixed is opposite of scientific
 showpoint Manipulator
 The following statement sets the output of decimal numbers
with a decimal point and trailing zeros on the standard out
device:
cout << showpoint;

October 27, 2021 Prof. Abdelaziz Khamis 16


Writing to Standard Output (Continued)
 Example 5:

October 27, 2021 Prof. Abdelaziz Khamis 17


Writing to Standard Output (Continued)
 setw Manipulator
 setw outputs the value of an expression in specific columns
 If the number of columns exceeds the number of columns
required by the expression
 Output of the expression is right-justified
 Unused columns to the left are filled with spaces
 If the number of columns specified is less than the number of
columns required by the output, the output automatically
expands to the required number of columns
 An example: cout << setw(5) << x << endl;
 To use the manipulator setw, the program must include the
header file iomanip

October 27, 2021 Prof. Abdelaziz Khamis 18


Writing to Standard Output (Continued)
 setfill Manipulator
 The output stream variables can use the manipulator setfill to
fill the unused columns with a character other than a space.
 For example, cout << setfill('*');
sets the fill character to ‘*' on the standard output device.
 left and right Manipulators
 To left-justify the output, you use the manipulator left.
 For example, the following statement sets the output to be
left-justified on the standard output device:
cout << left;

October 27, 2021 Prof. Abdelaziz Khamis 19


I/O and string Type
 An input stream variable (cin) and extraction operator >>
can read a string into a variable of the data type string
 The extraction operator >> skips any leading whitespace
characters and reading stops at a whitespace character
 The extraction operator should not be used to read strings
with blanks
 The function getline
 Reads until end of the current line
 Should be used to read strings with blanks
 The syntax to use the function getline is:
getline(istreamVar, strVar);
The reading is delimited by the newline character '\n'.
October 27, 2021 Prof. Abdelaziz Khamis 20
File Input/Output
 The standard I/O header file, iostream, contains data types
and variables that are used only for input from the
standard input device (Keyboard) and output to the
standard output device (Screen).
 In addition, C++ provides a header file called fstream,
which is used for file I/O.
 File I/O is a five-step process:
1. Include the header file fstream in the program.
2. Declare file stream variables.
3. Associate the file stream variables with the input/output sources.
4. Use the file stream variables with >>, <<, or other input/output
functions.
5. Close the files.

October 27, 2021 Prof. Abdelaziz Khamis 21


File Input/Output (Continued)
 Step 1
#include <fstream>
 Step 2
ifstream inData;
ofstream outData;
 Step 3
inData.open(“prog.dat");
outData.open("prog.out");
 Step 4
inData >> payRate; // reads the data from the file prog.dat
outData << pay; // stores the output in the file prog.out
 Step 5
inData.close();
outData.close();
October 27, 2021 Prof. Abdelaziz Khamis 22
Programming Example
 Write a program that reads a student name followed by
five test scores. The program should output the student
name, the five test scores, and the average test score.
Output the average test score with two decimal places.
 The data to be read is stored in a file called test.dat. The
output should be stored in a file called testavg.out.
 A sample input:
Andrew Miller
87.50 89 65.75 37 98.50
 A sample output:
Student Name: Andrew Miller
Test scores: 87.50 89.00 65.75 37.00 98.50
Average test score: 75.55

October 27, 2021 Prof. Abdelaziz Khamis 23


Programming Example (Continued)
 Problem Analysis:
 Input: string studentName;
double test1, test2, test3, test4, test5;
 Output: double average;
 Formulas:
average = (test1 + test2 + test3 + test4 + test5) / 5;

 Algorithm Design:
1. Read the student name and the five test scores.
2. Output the student name and the five test scores.
3. Calculate the average.
4. Output the average

October 27, 2021 Prof. Abdelaziz Khamis 24

You might also like