Introduction
We use cin to get information from the user and cout to show information on the screen.
We use cin to get information from the user and cout to show information on the screen.
#include <iostream> using namespace std; int main() { int number; cout << "Enter a number: "; cin >> number; cout << "You entered: " << number << endl; return 0; }
cout is used with the << operator to send output to the screen.
cin is used with the >> operator to get input from the user.
#include <iostream> using namespace std; int main() { int age; cout << "Enter your age: "; cin >> age; cout << "Your age is " << age << endl; return 0; }
#include <iostream> #include <string> using namespace std; int main() { string name; cout << "Enter your name: "; cin >> name; cout << "Hello, " << name << "!" << endl; return 0; }
#include <iostream> using namespace std; int main() { double price; cout << "Enter price: "; cin >> price; cout << "Price is $" << price << endl; return 0; }
This program asks the user to enter two numbers, adds them, and shows the result.
#include <iostream> using namespace std; int main() { int num1, num2; cout << "Enter first number: "; cin >> num1; cout << "Enter second number: "; cin >> num2; int sum = num1 + num2; cout << "Sum is: " << sum << endl; return 0; }
Use cin carefully: if the user types the wrong type (like letters instead of numbers), it can cause errors.
To read a full line with spaces, use getline(cin, variable) instead of cin >> variable.
cin gets input from the user.
cout shows output to the screen.
Use << with cout and >> with cin.
cin do in a C++ program?cincin is used to get input from the user during program execution.coutcout is used to show output, not input.cin = input [OK]num from the user?cin with the extraction operator >>.cin >> num; is the correct syntax for input. Options using cout are for output, not input. The option with cin << num; uses the wrong operator <<.5?
int num; std::cin >> num; std::cout << num + 3;
num becomes 5.num + 3 which is 5 + 3 = 8.int age; std::cin << age; std::cout >> age;
cin should use >> to get input, but code uses <<. Similarly, cout should use << to output, but code uses >>.age is declared and semicolons are present, so no issues there.cin >> a >> b; which correctly reads two integers.cout << a + b; which correctly prints the sum.Pass the test and this pattern moves unknown