Prompt the user to enter the age of the recipient and assign it to an int variable age. Have your program write "I hear you just had a birthday and you are age years old." If age is 0 or less or 110 or more, call simple_error("you are kidding!") using simple_error() from std_lib_facilities.h.
Add this to your letter:
If your friend is under 12, write "Next year you will be age+1."
If your friend is 17, write "Next year you will be able to vote."
If your friend is over 70, write "I hope you are enjoying retirement."
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
usingnamespace std;
int main()
{
int age;
cout<<"Enter your age";
cin>>age;
if (age>0 && age<=110) cout<<"I hear you just had a birthday and you are "<< age << "years old!";
elseif (age<12) cout<<"Next year you will be (<< age << +1)";
if (age==17) cout<<"Next year you will be able to vote";
if (age>70) cout<<"I hope you are enjoying retirement";
else
cout<<"You are kidding!";
}
And not quite working, any suggestions? I can though compile the program.
if (age>0 && age<=110)
{
cout<<"I hear you just had a birthday and you are "<< age << "years old!";
}
elseif (age<12)
{
cout<<"Next year you will be (<< age << +1)";
}
if (age==17)
{
cout<<"Next year you will be able to vote";
}
if (age>70)
{
cout<<"I hope you are enjoying retirement";
}
else
{
cout<<"You are kidding!";
}
Can you now see why the logic of your code is not quite what you do expect?
if (age>0 && age<=110) {
cout<<"I hear you just had a birthday and you are "<< age << "years old!\n";
if (age<12) {
cout<<"Next year you will be " << age + 1 << '\n';
}
if (age==17) {
cout<<"Next year you will be able to vote\n";
}
if (age>70) {
cout<<"I hope you are enjoying retirement\n";
}
} else {
cout<<"You are kidding!\n";
}
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
usingnamespace std;
int main()
{
int age;
cout<<"Enter your age";
cin>>age;
if (age>0 && age<=110)
{
cout<<"I hear you just had a birthday and you are "<< age << "years old!";
}
elseif (age<12)
{
cout<<"Next year you will be" << age +1 ;
}
elseif (age==17)
{
cout<<"Next year you will be able to vote";
}
elseif (age>70)
{
cout<<"I hope you are enjoying retirement";
} else
{
cout<<"You are kidding!";
}
}
Still a misunderstanding of the problem. According to you Problem mentioned above. You should not use " else if " since it will be ignored whenever a single "if" statement is true. Try this:
int main()
{
int age = 0;
cout << "Enter your age";
cin >> age;
if (age>0 && age<=110)
{
cout << "I hear you just had a birthday and you are " << age << "years old!";
//Once The Age is valid! Check Additional Information Here
if (age<12)
{
cout<<"Next year you will be" << age +1 ;
}
elseif (age==17)
{
cout<<"Next year you will be able to vote";
}
elseif (age>70)
{
cout<<"I hope you are enjoying retirement";
}
}
//Once The Age is Invalid - No need to check anything now
else
cout<<"You are kidding!";
//Next Line + endl Clears the input buffer too
cout << endl;
return 0;
}
No. Read the task description. MiiNiPaa's code does what is required, but yours doesn't. One of the key things in programming is to learn to see the subtle differences in the code and how they change the logical outcome.
@pacman169: I did not change your code at all. I just used different style.
This thread now has four blocks of code that look different. Two of them have same logic, so only three different logic, but the three are truly different.