I want to write a code where the user can enter undefined number of ages until input is 0,and for every age entered the program will show "You have lived a century" If the age is greater than or equal to 100.
"You are middle aged person" otherwise if age is greater than or equal to 55.
Otherwise output "You are young.” I wrote the code but it doesn't give the desired result.So how can I write it in order to show the words in "" immediately after every age the user enters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <stdio.h>
usingnamespace std;
int main ()
{
int age;
while (age!=0)
cin >> age;
if (age<55) cout << "You are young!" << endl;
elseif (age>=55 || age <100) cout << "You are middle aged!" << endl;
else cout << "You have lived one century!" << endl;
return 0;
}
//inp_age.cpp
//##
#include <iostream> //C++ standard input output library
//#include <stdio.h> <-- first off you do not need this library
//C language standard input output i.e. printf(), scanf(),...
usingnamespace std;
int main(){
int age=-1; //initialize your age variable
//otherwise holds garbage
cout<<"\nEnter age: ";
cin>>age;
while(age!=0){
if(age>=100) //if age is greater or equal to 100
cout<<"\nYou have lived a century"<<endl;
elseif(age>=55) //if age is greater or equal to 55
cout<<"\nYou are middle aged person"<<endl;
elseif(age<55) //this "conditional if" is not necessary
//because if is not greater or equal to 100 nor
//greater or equal to 55 then is less than 55
cout<<"\nYou are young"<<endl;
cout<<"\nEnter age: ";
cin>>age;
}//end while
cout<<"\nBYE!!!"<<endl;
return 0; //indicates success
}//end of main
Eyenrique-MacBook-Pro:Desktop Eyenrique$ ./inp_age
Enter age: 20
You are young
Enter age: 55
You are middle aged person
Enter age: 100
You have lived a century
Enter age: 0
BYE!!!