📖 Scenario: Imagine you want to create a simple program that asks a user for their age and then tells them how old they will be next year. To do this, the program needs to get information from the user (input) and then show the result back to the user (output).
🎯 Goal: Build a small C++ program that takes a user's age as input and outputs their age next year.
📋 What You'll Learn
Use int variable to store age
Use std::cin to get input from the user
Use std::cout to display output to the user
Add 1 to the input age to calculate next year's age
💡 Why This Matters
🌍 Real World
Programs often need to ask users for information and then show results or messages based on that information.
💼 Career
Understanding input and output is essential for writing any interactive software, from simple tools to complex applications.
Progress0 / 4 steps
1
Create an integer variable called age
Write a line of code to declare an integer variable named age.
C++
Hint
Use int age; to create the variable.
2
Get the user's age using std::cin
Write a line of code to read an integer from the user into the variable age using std::cin.
C++
Hint
Use std::cin >> age; to get input from the user.
3
Calculate next year's age and store in next_age
Create an integer variable called next_age and set it to age + 1.
C++
Hint
Use int next_age = age + 1; to calculate next year's age.
4
Print the result using std::cout
Write a line of code to print the text Your age next year will be: followed by the value of next_age using std::cout.
C++
Hint
Use std::cout << "Your age next year will be: " << next_age << std::endl; to print the message.
Try entering 30 as input to test.
Practice
(1/5)
1. Why do programs need input from users?
easy
A. To store data permanently
B. To display messages on the screen
C. To receive data and make decisions based on it
D. To run faster without errors
Solution
Step 1: Understand the role of input
Input allows a program to get data from the user, which it can use to perform tasks or make decisions.
Step 2: Differentiate input from output
Output is for showing results, not for receiving data, so input is needed to get user information.
Final Answer:
To receive data and make decisions based on it -> Option C
Quick Check:
Input = receive data [OK]
Hint: Input means getting data from users [OK]
Common Mistakes:
Confusing input with output
Thinking input makes program faster
Believing input stores data permanently
2. Which of the following is the correct way to output text in C++?
easy
A. cout >> "Hello";
B. cout << "Hello";
C. cin << "Hello";
D. input << "Hello";
Solution
Step 1: Recall C++ output syntax
In C++, output to the screen uses 'cout' with the insertion operator '<<'.
Step 2: Check each option
cout << "Hello"; uses 'cout << "Hello";' which is correct syntax for output.
Final Answer:
cout << "Hello"; -> Option B
Quick Check:
Output uses cout << [OK]
Hint: Output uses cout with << operator [OK]
Common Mistakes:
Using cin instead of cout for output
Reversing << and >> operators
Using undefined keywords like input
3. What will be the output of this C++ code?
int main() {
int age;
std::cin >> age;
std::cout << "Age: " << age << std::endl;
return 0;
}
If the user inputs 25?
medium
A. Age: 25
B. 25
C. Age: age
D. Error: missing output
Solution
Step 1: Understand input reading
The program reads an integer from the user and stores it in variable 'age'.
Step 2: Understand output statement
The program prints the string "Age: " followed by the value of 'age' and a newline.
Final Answer:
Age: 25 -> Option A
Quick Check:
Input 25 outputs 'Age: 25' [OK]
Hint: Input value prints after label 'Age:' [OK]
Common Mistakes:
Expecting just the number without label
Confusing variable name with output
Thinking code has syntax error
4. Identify the error in this C++ code snippet:
int main() {
int number;
std::cout << "Enter number: ";
std::cout >> number;
std::cout << "Number is " << number;
return 0;
}
The code should use 'cin >> number;' to read input, not 'cout >> number;'.
Final Answer:
Using >> operator with cout instead of << -> Option D
Quick Check:
cout uses <<, cin uses >> [OK]
Hint: Input uses cin >>, output uses cout << [OK]
Common Mistakes:
Mixing up cin and cout
Using wrong operators with cout
Ignoring operator direction
5. You want to write a program that asks the user for two numbers, adds them, and shows the result. Which steps correctly explain why input and output are required?
hard
A. Input gets numbers from user; output shows the sum
B. Input shows the sum; output gets numbers from user
C. Input stores the sum; output deletes numbers
D. Input and output are not needed for this task
Solution
Step 1: Understand input role in the program
The program needs input to receive the two numbers from the user to add.
Step 2: Understand output role in the program
After adding, the program uses output to display the sum to the user.
Final Answer:
Input gets numbers from user; output shows the sum -> Option A
Quick Check:
Input = get data, Output = show result [OK]
Hint: Input gets data; output shows results [OK]
Common Mistakes:
Confusing input and output roles
Thinking input shows results
Ignoring need for user data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.