The <iostream> library in C++ is essential for handling input and output operations. Whether you're reading from the keyboard, writing to the screen, or logging errors, understanding how to use this library effectively is crucial for any C++ programmer.
In this tutorial, we'll explore various components of the <iostream> library, including cout, cin, cerr, clog, endl, flush, getline, put, and get. We'll also delve into formatted I/O using the <iomanip> library. By the end of this tutorial, you'll have a solid understanding of how to perform input and output operations in C++.
coutcout is used for standard output, typically the screen. It's part of the std namespace and is commonly used with the insertion operator (<<).
1#include <iostream>23int main() {4std::cout << "Hello, World!" << std::endl;5return 0;6}
Hello, World!
cincin is used for standard input, typically from the keyboard. It's also part of the std namespace and is commonly used with the extraction operator (>>).
1#include <iostream>23int main() {4int number;5std::cout << "Enter a number: ";6std::cin >> number;7std::cout << "You entered: " << number << std::endl;8return 0;9}
Enter a number: 42 You entered: 42
cerrcerr is used for error messages. Unlike cout, it does not use buffering, ensuring that error messages are displayed immediately.
1#include <iostream>23int main() {4std::cerr << "This is an error message!" << std::endl;5return 0;6}
This is an error message!
clogclog is similar to cout, but it also does not use buffering, making it suitable for logging purposes.
1#include <iostream>23int main() {4std::clog << "This is a log message!" << std::endl;5return 0;6}
This is a log message!
endl and flushendl inserts a newline character and flushes the output buffer, ensuring that all data is written to the destination.
1#include <iostream>23int main() {4std::cout << "Line 1";5std::cout << std::endl;6std::cout << "Line 2";7return 0;8}
Line 1 Line 2
flush also flushes the output buffer without inserting a newline character.
1#include <iostream>23int main() {4std::cout << "Part 1";5std::cout.flush();6std::cout << " Part 2";7return 0;8}
Part 1 Part 2
getlinegetline reads an entire line of input, including spaces, until it encounters a newline character.
1#include <iostream>2#include <string>34int main() {5std::string name;6std::cout << "Enter your full name: ";7std::getline(std::cin, name);8std::cout << "Hello, " << name << "!" << std::endl;9return 0;10}
Enter your full name: John Doe Hello, John Doe!
putput writes a single character to the output stream.
1#include <iostream>23int main() {4std::cout.put('H');5std::cout.put('e');6std::cout.put('l');7std::cout.put('l');8std::cout.put('o');9std::cout.put('!');10return 0;11}
Hello!
getget reads a single character from the input stream.
1#include <iostream>23int main() {4char ch;5std::cout << "Enter a character: ";6std::cin.get(ch);7std::cout << "You entered: " << ch << std::endl;8return 0;9}
Enter a character: A You entered: A
<iomanip>The <iomanip> library provides manipulators for formatting input and output operations.
setw sets the field width for subsequent output operations.
1#include <iostream>2#include <iomanip>34int main() {5int num = 42;6std::cout << "Number: " << std::setw(10) << num << std::endl;7return 0;8}
Number: 42
setprecision sets the number of digits to be displayed in floating-point numbers.
1#include <iostream>2#include <iomanip>34int main() {5double pi = 3.141592653589793;6std::cout << "PI: " << std::setprecision(5) << pi << std::endl;7return 0;8}
PI: 3.1416
fixed and scientific control the notation used for floating-point numbers.
1#include <iostream>2#include <iomanip>34int main() {5double pi = 3.141592653589793;6std::cout << "Fixed: " << std::fixed << pi << std::endl;7std::cout << "Scientific: " << std::scientific << pi << std::endl;8return 0;9}
Fixed: 3.141593 Scientific: 3.141593e+00
Let's create a complete program that demonstrates various input and output operations, including formatted I/O.
1#include <iostream>2#include <iomanip>3#include <string>45int main() {6std::string name;7int age;8double height;910std::cout << "Enter your full name: ";11std::getline(std::cin, name);1213std::cout << "Enter your age: ";14std::cin >> age;1516std::cout << "Enter your height (in meters): ";17std::cin >> height;1819std::cout << "20--- User Information ---21";22std::cout << "Name: " << name << std::endl;23std::cout << "Age: " << age << std::endl;24std::cout << "Height: " << std::fixed << std::setprecision(2) << height << " meters" << std::endl;2526return 0;27}
Enter your full name: John Doe Enter your age: 30 Enter your height (in meters): 1.75
--- User Information --- Name: John Doe Age: 30 Height: 1.75 meters
| Concept | Description |
|---|---|
cout | Standard output stream for displaying text on the screen. |
cin | Standard input stream for reading data from the keyboard. |
cerr | Error message stream, not buffered. |
clog | Logging stream, not buffered. |
endl | Inserts a newline and flushes the output buffer. |
flush | Flushes the output buffer without inserting a newline. |
getline | Reads an entire line of input, including spaces. |
put | Writes a single character to the output stream. |
get | Reads a single character from the input stream. |
<iomanip> | Provides manipulators for formatting I/O operations. |
setw | Sets the field width for subsequent output operations. |
setprecision | Sets the number of digits to be displayed in floating-point numbers. |
fixed | Forces floating-point numbers to use fixed notation. |
scientific | Forces floating-point numbers to use scientific notation. |
Now that you have a comprehensive understanding of input and output operations using the <iostream> library, it's time to explore file I/O with the <fstream> library. The next tutorial will cover how to read from and write to files in C++. Stay tuned!