Introduction
Input and output let a program talk with people. Input lets the program get information from users, and output shows results back to them.
Input and output let a program talk with people. Input lets the program get information from users, and output shows results back to them.
#include <iostream> int main() { int number; std::cout << "Enter a number: "; // output std::cin >> number; // input std::cout << "You entered: " << number << std::endl; // output return 0; }
std::cout is used to show output on the screen.
std::cin is used to get input from the user.
#include <iostream> #include <string> int main() { std::string name; std::cout << "What is your name? "; std::cin >> name; std::cout << "Hello, " << name << "!" << std::endl; return 0; }
#include <iostream> int main() { int a, b; std::cout << "Enter two numbers: "; std::cin >> a >> b; std::cout << "Sum is: " << (a + b) << std::endl; return 0; }
This program asks the user for their age and then shows it back to them.
#include <iostream> int main() { int age; std::cout << "Enter your age: "; std::cin >> age; std::cout << "You are " << age << " years old." << std::endl; return 0; }
Without input, programs cannot get information from users.
Without output, users cannot see what the program did.
Input and output make programs interactive and useful.
Input lets programs receive data from users.
Output lets programs show results to users.
Both are needed for programs to communicate with people.
int main() {
int age;
std::cin >> age;
std::cout << "Age: " << age << std::endl;
return 0;
}25?int main() {
int number;
std::cout << "Enter number: ";
std::cout >> number;
std::cout << "Number is " << number;
return 0;
}Pass the test and this pattern moves unknown