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

Lesson 5:

BASIC
INPUT/OUTPUT
BSMATH-1A
May 13, 2024

Prepared by:

John Mark S. Policarpio


Instructor I
Basic Input / Output in C++
C++ comes with libraries that provide us with many ways
for performing input and output. In C++ input and output
are performed in the form of a sequence of bytes or
more commonly known as streams.
● Input Stream: If the direction of flow of bytes is
from the device(for example, Keyboard) to the
main memory then this process is called input.
● Output Stream: If the direction of flow of bytes is
opposite, i.e. from main memory to device (display
screen) then this process is called output.
2
Header files available in C++ for Input/Output operations
are:
1. iostream: iostream stands for standard input-output
stream. This header file contains definitions to objects like
cin, cout, cerr etc.
2. iomanip: iomanip stands for input output manipulators.
The methods declared in this files are used for manipulating
streams. This file contains definitions of setw, setprecision,
etc.
3. fstream: This header file mainly describes the file stream.
This header file is used to handle the data being read from a
file as input or data being written into the file as output.
3
The two keywords cout in C++ and cin in C++ are
used very often for printing outputs and taking
inputs respectively. These two are the most basic
methods of taking input and printing output in C++.
To use cin and cout in C++ one must include the
header file iostream in the program.

4
C++ Output

In C++, cout sends formatted output to standard


output devices, such as the screen. We use the
cout object along with the << operator for
displaying output.

5
Example 1: String Output

#include <iostream> Output


using namespace std; This is C++ Programming

int main() {
// prints the string enclosed in double quotes
cout << "This is C++ Programming";
return 0;
}

6
Note: If we don't include the using namespace std; statement, we need to
use std::cout instead of cout.

This is the preferred method as using the std namespace can create
potential problems.

However, we have used the std namespace in our tutorials in order to


make the codes more readable.

#include <iostream>

int main() {
// prints the string enclosed in double quotes
std::cout << "This is C++ Programming";
return 0;
}
7
Example 2: Numbers and Characters Output

To print the numbers and character variables, we use the same


cout object but without using quotation marks.

#include <iostream>
using namespace std;
Output:
int main() { 70
int num1 = 70; 256.783
double num2 = 256.783; character: A
char ch = 'A';

cout << num1 << endl; // print integer


cout << num2 << endl; // print double
cout << "character: " << ch << endl; // print char
return 0;
} 8
Notes:

The endl manipulator is used to insert a new line. That's


why each output is displayed in a new line.

The << operator can be used more than once if we want


to print different variables, strings and so on in a single
statement. For example:

cout << "character: " << ch << endl;

9
C++ Input

In C++, cin takes formatted input from standard


input devices such as the keyboard. We use the
cin object along with the >> operator for taking
input.

10
Example 3: Integer Input/Output

#include <iostream>
using namespace std; Output

int main() { Enter an integer: 70


int num; The number is: 70
cout << "Enter an integer: ";
cin >> num; // Taking input
cout << "The number is: " << num;
return 0;
}
11
In the program, we used
cin >> num;
to take input from the user. The input is stored in
the variable num. We use the >> operator with cin
to take input.

Note: If we don't include the using namespace


std; statement, we need to use std::cin instead
of cin.

12
C++ Taking Multiple Inputs
#include <iostream>
Output
using namespace std;
Enter a character and an integer:
int main() { F
char a; 23
int num; Character: F
Number: 23
cout << "Enter a character and an integer: ";
cin >> a >> num;

cout << "Character: " << a << endl;


cout << "Number: " << num;

return 0;
} 13
THANK YOU!
Any questions?
Prepared by:

John Mark S. Policarpio

Instructor I

You might also like