Getting Started Lesson 3 of 17
C++ Input and Output — cout, cin and getline
Print values with cout and the << operator, read input with cin and >>, read full lines with getline, format decimals with fixed and setprecision, and align columns with setw.
C++ uses streams for input and output. Data "flows" out to the screen through cout, and flows in from the keyboard through cin. The arrows show the direction: << sends data out, >> pulls it in.
cout
#include <iostream>
using namespace std;
int main() {
int age = 16;
double average = 92.5;
cout << "Age: " << age << endl;
cout << "Average: " << average << "\n";
cout << "Sum: " << 7 + 8 << endl;
cout << "A" << "B" << "C" << endl;
return 0;
}
Output
Age: 16 Average: 92.5 Sum: 15 ABC
You can chain as many << as you like. Unlike C's printf, there are no format specifiers: cout knows the type of each value automatically.
endl and "\n" both end the line. endl also flushes the output immediately, which is slightly slower; "\n" is preferred in big loops.
cin
#include <iostream>
using namespace std;
int main() {
int qty;
double price;
cout << "Quantity: ";
cin >> qty;
cout << "Price: ";
cin >> price;
cout << "Total: " << qty * price << endl;
return 0;
}
Output
Quantity: 4 Price: 12.50 Total: 50
cin >> x reads one value and stores it in x, converting it to the right type. You can read several at once: cin >> a >> b >> c;. The user separates them with spaces or Enter.
Formatting decimals
cout printed "50" instead of "50.00". To control decimals, use fixed and setprecision from <iomanip>:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double total = 50;
double average = 88.4567;
cout << fixed << setprecision(2);
cout << "Total: " << total << endl;
cout << "Average: " << average << endl;
return 0;
}
Output
Total: 50.00 Average: 88.46
fixed << setprecision(2) stays in effect for all later output until you change it.
Aligning columns with setw
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << fixed << setprecision(2);
cout << left << setw(12) << "ITEM" << right << setw(5) << "QTY" << setw(10) << "AMOUNT" << "\n";
cout << left << setw(12) << "Pandesal" << right << setw(5) << 10 << setw(10) << 25.0 << "\n";
cout << left << setw(12) << "Kape" << right << setw(5) << 4 << setw(10) << 32.0 << "\n";
cout << left << setw(12) << "Itlog" << right << setw(5) << 6 << setw(10) << 54.0 << "\n";
return 0;
}
Output
ITEM QTY AMOUNT Pandesal 10 25.00 Kape 4 32.00 Itlog 6 54.00
setw(n) sets the width of the next item only. left and right choose the alignment.
Reading a full line: getline
cin >> name stops at the first space, so "Maria Clara" becomes just "Maria". Use getline:
#include <iostream>
#include <string>
using namespace std;
int main() {
string fullName;
int age;
cout << "Age: ";
cin >> age;
cin.ignore(); // skip the Enter left by cin >>
cout << "Full name: ";
getline(cin, fullName);
cout << fullName << " is " << age << "." << endl;
return 0;
}
Output
Age: 16 Full name: Maria Clara Santos Maria Clara Santos is 16.
Checking for bad input
#include <iostream>
using namespace std;
int main() {
int qty;
cout << "How many? ";
if (!(cin >> qty)) {
cout << "That's not a number!" << endl;
return 1;
}
cout << "You ordered " << qty << endl;
return 0;
}
Output
How many? sampu That's not a number!
When cin can't convert the input, it goes into a "failed" state and the expression is false. You'll learn to recover and ask again in the loops lesson.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04