Im trying to create a simple age calculator that has cout of custom messages, Im trying to use multiple "if" statements. It often shows nothing in the console when i run it. why is that so? I believe i covered most of the ages, do i need an else statement?
#include <iostream>
usingnamespace std;
int main(){
int age;
cout<<"Please enter an age and press Enter";
cin>>age;
if(age>1&&age<15)
{
cout<<"You're a child!";}
if(age>15&&age<19){
cout<<"You're a teenager!";
}
if(age>19&&age<30){
cout<<"Youre an adult!";
}
if(age>30&&age<45){
cout<<"You're getting old!";
}
if(age>45&&age<65){
cout<<"you're over the hill!";
}
if(age>65&&age<85){
cout<<"Holy Moly You're almost to 100!";
}
if(age>85&&age<100){
cout<<"Keep going!";
}
if(age>100&&age<400)
{cout<<"You are Not Human!!!";
}
if(age>1&&age<-1){
cout<<"You dont exist!";}
return 0;
}
Your program is good.but try using a series of IF...ELSE statement instead of IF only.
e.g
if(age>1&&<15)
{cout<<"......."};
else is(...)
{"....."};
//continue using else if until the last expression then you can use single else
#include <iostream>
usingnamespace std;
int main(){
int age = 0; // generally a good idea to init varaibles
cout<<"Please enter an age and press Enter";
cin>>age;
// -ve ages not handled
if(age == 0) {
cout<<"You dont exist!";
} elseif(age<15) { // 13 is a teenager?
cout<<"You're a child!";
} elseif(age<19) { // no need to check lower bound as already handled
cout<<"You're a teenager!";
} elseif(age<30){
cout<<"You're an adult!";
} elseif(age<45){
cout<<"You're getting old!";
} elseif(age<65){
cout<<"you're over the hill!";
} elseif(age<85) {
cout<<"Holy Moly You're almost to 100!";
} elseif(age<100){
cout<<"Keep going!";
} elseif(age>100&&age<400) {
cout<<"You are Not Human!!!";
}
// Jeanne Calment of France (1875–1997), died at age 122 years
// And what about "people" 400 and older??
return 0;
}