I just compiled a program using these instructions:
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.
Now I have not downloaded std_lib_facilities.h but I assume the header files that I include with the program should be sufficient.
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
usingnamespace std;
int main()
{
int age;
cout<<"Enter your age";
cin>>age;
cout<<"I hear you just had a birthday and you are << age << years old :\n";
if (age<=0 or age>=110) cout<<"You are kidding!:\n";
}
Now I can compile the program but it appears like this:
Enter your age-4
I hear you just had a birthday and you are << age << years old :
You are kidding!:
cout<<"I hear you just had a birthday and you are << age << years old :\n";
should be cout<<"I hear you just had a birthday and you are "<< age << " years old :\n";
If you only want the "I hear you just had a birthday..." line to print out if they entered between 1 and 109 then you need to put that condition in the code. Right now you print that out regardless of what was entered in age.
You can use an if else statement to do one thing in one condition and something else in the opposite of that condition...
if ((age > 0 or age <110)
output the "I hear you just had a birthday... " line
else
output the "You're kidding!" line
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
usingnamespace std;
int main()
{
int age;
cout<<"Enter your age";
cin>>age;
if (age>0 or age<=110) cout<<"I hear you just had a birthday and you are "<< age <<"years old!";
else
cout<<"You are kidding!";
}
There was a simple Mistake. It will work fine now :) ... Whenever we consider a number in a range we use && operator rather than || .... so try this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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!" << endl;
else
cout<<"You are kidding!"<<endl;
return 0;
}