Input and Output in C++
Input and output are the most basic forms of communication between a program and the user. C++ provides a simple and powerful way to display information on the screen and receive data from the keyboard using the iostream library.
The iostream Library
To use input and output in C++, include the <iostream> header file at the top of the program. This file provides two main objects:
cout— Used to output (print) data to the console.cin— Used to input (read) data from the keyboard.
Output Using cout
cout stands for character output. The << operator (called the insertion operator) is used to send data to the output.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
Output:
Hello, World!Printing Multiple Items:
int age = 20;
string name = "Raj";
cout << "Name: " << name << ", Age: " << age << endl;
Output:
Name: Raj, Age: 20New Line — endl vs
Both endl and
move the output to a new line. The difference is:
endl— Moves to new line AND flushes the output buffer (slightly slower).— Just moves to a new line (faster, preferred in loops).
cout << "Line 1" << endl;
cout << "Line 2" << "
";
cout << "Line 3
";
Output:
Line 1
Line 2
Line 3Input Using cin
cin stands for character input. The >> operator (extraction operator) reads data from the keyboard and stores it in a variable.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
cout << "You entered: " << number << endl;
return 0;
}
Sample Run:
Enter a number: 42
You entered: 42Reading Multiple Inputs
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Sum = " << a + b << endl;
return 0;
}
Sample Run:
Enter two numbers: 5 7
Sum = 12Reading Strings with cin
When using cin to read a string, it reads only up to the first space. For reading a full line (including spaces), use getline().
#include <iostream>
#include <string>
using namespace std;
int main() {
string fullName;
cout << "Enter your full name: ";
getline(cin, fullName);
cout << "Hello, " << fullName << "!" << endl;
return 0;
}
Sample Run:
Enter your full name: Priya Sharma
Hello, Priya Sharma!Formatting Output with setw and fixed
For formatted output (like aligning columns or controlling decimal places), include <iomanip>:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double price = 49.9;
cout << fixed << setprecision(2);
cout << "Price: " << price << endl;
return 0;
}
Output:
Price: 49.90Escape Sequences
Escape sequences are special characters that start with a backslash \ and produce formatting effects in output:
| Escape Sequence | Meaning |
|---|---|
| New line |
| Horizontal tab |
\ | Prints a backslash |
" | Prints a double quote |
| Carriage return |
cout << "Name: Alice
";
cout << "She said: "Hello!"" << endl;
Output:
Name: Alice
She said: "Hello!"A Complete Input/Output Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
int age;
cout << "Enter your name: ";
cin >> name;
cout << "Enter your age: ";
cin >> age;
cout << "
--- Profile ---
";
cout << "Name : " << name << "
";
cout << "Age : " << age << "
";
return 0;
}
Sample Run:
Enter your name: Sam
Enter your age: 22
--- Profile ---
Name : Sam
Age : 22Key Takeaways
cout <<is used to print output to the screen.cin >>is used to read input from the keyboard.endlandboth add a new line;is faster.- Use
getline()to read a full line of text including spaces. - Escape sequences like
andcontrol text formatting.
