Read a person's first name and age, then greet them and say how old they will be next year.
What to build
A program that greets someone and works out their age next year.
Requirements
- Read the name and the age with
cin. - Print the whole message with one chain of
<<. - End the line with
endl.
Input
One line with a name (one word) and an age (a whole number from 0 to 120).
Output
Hello, NAME! Next year you will be AGE. where AGE is one more than the age read.
Constraints
The name is one word, and the age is from 0 to 120.
Example
Input:
Nadia 19
Output:
Hello, Nadia! Next year you will be 20.
Why it works
cin >> reads each value into a variable of the right type, so the age can be used in arithmetic straight away.
Examples
Example 1 · Input
Nadia 19
Expected output
Hello, Nadia! Next year you will be 20.
Example 2 · Input
Rahim 0
Expected output
Hello, Rahim! Next year you will be 1.
Hints
Hint 1
Read a string and an int with cin >> name >> age;
Hint 2
Print several pieces in one statement with << between them, and end with endl.
Solution
Show one correct solution (try it yourself first)
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
int age;
cin >> name >> age;
cout << "Hello, " << name << "! Next year you will be " << age + 1 << "." << endl;
return 0;
}