The <iostream> library in C++ provides a convenient way to perform input and output operations. By including this library in your C++ program, you can easily read input from the user and display output on the screen.
Standard Input with cin
The cin
object in the <iostream> library is used to read input from the user. You can use cin
to read different types of data, such as integers, characters, or strings.
Here’s an example of how to use cin
to read an integer from the user:
#include <iostream>
int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;
std::cout << "You entered: " << number << std::endl;
return 0;
}
In the above code, the user is prompted to enter a number using std::cout
, and the input is then stored in the number
variable using std::cin
. Finally, the entered number is displayed back to the user using std::cout
.
Standard Output with cout
The cout
object in the <iostream> library is used to display output on the screen. You can use cout
to display different types of data, such as integers, characters, or strings.
Here’s an example of how to use cout
to display a message on the screen:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
In the above code, Hello, World!
is displayed on the screen using std::cout
. The <<
operator is used to output the message, and std::endl
is used to insert a newline character.
Conclusion
The <iostream> library in C++ provides a simplified way to perform input and output operations. With the cin
object, you can easily read input from the user, and with the cout
object, you can display output on the screen. By including this library in your C++ program, you can streamline the input and output process.
References: