Input/Output Functions
SESSION 4
Input/Output
Input/Output (I/O) is how programs
communicate with users.
Input: Data given by user → Program
Output: Data shown by program → User
In C++, I/O can be done using:
iostream (C++ way)
stdio.h (C-style I/O functions)
Standard Output Functions
Header file: #include <iostream>
Uses streams for input and output.
Important objects:
cout → Standard Output Stream
cin → Standard Input Stream
Part of the std namespace → Use std::cout, or
using namespace std;
The cout Object and << Operator
Syntax: cout << "Hello";
<< is the insertion operator (puts data into
output stream).
Can chain multiple outputs:
cout << "Name: " << name << ", Age: " <<
age;
Data is sent to the console in sequence.
Escape Sequences
Escape sequences start with \ and allow special
formatting:
Sequence Description
\n New Line
\t Horizontal Tab
\\ Backslash
\" Double Quote
\a Alert (Beep sound)
🔹 \n – New Line
.
Moves the cursor to the next line.
Eg: "Hello\nWorld!“
🔹 \t – Horizontal Tab
Inserts a tab space (like pressing Tab key).
Eg: "Name\t:\tJohn\nAge\t:\t20“
🔹 \\ – Backslash
Prints a single backslash (\).
Eg: "This is a backslash: \\“
🔹 \" – Double Quote
Prints a double quote character.
Eg: "He said, \"C++ is powerful!\""
Console Input/Output Functions
#include <iostream>: Enables input/output
➡ cin takes input from the user
➡ >> is the extraction operator
➡ cout prints to console
➡ << is the insertion operator
getch(), getche(), getchar()
getch() is read a single character from console
defined in conio.h.
getch() not echo the character.
getche() is read a single character from console
define in conio.h.
getche() echo the character.
getchar() is read a single character from
console defined in stdio.h.
cin vs getline
Feature cin getline(cin, var)
Reads until Whitespace Newline
Works with Any datatype Only string
Useful for Numbers, words Full sentences
Output statement Input Statement
.
‘cout’ is console ‘cin’ is console
output ,used to print input,used to read from
output. console.
cout is the object of cin is the object of
ostream class istream class.
The cout is used in The cin is used in
conjunction with stream conjunction with stream
insertion operator (<<) extraction operator (>>)
to display the output on to read the input from a
a console console.
END