Lesson 4 - Basic Input-Process-Output With Flowchart
Lesson 4 - Basic Input-Process-Output With Flowchart
B. INTRODUCTION
This module describes the use of stream for input and output. Using the
standard input and output library, we will be able to interact with the user by
printing messages on the screen and getting the user's input from the keyboard
and flowcharting that will represent an algorithm
C. LEARNING CONTENT
I/O Stream Classes
During the development of C++, a new class-based input/output system was
implemented. This gave rise to the I/O stream classes, which are now available
in a library of their own, the so-called iostream library.
The istream and ostream classes derived from ios form a user-friendly interface
for stream manipulation. The istream class is used for reading streams and the
ostream class is used for writing to streams. The operator >> is defined in
istream and << is defined in ostream, for example.
The iostream class is derived by multiple inheritance from istream and ostream
and thus offers the functionality of both classes.
The << operator inserts the data that follows it into the stream preceding it. In
the examples above it inserted the constant string Output sentence, the
numerical constant 120 and variable x into the standard output stream cout.
Notice that the sentence in the first instruction is enclosed between double
quotes (") because it is a constant string of characters. Whenever we want to
use constant strings of characters, we must enclose them between double
quotes (") so that they can be clearly distinguished from variable names.
This last statement would print the message Hello, I am a C++ statement on
the screen. The utility of repeating the insertion operator (<<) is demonstrated
when we want to print out a combination of variables and constants or more
than one variable:
cout << "Hello, I am " << age << " years old and my zipcode is " <<
zipcode;
If we assume the age variable to contain the value 24 and the zipcode variable
to contain 90064 the output of the previous statement would be:
Hello, I am 24 years old and my zipcode is 90064
It is important to notice that cout does not add a line break after its output unless
we explicitly indicate it, therefore, the following statements:
cout << "This is a sentence.";
cout << "This is another sentence.";
will be shown on the screen one following the other without any line break
between them:
This is a sentence.This is another sentence.
even though we had written them in two different insertions into cout. In order
to perform a line break on the output we must explicitly insert a new-line
character into cout. In C++ a new-line character can be specified as \n
(backslash, n):
cout << "First sentence.\n ";
cout << "Second sentence.\nThird sentence.";
Additionally, to add a new-line, you may also use the endl manipulator. For
example:
cout << "First sentence." << endl;
cout << "Second sentence." << endl;
The first statement declares a variable of type int called age, and the second
one waits for an input from cin (the keyboard) in order to store it in this integer
variable.
cin can only process the input from the keyboard once the RETURN key has
been pressed. Therefore, even if you request a single character, the extraction
from cin will not process the input until the user presses RETURN after the
character has been introduced.
You must always consider the type of the variable that you are using as a
container with cin extractions. If you request an integer you will get an integer,
if you request a character you will get a character and if you request a string of
characters you will get a string of characters.
Example 1
// i/o example
#include <iostream>
using namespace std;
int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
return 0;
}
Output:
Please enter an integer value: 702
The value you entered is 702 and its double is 1404.
The user of a program may be one of the factors that generate errors even in
the simplest programs that use cin (like the one we have just seen). Since if
you request an integer value and the user introduces a name (which generally
is a string of characters), the result may cause your program to misoperate
since it is not what we were expecting from the user. So when you use the data
input provided by cin extractions you will have to trust that the user of your
You can also use cin to request more than one datum input from the user:
cin >> a >> b;
equivalent to:
cin >> a;
cin >> b;
In both cases the user must give two data, one for variable a and another one
for variable b that may be separated by any valid blank separator: a space, a
tab character or a newline.
Flow chart
❖ It is a diagram showing step-by-step procedure of a specific task
❖ It can also be used to illustrate the existing process or the solution to a
problem
❖ Using flow chart it is possible to write computer programs in several
computer language
Flowchart Symbol
- Terminal Block. This symbol is used to mark the
start and end of the list of instructions. Start is used
to mark the starting point and End is used to mark
the end of the flowchart