Standard Input/Output in C++
Standard Input/Output (I/O) is the process of reading from and writing to the console (also known as the terminal). In C++, you can use the cin and cout objects to perform standard I/O. Here's an example of using standard I/O in C++:
c++
#include <iostream>
int main() {
std::string name;
int age;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Hello, " << name << "! You are " << age << " years old." << std::endl;
return 0;
}
In this example, we're using std::cin to read a string and an integer from the console, and std::cout to print a message to the console using the values entered by the user. When you run this program, you should see the message "Enter your name: " printed to the console, followed by a prompt for the user to enter their name. After the user enters their name and presses enter, the message "Enter your age: " is printed to the console, followed by a prompt for the user to enter their age. After the user enters their age and presses enter, the message "Hello, [name]! You are [age] years old." is printed to the console, where [name] and [age] are replaced with the values entered by the user.
No comments:
Post a Comment