C++ Input and Output
Input and output (I/O) in C++ are essential for interacting with users, files, or other programs. The C++ Standard Library provides several features for handling I/O operations, primarily through the <iostream>
header, which includes functionalities for standard input and output through the console. Here’s a comprehensive overview of how input and output work in C++:
1. Standard Input and Output Streams
std::cin
: Used for input (typically from the keyboard).std::cout
: Used for output (typically to the console).std::cerr
: Used for error output (unbuffered).std::clog
: Used for logging output (buffered).
2. Including the I/O Library
To use standard I/O in C++, you need to include the <iostream>
header.
#include <iostream>
3. Output with std::cout
The std::cout
object is used to display output to the console. You can use the insertion operator (<<
) to send data to the output stream.
Syntax:
std::cout << "Message" << variable;
Example:
#include <iostream> int main() { int age = 25; std::cout << "Hello, World!" << std::endl; // Outputs: Hello, World! std::cout << "Age: " << age << std::endl; // Outputs: Age: 25 return 0; }
4. Input with std::cin
The std::cin
object is used to receive input from the user. You can use the extraction operator (>>
) to read data from the input stream.
Syntax:
std::cin >> variable;
Example:
#include <iostream> int main() { int age; std::cout << "Enter your age: "; std::cin >> age; // Reads input from the user std::cout << "You entered: " << age << std::endl; // Outputs the age return 0; }
5. Multiple Inputs and Outputs
You can chain multiple input and output operations together.
- Example:
#include <iostream> int main() { int a, b; std::cout << "Enter two numbers: "; std::cin >> a >> b; // Reads two integers std::cout << "Sum: " << (a + b) << std::endl; // Outputs their sum return 0; }
6. Formatted Input and Output
You can format the output using manipulators like std::setw()
, std::setprecision()
, and others from the <iomanip>
header.
- Example:
#include <iostream> #include <iomanip> // Include for manipulators int main() { double number = 123.456789; std::cout << std::fixed << std::setprecision(2); // Set precision to 2 std::cout << "Formatted number: " << number << std::endl; // Outputs: 123.46 return 0; }
7. Error Handling with std::cerr
and std::clog
std::cerr
is used for printing error messages. It is unbuffered, meaning output appears immediately.std::clog
is buffered, which means output may be delayed until the buffer is flushed.Example:
#include <iostream> int main() { std::cerr << "Error: Invalid input!" << std::endl; // Outputs error message return 0; }
8. File Input and Output
For file operations, C++ uses file stream classes provided by the <fstream>
header.
Basic Classes:
std::ifstream
: For reading from files.std::ofstream
: For writing to files.std::fstream
: For both reading and writing.
Example of Writing to a File:
#include <iostream> #include <fstream> int main() { std::ofstream outfile("example.txt"); // Open a file for writing if (outfile.is_open()) { outfile << "Hello, file!" << std::endl; // Write to file outfile.close(); // Close the file } else { std::cerr << "Unable to open file!" << std::endl; } return 0; }
Example of Reading from a File:
#include <iostream> #include <fstream> #include <string> int main() { std::ifstream infile("example.txt"); // Open a file for reading std::string line; if (infile.is_open()) { while (getline(infile, line)) { // Read line by line std::cout << line << std::endl; // Output each line } infile.close(); // Close the file } else { std::cerr << "Unable to open file!" << std::endl; } return 0; }