cpp

Input Output

03_Input_Output⚙️
/**
 * ============================================================
 * C++ INPUT/OUTPUT BASICS
 * ============================================================
 * 
 * This file covers fundamental I/O operations in C++:
 * - cout for output
 * - cin for input
 * - cerr for error messages
 * - getline for string input
 * 
 * Compile: g++ -std=c++17 -Wall 01_basic_io.cpp -o basic_io
 * Run: ./basic_io
 * 
 * ============================================================
 */

#include <iostream>   // For cout, cin, cerr, clog
#include <string>     // For std::string and getline

using std::cout;
using std::cin;
using std::cerr;
using std::clog;
using std::endl;
using std::string;

int main() {
    cout << "============================================" << endl;
    cout << "     C++ INPUT/OUTPUT DEMONSTRATION" << endl;
    cout << "============================================" << endl << endl;

    // ========================================================
    // PART 1: OUTPUT WITH cout
    // ========================================================
    
    cout << "--- PART 1: OUTPUT (cout) ---" << endl;
    
    // Basic output
    cout << "Hello, World!" << endl;
    
    // Chaining multiple outputs
    cout << "This " << "is " << "chained " << "output." << endl;
    
    // Output different data types
    int age = 25;
    double height = 5.9;
    char grade = 'A';
    bool isStudent = true;
    
    cout << "Age: " << age << endl;
    cout << "Height: " << height << " feet" << endl;
    cout << "Grade: " << grade << endl;
    cout << "Is Student: " << isStudent << endl;  // Prints 1 for true
    cout << "Is Student: " << std::boolalpha << isStudent << endl;  // Prints "true"
    
    // Multiple values on same line
    cout << "Age: " << age << ", Height: " << height << ", Grade: " << grade << endl;
    
    // Using \n vs endl
    cout << "Line 1\n";        // \n: just newline
    cout << "Line 2" << endl;   // endl: newline + flush buffer
    
    // Special characters
    cout << "\nSpecial Characters:" << endl;
    cout << "Tab:\tTabbed text" << endl;
    cout << "Quote: \"Hello\"" << endl;
    cout << "Backslash: \\" << endl;
    cout << "Newline in string:\nSecond line" << endl;
    
    cout << endl;

    // ========================================================
    // PART 2: INPUT WITH cin
    // ========================================================
    
    cout << "--- PART 2: INPUT (cin) ---" << endl;
    
    // Input integer
    int userAge;
    cout << "Enter your age: ";
    cin >> userAge;
    cout << "You entered: " << userAge << endl;
    
    // Input floating point
    double userHeight;
    cout << "Enter your height (in feet): ";
    cin >> userHeight;
    cout << "Your height: " << userHeight << " feet" << endl;
    
    // Input character
    char initial;
    cout << "Enter your first initial: ";
    cin >> initial;
    cout << "Your initial: " << initial << endl;
    
    // Multiple inputs in one line
    int x, y;
    cout << "Enter two numbers separated by space: ";
    cin >> x >> y;
    cout << "You entered: " << x << " and " << y << endl;
    cout << "Sum: " << x + y << endl;
    
    cout << endl;

    // ========================================================
    // PART 3: STRING INPUT
    // ========================================================
    
    cout << "--- PART 3: STRING INPUT ---" << endl;
    
    // Clear the input buffer before getline
    cin.ignore();  // Ignore the newline left in buffer
    
    // Using cin >> for single word
    string firstName;
    cout << "Enter your first name: ";
    cin >> firstName;  // Stops at whitespace
    cout << "Hello, " << firstName << "!" << endl;
    
    // Clear buffer again
    cin.ignore();
    
    // Using getline for full line (including spaces)
    string fullName;
    cout << "Enter your full name: ";
    getline(cin, fullName);
    cout << "Full name: " << fullName << endl;
    
    // getline with custom delimiter
    string sentence;
    cout << "Enter a sentence (end with .): ";
    getline(cin, sentence, '.');
    cout << "You said: " << sentence << endl;
    
    cout << endl;

    // ========================================================
    // PART 4: ERROR OUTPUT (cerr and clog)
    // ========================================================
    
    cout << "--- PART 4: ERROR OUTPUT ---" << endl;
    
    // cerr - unbuffered, for error messages
    cerr << "This is an error message (cerr)" << endl;
    
    // clog - buffered, for log messages
    clog << "This is a log message (clog)" << endl;
    
    // Practical example
    int divisor = 0;
    if (divisor == 0) {
        cerr << "ERROR: Division by zero is not allowed!" << endl;
    }
    
    cout << endl;

    // ========================================================
    // PART 5: INPUT VALIDATION
    // ========================================================
    
    cout << "--- PART 5: INPUT VALIDATION ---" << endl;
    
    int number;
    cout << "Enter a number: ";
    
    // Check if input was successful
    if (cin >> number) {
        cout << "Valid input: " << number << endl;
    } else {
        cerr << "Invalid input! Not a number." << endl;
        cin.clear();  // Clear error flags
        cin.ignore(10000, '\n');  // Discard bad input
    }
    
    cout << endl;

    // ========================================================
    // SUMMARY
    // ========================================================
    
    cout << "============================================" << endl;
    cout << "SUMMARY:" << endl;
    cout << "- cout: Standard output (console)" << endl;
    cout << "- cin: Standard input (keyboard)" << endl;
    cout << "- cerr: Error output (unbuffered)" << endl;
    cout << "- clog: Log output (buffered)" << endl;
    cout << "- getline(): Read entire lines" << endl;
    cout << "- <<: Insertion operator (output)" << endl;
    cout << "- >>: Extraction operator (input)" << endl;
    cout << "============================================" << endl;

    return 0;
}

// ============================================================
// KEY CONCEPTS:
// ============================================================
/*
 * 1. cout << data;        - Output to console
 * 2. cin >> variable;     - Input from keyboard
 * 3. getline(cin, str);   - Read entire line including spaces
 * 4. cerr <<              - Error messages (unbuffered)
 * 5. clog <<              - Log messages (buffered)
 * 6. cin.ignore();        - Clear input buffer
 * 7. cin.clear();         - Clear error flags
 */

// ============================================================
// EXERCISES:
// ============================================================
/*
 * 1. Write a program that asks for the user's name, age, and 
 *    favorite color, then displays a summary
 * 
 * 2. Create a simple calculator that takes two numbers and an
 *    operator (+, -, *, /) from the user
 * 
 * 3. Write a program that reads a paragraph using getline and
 *    counts the number of characters
 * 
 * 4. Create a program with input validation that only accepts
 *    numbers between 1 and 100
 */
Input Output - C++ Tutorial | DeepML