In C++, programs can interact with users by receiving input from the keyboard. While cout is used to display output, cin allows the program to capture input using the extraction operator (>>).
Reading User Input
To prompt a user to enter a value, use cin along with a variable to store the input.
Example
#include <iostream>
using namespace std;
int main() {
int engineCC;
cout << "Enter engine capacity (cc): "; // Asking for user input
cin >> engineCC; // Storing the input
cout << "Your engine capacity is: " << engineCC;
return 0;
}
Explanation:
coutdisplays a message asking the user to enter a number.cin >> engineCC;stores the user’s input in the variableengineCC.- The value entered is then displayed using
cout.
Understanding cout and cin
cout(pronounced “see-out”) → Displays information using the insertion operator (<<).cin(pronounced “see-in”) → Captures input using the extraction operator (>>).
Creating a Simple Calculator
Let’s build a program that asks the user for two numbers, adds them, and displays the result.
Example
#include <iostream>
using namespace std;
int main() {
int speedA, speedB;
int totalSpeed;
cout << "Enter first speed: ";
cin >> speedA;
cout << "Enter second speed: ";
cin >> speedB;
totalSpeed = speedA + speedB;
cout << "Total combined speed is: " << totalSpeed;
return 0;
}
Explanation:
- The user enters two values (
speedAandspeedB). - The program adds them and displays the result.


