Chapter 3: Input and Output in C++
Almost every program needs to interact with users. A calculator asks for numbers, a login system asks for a username and password, and a game asks for the player's name.
In C++, we use Input and Output (I/O) to communicate with users.
- Input means receiving data from the user.
- Output means displaying information on the screen.
The <iostream> library provides the tools needed for console input and output.
1#include <iostream>
Understanding <iostream>
iostream stands for Input/Output Stream.
It provides:
cin→ Read input from the keyboard.cout→ Display output on the screen.cerr→ Display error messages.clog→ Display log messages.
For beginners, the most commonly used objects are cin and cout.
Using cout
cout stands for Character Output. It sends data to the console.
Syntax
1cout << value;
Example 1: Print Text
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 cout << "Welcome to C++ Programming!"; 8 9 return 0; 10}
Output
Welcome to C++ Programming!
Example 2: Print Numbers
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 cout << 100; 8 9 return 0; 10}
Output
100
Example 3: Print Variables
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int age = 21; 8 9 cout << age; 10 11 return 0; 12}
Output
21
Example 4: Print Multiple Values
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 string name = "Ankit"; 8 int age = 22; 9 10 cout << "Name: " << name << endl; 11 cout << "Age: " << age; 12 13 return 0; 14}
Output
Name: Ankit
Age: 22
Stream Insertion Operator (<<)
The << symbol is called the stream insertion operator.
It inserts data into the output stream.
Example:
1cout << "Hello"; 2cout << 25; 3cout << 99.5;
You can chain multiple values together.
1cout << "Age: " << age << " Years";
Output
Age: 22 Years
Using cin
cin stands for Character Input.
It reads data entered by the user.
Syntax
1cin >> variable;
Example 1: Read an Integer
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int age; 8 9 cout << "Enter your age: "; 10 11 cin >> age; 12 13 cout << "Your age is " << age; 14 15 return 0; 16}
Output
Enter your age: 22
Your age is 22
Example 2: Read Multiple Values
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int a, b; 8 9 cout << "Enter two numbers: "; 10 11 cin >> a >> b; 12 13 cout << "First Number: " << a << endl; 14 cout << "Second Number: " << b; 15 16 return 0; 17}
Output
Enter two numbers: 10 20
First Number: 10
Second Number: 20
Stream Extraction Operator (>>)
The >> operator is called the stream extraction operator.
It extracts data from the keyboard and stores it in variables.
1cin >> age;
Using endl
endl inserts a new line and flushes the output buffer.
Example
1cout << "Welcome" << endl; 2cout << "Tech3Space";
Output
Welcome
Tech3Space
Using \n
You can also use the newline character.
1cout << "Welcome\n"; 2cout << "Tech3Space";
Output
Welcome
Tech3Space
endl vs \n
endl | \n |
|---|---|
| Inserts a new line | Inserts a new line |
| Flushes the output buffer | Does not flush the buffer |
| Slightly slower | Faster |
For most programs, \n is preferred unless you specifically need to flush the output.
Using getline()
getline() reads an entire line of text, including spaces.
Syntax
1getline(cin, variable);
Example
1#include <iostream> 2#include <string> 3 4using namespace std; 5 6int main() 7{ 8 string fullName; 9 10 cout << "Enter your full name: "; 11 12 getline(cin, fullName); 13 14 cout << "Hello " << fullName; 15 16 return 0; 17}
Output
Enter your full name:
Ankit Kushwaha
Hello Ankit Kushwaha
Difference Between cin and getline()
cin | getline() |
|---|---|
| Reads one word | Reads the entire line |
| Stops at space | Includes spaces |
| Best for numbers and single words | Best for names, addresses, and sentences |
Example
1string name; 2 3cin >> name;
Input
Ankit Kushwaha
Output
Ankit
Only the first word is stored.
Using getline():
1getline(cin, name);
Output
Ankit Kushwaha
Mixing cin and getline()
A common beginner mistake is using getline() immediately after cin.
Incorrect
1int age; 2string name; 3 4cin >> age; 5getline(cin, name);
getline() reads the leftover newline character and returns an empty string.
Correct
1#include <iostream> 2#include <limits> 3#include <string> 4 5using namespace std; 6 7int main() 8{ 9 int age; 10 string name; 11 12 cout << "Enter age: "; 13 cin >> age; 14 15 cin.ignore(numeric_limits<streamsize>::max(), '\n'); 16 17 cout << "Enter full name: "; 18 getline(cin, name); 19 20 cout << "\nName: " << name << endl; 21 cout << "Age: " << age << endl; 22 23 return 0; 24}
Complete Example
1#include <iostream> 2#include <string> 3 4using namespace std; 5 6int main() 7{ 8 string name; 9 int age; 10 11 cout << "Enter your name: "; 12 getline(cin, name); 13 14 cout << "Enter your age: "; 15 cin >> age; 16 17 cout << "\n----- User Information -----\n"; 18 cout << "Name : " << name << endl; 19 cout << "Age : " << age << endl; 20 21 return 0; 22}
Sample Output
Enter your name:
Ankit Kushwaha
Enter your age:
22
----- User Information -----
Name : Ankit Kushwaha
Age : 22
Best Practices
- Include
<iostream>for input/output. - Include
<string>when usingstring. - Use prompts before reading input.
- Use meaningful variable names.
- Prefer
getline()for full names and sentences. - Use
\nfor better performance when flushing isn't needed. - Use
cin.ignore()beforegetline()ifcinwas used first.
Common Beginner Mistakes
Forgetting >>
❌ Incorrect
1cin age;
✅ Correct
1cin >> age;
Missing Semicolon
❌ Incorrect
1cout << "Hello"
✅ Correct
1cout << "Hello";
Using cin for Full Names
❌
1cin >> fullName;
Input
Ankit Kushwaha
Output
Ankit
✅
1getline(cin, fullName);
Practice Exercises
Exercise 1
Write a program to input your name and display it.
1#include <iostream> 2#include <string> 3 4using namespace std; 5 6int main() 7{ 8 string name; 9 10 cout << "Enter your name: "; 11 getline(cin, name); 12 13 cout << "Hello, " << name << "!"; 14 15 return 0; 16}
Exercise 2
Read two integers and print their sum.
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int a, b; 8 9 cout << "Enter two numbers: "; 10 cin >> a >> b; 11 12 cout << "Sum = " << a + b; 13 14 return 0; 15}
Exercise 3
Create a student information program.
1#include <iostream> 2#include <string> 3 4using namespace std; 5 6int main() 7{ 8 string name; 9 int age; 10 string course; 11 12 cout << "Enter Name: "; 13 getline(cin, name); 14 15 cout << "Enter Age: "; 16 cin >> age; 17 18 cin.ignore(); 19 20 cout << "Enter Course: "; 21 getline(cin, course); 22 23 cout << "\nStudent Details\n"; 24 cout << "Name : " << name << endl; 25 cout << "Age : " << age << endl; 26 cout << "Course : " << course << endl; 27 28 return 0; 29}
Interview Questions
- What is the difference between
cinandcout? - What is the purpose of
<iostream>? - Explain the
<<and>>operators. - What is the difference between
endland\n? - Why is
getline()used? - What happens when
cinis used beforegetline()? - How do you fix the
cinandgetline()issue? - When should you use
getline()instead ofcin?
Summary
In this chapter, you learned:
- How to use
coutto display output. - How to use
cinto receive user input. - The purpose of the stream insertion (
<<) and extraction (>>) operators. - The difference between
endland\n. - How
getline()reads an entire line of text. - How to safely combine
cinandgetline()usingcin.ignore(). - Best practices and common mistakes when working with console input and output.
In the next chapter, you'll learn C++ Operators, including arithmetic, assignment, comparison, logical, and bitwise operators, with real-world examples and practice exercises.