Lesson 6 of 25

User Input & Output

Console I/O

Use cin for input and cout for output. They are part of the iostream library.

Example
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    // Output
    cout << "Hello!" << endl;        // with newline
    cout << "A" << " B" << " C\n"; // chaining

    // Input
    int age;
    cout << "Enter age: ";
    cin >> age;

    double price;
    cout << "Enter price: ";
    cin >> price;

    // Formatted output
    cout << fixed << setprecision(2);
    cout << "Price: $" << price << endl;

    cout << setw(10) << "Name" << setw(10) << "Score" << endl;
    cout << setw(10) << "Alice" << setw(10) << 95 << endl;

    return 0;
}