#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
#include <limits>
void clearCin() {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
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";
clearCin();
std::cout << "Please enter a line of text containing spaces: ";
std::getline(std::cin, name);
std::cout << "The entire line you entered is: \"" << name << "\"\n";
}
void demoFormattedOutput() {
std::cout << "\n========== 2. Formatted Output (iomanip manipulators) ==========\n";
int num = 255;
double pi = 3.14159265358979;
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;
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;
std::cout << std::setw(10) << std::setfill('*') << 42 << "\n";
std::cout << std::setw(10) << std::setfill(' ') << "Hello" << "\n";
}
void demoFileIO() {
std::cout << "\n========== 3. File Input/Output ==========\n";
const std::string filename = "demo.txt";
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;
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();
}
void demoStringStream() {
std::cout << "\n========== 4. String Stream (stringstream) ==========\n";
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;
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;
}
}
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();
}
std::cout << "Please enter a character (e.g., 'a'): ";
char ch;
std::cin >> ch;
std::cout << "The first non-whitespace character you entered is: '" << ch << "'" << std::endl;
clearCin();
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 ===";
demoStandardIO();
demoFormattedOutput();
demoFileIO();
demoStringStream();
demoErrorHandling();
std::cout << "\nDemo complete.\n";
return 0;
}