#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
#include <limits>

// Helper function: clear input buffer and ignore error state
void clearCin() {
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

// Demo 1: Standard input/output (cin / cout) basics
void demoStandardIO() {
    std::cout << "\n========== 1. Standard Input/Output (cin / cout) ==========\n";

    int age;
    double height;
    std::string name;

    std::cout << "Please enter your name, age and height (e.g., Alice 25 1.68): ";
    std::cin >> name >> age >> height;

    std::cout << "You entered:\n";
    std::cout << "Name: " << name << "\nAge: " << age << "\nHeight: " << height << " meters\n";

    // Demo getline reading entire line with spaces
    clearCin(); // Clear remaining newline
    std::cout << "Please enter a line of text containing spaces: ";
    std::getline(std::cin, name);  // Reuse name variable
    std::cout << "The entire line you entered is: \"" << name << "\"\n";
}

// Demo 2: Formatted output (manipulators)
void demoFormattedOutput() {
    std::cout << "\n========== 2. Formatted Output (iomanip manipulators) ==========\n";

    int num = 255;
    double pi = 3.14159265358979;

    // Integer base conversion
    std::cout << "Decimal: " << std::dec << num << "\n";
    std::cout << "Hexadecimal: " << std::hex << num << "\n";
    std::cout << "Octal: " << std::oct << num << "\n";
    std::cout << std::dec; // Restore decimal

    // Floating point precision and format
    std::cout << std::fixed << std::setprecision(2);
    std::cout << "Fixed decimal places (2 digits): " << pi << "\n";
    std::cout << std::scientific << std::setprecision(4);
    std::cout << "Scientific notation (4 significant digits): " << pi << "\n";
    std::cout << std::defaultfloat; // Restore default

    // Width and fill
    std::cout << std::setw(10) << std::setfill('*') << 42 << "\n";
    std::cout << std::setw(10) << std::setfill(' ') << "Hello" << "\n";
}

// Demo 3: File input/output (ifstream / ofstream)
void demoFileIO() {
    std::cout << "\n========== 3. File Input/Output ==========\n";

    const std::string filename = "demo.txt";

    // Write to file
    std::ofstream outFile(filename);
    if (!outFile) {
        std::cerr << "Cannot create file " << filename << std::endl;
        return;
    }
    outFile << "Line 1: Hello, C++ I/O!\n";
    outFile << "Line 2: Number " << 100 << " and float " << 3.14159 << "\n";
    outFile.close();
    std::cout << "Written to file: " << filename << std::endl;

    // Read from file
    std::ifstream inFile(filename);
    if (!inFile) {
        std::cerr << "Cannot open file " << filename << std::endl;
        return;
    }
    std::string line;
    std::cout << "Reading file contents:\n";
    while (std::getline(inFile, line)) {
        std::cout << "  " << line << '\n';
    }
    inFile.close();
}

// Demo 4: String stream (stringstream)
void demoStringStream() {
    std::cout << "\n========== 4. String Stream (stringstream) ==========\n";

    // Build string using ostringstream
    std::ostringstream oss;
    oss << "Integer: " << 42 << ", Float: " << std::fixed << std::setprecision(3) << 9.99;
    std::string result = oss.str();
    std::cout << "String built by ostringstream: " << result << std::endl;

    // Parse string using istringstream
    std::string data = "Alice 25 160.5";
    std::istringstream iss(data);
    std::string name;
    int age;
    double height;
    if (iss >> name >> age >> height) {
        std::cout << "Parsed from string: Name=" << name << ", Age=" << age << ", Height=" << height << std::endl;
    } else {
        std::cout << "Parsing failed" << std::endl;
    }
}

// Demo 5: Error handling and state bits
void demoErrorHandling() {
    std::cout << "\n========== 5. Input Error Handling ==========\n";

    int value;
    std::cout << "Please enter an integer: ";
    if (std::cin >> value) {
        std::cout << "You entered integer: " << value << std::endl;
    } else {
        std::cout << "Input error! Not a valid integer." << std::endl;
        clearCin(); // Clear error state and remaining input
    }

    // Demo ignoring whitespace and reading single character
    std::cout << "Please enter a character (e.g., 'a'): ";
    char ch;
    std::cin >> ch;  // Skip whitespace
    std::cout << "The first non-whitespace character you entered is: '" << ch << "'" << std::endl;

    // Demo cin.get() reading characters including whitespace
    clearCin(); // Clear buffer
    std::cout << "Please enter a character (including space or enter): ";
    ch = std::cin.get();
    std::cout << "The character you entered (including possible whitespace) ASCII = " << int(ch) << " (" << ch << ")" << std::endl;
}

int main() {
    std::cout << "=== C++ Input/Output Methods Demo Program ===";

    // Demonstrate each feature in sequence
    demoStandardIO();
    demoFormattedOutput();
    demoFileIO();
    demoStringStream();
    demoErrorHandling();

    std::cout << "\nDemo complete.\n";
    return 0;
}

results matching ""

    No results matching ""