Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Lesson 4 - Basic Input-Process-Output With Flowchart

Uploaded by

edwardpoley5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lesson 4 - Basic Input-Process-Output With Flowchart

Uploaded by

edwardpoley5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MODULE

Basic Input, Process and

9 Output with Flowchart


XI. MODULE NINE: Basic Input, Process and Output with Flowchart

A. TARGET LEARNING OUTCOMES:


Upon going through the Module, the student should be able to
Determine how to display data on the screen
Identify how to accept input from the user
Create simple flowchart
Differentiate the flowchart symbol
Demonstrate and apply the used a 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.

Standard Output (cout)


By default, the standard output of a program is the screen, and the C++ stream
object defined to access it is cout. cout is used in conjunction with the insertion
operator, which is written as << (two "less than" signs).
cout << "Output sentence"; // prints Output sentence on screen
cout << 120; // prints number 120 on screen
cout << x; // prints the content of x on screen

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.

For example, these two sentences have very different results:


cout<< "Hello"; // prints Hello
cout << Hello; // prints the content of Hello variable

GEADLITE – Living in the Information Technology Era 1|P age


The insertion operator (<<) may be used more than once in a single statement:
cout << "Hello, " << "I am " << "a C++ statement";

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.";

This produces the following output:


First sentence.
Second sentence.
Third sentence.

Additionally, to add a new-line, you may also use the endl manipulator. For
example:
cout << "First sentence." << endl;
cout << "Second sentence." << endl;

would print out:


First sentence.

GEADLITE – Living in the Information Technology Era 2|P age


Second sentence.

Standard Input (cin)


The standard input device is usually the keyboard. Handling the standard input
in C++ is done by applying the overloaded operator of extraction (>>) on the cin
stream. The operator must be followed by the variable that will store the data
that is going to be extracted from the stream. For example:
int age;
cin >> age;

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

GEADLITE – Living in the Information Technology Era 3|P age


program will be cooperative and that he/she will not introduce his/her name or
something similar when an integer value is requested. A little ahead, when we
see the stringstream class we will see a possible solution for the errors that can
be caused by this type of user input.

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

Flow Chart Symbols Meaning


❖ The most common symbol used in a flowchart is the rectangle. A
rectangle represents a process, operation or a task. The next most
common symbol is the diamond which is used to represent a decision.
❖ There are many other flowchart symbols like Sequential access storage,
Direct data, Manual input etc. Check the flowchart symbols page for a
detailed explanation of different symbols.
❖ Although these are the standard symbols available in most flowchart
software, some people do use different shapes for different meanings.
The most common example of this is the using circles to denote start
and end. The examples in this flowchart guide will stick with the standard
symbols.

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

- Flow lines. This shows the direction of the


flowchart

- Input/ Output Block. The parallelogram symbol is


used for input and output instruction. For input the
instruction that can be used is Read or Input,
followed by a list of variables separated by comma.

GEADLITE – Living in the Information Technology Era 4|P age


For output the instruction that can be used is
Display or Print, followed by a list of variables and/
or text that will be displayed or printed.

- Preparation. This symbol is used to initialize the


value/s of the variable/s to be used

- Processing Block. This symbol is used to


process calculations. Formulas are usually entered
in this section of the flowchart. Formulas include
variables that will be replaced by figures entered by
the user. Activities that are ti be performed are also
included

- Decision Block. A diamond shaped symbol used


to indicate decisions. Done in question form and is
answerable by yes or no. Uses logical relations
such as equal, not equal, greater than, greater than
equal, less than and less than equal.

- On-page Connector. Flowcharts are written from


top to button, left to right. Once the bottom of the left
side is reached continuation of the flowchart can be
at the right side of the same page

- Off Page Connector. This symbol is used if the


flowchart is to be continued on the next page.

GEADLITE – Living in the Information Technology Era 5|P age

You might also like