Need a little help!

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>

using namespace std;

int main ()
{
    int age;
    while (age!=0)
        cin >> age;
    
    if (age<55) cout << "You are young!" << endl;
    else if (age>=55 || age <100) cout << "You are middle aged!" << endl;
    else cout << "You have lived one century!" << endl;

    return 0;
}
Hi @binf,
i made it simple
as possible,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//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(),...

using namespace 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;
                else if(age>=55) //if age is greater or equal to 55
                        cout<<"\nYou are middle aged person"<<endl;
                else if(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!!!


EDIT: "libary" ->"library"; "lees"->"less"; xD
Last edited on
Thank you so much! Very helpful.. :D
you are welcome!
Topic archived. No new replies allowed.