Input and Output in C++
1. Introduction
Input and Output (I/O) are fundamental operations in C++ programming.
- Input means receiving data from the user or another source.
- Output means displaying or sending data to the user or another destination.
For example:
User → Input → C++ Program → Output → User
C++ mainly uses the <iostream> library for standard input and output.
2. The <iostream> Header
To perform basic input and output operations, include:
#include <iostream>
The <iostream> library provides objects such as:
cin→ standard inputcout→ standard outputcerr→ standard errorclog→ standard logging output
Example:
#include <iostream> using namespace std; int main() { cout << "Hello, C++!"; return 0; }
Output:
Hello, C++!
3. Output Using cout
cout is used to display information on the screen.
Syntax
cout << value;
Example:
#include <iostream> using namespace std; int main() { cout << "Hello World"; return 0; }
Output:
Hello World
4. Printing Variables
Variables can be displayed using cout.
#include <iostream> using namespace std; int main() { int age = 20; cout << "Age: " << age; return 0; }
Output:
Age: 20
5. Printing Multiple Values
Multiple values can be printed using multiple << operators.
#include <iostream> using namespace std; int main() { string name = "Anuradha"; int age = 20; cout << "Name: " << name << ", Age: " << age; return 0; }
Output:
Name: Anuradha, Age: 20
6. New Line
There are two common ways to move the output to a new line.
Using endl
cout << "Hello" << endl; cout << "World";
Output:
Hello World
Using \n
cout << "Hello\n"; cout << "World";
Output:
Hello World
\n is a newline character.
7. Taking Input Using cin
cin is used to receive input from the user.
Syntax
cin >> variable;
Example:
#include <iostream> using namespace std; int main() { int age; cout << "Enter your age: "; cin >> age; cout << "Your age is: " << age; return 0; }
Example output:
Enter your age: 20 Your age is: 20
8. Taking Multiple Inputs
Multiple values can be entered using multiple >> operators.
#include <iostream> using namespace std; int main() { int age; float marks; cout << "Enter age and marks: "; cin >> age >> marks; cout << "Age: " << age << endl; cout << "Marks: " << marks; return 0; }
Example input:
20 85.5
Output:
Age: 20 Marks: 85.5
9. Taking String Input
For a single word, cin can be used:
string name; cin >> name;
Example:
#include <iostream> #include <string> using namespace std; int main() { string name; cout << "Enter your name: "; cin >> name; cout << "Hello " << name; return 0; }
If the user enters:
Anuradha
Output:
Hello Anuradha
10. Reading a Full Line Using getline()
getline() is used when you want to read a complete line, including spaces.
Example:
#include <iostream> #include <string> using namespace std; int main() { string fullName; cout << "Enter your full name: "; getline(cin, fullName); cout << "Your name is: " << fullName; return 0; }
Input:
Anuradha Kumari Mandal
Output:
Your name is: Anuradha Kumari Mandal
Difference
cin >> name;
reads only up to whitespace.
getline(cin, name);
reads the entire line.
11. Important Issue with cin and getline()
Sometimes getline() appears to skip input when it is used immediately after cin >>.
Example:
int age; string name; cin >> age; getline(cin, name);
The newline left in the input buffer can cause this problem.
One solution is:
cin >> age; cin.ignore(); getline(cin, name);
Example:
#include <iostream> #include <string> using namespace std; int main() { int age; string name; cout << "Enter age: "; cin >> age; cin.ignore(); cout << "Enter full name: "; getline(cin, name); cout << "Name: " << name << endl; cout << "Age: " << age; return 0; }
12. Character Input
A char variable can be used to store one character.
#include <iostream> using namespace std; int main() { char grade; cout << "Enter your grade: "; cin >> grade; cout << "Your grade is: " << grade; return 0; }
Input:
A
Output:
Your grade is: A
13. Boolean Input
Boolean values can be handled using bool.
Example:
#include <iostream> using namespace std; int main() { bool passed; cout << "Enter 1 for passed or 0 for failed: "; cin >> passed; cout << "Result: " << boolalpha << passed; return 0; }
Input:
1
Output:
Result: true
14. Formatted Output
C++ provides different ways to control output formatting.
For example:
#include <iostream> #include <iomanip> using namespace std; int main() { double price = 125.6789; cout << fixed << setprecision(2); cout << price; return 0; }
Output:
125.68
Here:
fixeddisplays the number in fixed-point notation.setprecision(2)displays two digits after the decimal point.
15. setw() for Width
setw() can be used to set the width of an output field.
It is available through <iomanip>.
Example:
#include <iostream> #include <iomanip> using namespace std; int main() { cout << setw(10) << "C++"; return 0; }
This reserves a field width of 10 characters for the output.
16. Error Output Using cerr
cerr is used for displaying error messages.
Example:
#include <iostream> using namespace std; int main() { cerr << "Error: Invalid input!"; return 0; }
Output:
Error: Invalid input!
cerr is intended for error reporting.
17. Logging Using clog
clog is used for logging messages.
Example:
#include <iostream> using namespace std; int main() { clog << "Program started."; return 0; }
It is useful for diagnostic or logging information.
18. Basic Input/Output Example
The following program takes student information and displays it.
#include <iostream> #include <string> using namespace std; int main() { string name; int age; float marks; cout << "Enter your name: "; getline(cin, name); cout << "Enter your age: "; cin >> age; cout << "Enter your marks: "; cin >> marks; cout << "\n--- Student Information ---" << endl; cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Marks: " << marks << endl; return 0; }
Example:
Enter your name: Anuradha Kumari Enter your age: 20 Enter your marks: 85.5 --- Student Information --- Name: Anuradha Kumari Age: 20 Marks: 85.5
19. Input/Output Operators
C++ uses two important operators for standard I/O.
| Operator | Name | Purpose |
|---|---|---|
<< | Insertion operator | Sends data to output |
>> | Extraction operator | Reads data from input |
Example:
cout << "Hello"; cin >> age;
20. Common Escape Sequences
Escape sequences are special characters used inside strings and character literals.
| Escape Sequence | Meaning |
|---|---|
\n | New line |
\t | Horizontal tab |
\\ | Backslash |
\" | Double quotation mark |
\' | Single quotation mark |
\0 | Null character |
Example:
cout << "Name:\tAnuradha\nAge:\t20";
Output:
Name: Anuradha Age: 20
21. Common Input/Output Errors
Error 1: Forgetting <iostream>
Incorrect:
cout << "Hello";
Correct:
#include <iostream>
Error 2: Forgetting ;
Incorrect:
cout << "Hello"
Correct:
cout << "Hello";
Error 3: Using the Wrong Operator
Correct:
cin >> age; cout << age;
Remember:
cin → >> cout → <<
22. Summary
| Feature | C++ Syntax | Purpose |
|---|---|---|
| Output | cout << | Display data |
| Input | cin >> | Read data |
| Full-line input | getline() | Read a complete line |
| New line | \n | Move to next line |
| New line/flush | endl | End line and flush stream |
| Error output | cerr | Display errors |
| Logging | clog | Display diagnostic messages |
| Formatting | <iomanip> | Control output formatting |
23. Key Points
- C++ uses
<iostream>for standard input and output. coutis used to display output.cinis used to take input.getline()reads a complete line including spaces.endland\ncan be used for new lines.cerris used for error messages.clogis used for logging and diagnostic messages.<<is the insertion operator.>>is the extraction operator.<iomanip>provides tools for formatted output.
Conclusion
Input and output are essential parts of C++ programming. By using cin, cout, getline(), cerr, and clog, a C++ program can communicate with users and handle information effectively.