In C++, input and output (I/O) operations are fundamental for interacting with users and handling data. These operations utilize streams, which provide a way to process sequences of bytes.
1. Streams: A stream is a flow of data. In C++, streams are used to handle input and output operations. There are different types of streams for different purposes:
• Input Stream: Handles data coming into the program (e.g., std::cin).
• Output Stream: Handles data going out of the program (e.g., std::cout).
2. Stream Operations:
• Input Operation: Reads data from an input stream.
• Output Operation: Writes data to an output stream.
• Purpose: Defines objects for standard I/O operations.
‣ std::cin: Standard input stream (e.g., keyboard input).
‣ std::cout: Standard output stream (e.g., console output).
‣ std::cerr: Standard error stream (e.g., error messages).
• Purpose: Provides facilities for formatted I/O operations.
‣ std::setw(int width): Sets the width of the next input/output field.
‣ std::setprecision(int precision): Sets the decimal precision for floating-point numbers.
• Purpose: Handles file input and output.
‣ std::ifstream: Input file stream (for reading files).
‣ std::ofstream: Output file stream (for writing to files).
‣ std::fstream: File stream (for both reading and writing).
• Definition: cout is an instance of the ostream class and is used for outputting data to the standard output device, typically the console.
• Header File: <iostream>
• Operator: The stream insertion operator (<<) is used with cout to send data to the output stream.
• Buffering: cout is buffered, meaning that output is collected in a buffer and written to the console in chunks rather than character-by-character. This improves performance, especially with multiple output operations.
#include <iostream>
using namespace std;
int main() {
int age = 25;
double height = 5.9;
cout << "Age: " << age << std::endl;
cout << "Height: " << height << " feet" <<
std::endl;
return 0;
}
Age: 25
Height: 5.9 feet
‣ Definition: cin is an instance of the istream class and is used for inputting data from the standard input device, typically the keyboard.
‣ Header File: <iostream>
‣ Operator: The stream extraction operator (>>) is used with cin to extract data from the input stream and store it in a variable.
‣ Buffering: cin is also buffered, which means input is collected in a buffer and processed in chunks. Input operations are typically blocking, meaning the program waits for the user to provide input before continuing.
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "You entered: " << age << std::endl;
return 0;
}
Enter your age: 10 You entered: 10
• Definition: endl is a manipulator of the ostream class that inserts a newline character (\n) into the output stream and flushes the stream.
• Header File: <iostream>
• Function: endl not only adds a newline character but also ensures that the buffer is flushed immediately. This is useful for ensuring that output is displayed promptly.
#include <iostream>
using namespace std;
int main() {
cout << "Line 1" << std::endl;
cout << "Line 2" << std::endl;
cout << "Line 3" << std::endl;
return 0;
}
Line 1 Line 2 Line 3