code prob

Hi, it is prog. Ask the user for two user's ages and indicate who is older; behave differently if both are over 100. i cannot write code for
behave differently if both are over 100.
plz help me
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
  #include <iostream>

using namespace std;

int main()
{
   string userAge1;
   string userAge2;
   string limit;
   limit = 100;
   cout << "Please enter user age1:";
   cin >> userAge1;
   cout << "Please enter user age2:";
   cin >> userAge2;
   if (userAge1 > limit)
       {

         cout << "You both are over 100\n";
       }

   else if (userAge1 > userAge2)
       {
       cout << "User 1 is older than User 2.\n";
       }

   else
       {
         cout << "User 2 is older than User 1.\n";
       }

    return 0;
}
Last edited on
&& is the and operator. || is the or operator. More in-depth descriptions can be found here: http://www.cplusplus.com/doc/tutorial/operators/

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
#include <iostream>
using namespace std;

int main()
{
    //declare variables
    int ageOne = 0;
    int ageTwo = 0;
    
    //enter input items
    cout << "Enter age one: ";
    cin >> ageOne;
    cout << "Enter age two: ";
    cin >> ageTwo;
    
    //determine output
    if (ageOne > 100 || ageTwo > 100)
    {
        //display message
        if (ageOne > 100 && ageTwo > 100)
            cout << "You are both over 100" << endl;
        else if (ageOne > 100)
            cout << "Person one is older than 100 and person two is " << ageTwo << endl;
        else if (ageTwo > 100)
            cout << "Person two is older than 100 and person one is " << ageOne << endl;
        //end if
    }
    else
    {
        //display message
        if (ageOne > ageTwo)
            cout << "Person one is older than person two" << endl;
        else if (ageOne < ageTwo)
            cout << "Person one is younger than person two" << endl;
        else
            cout << "Person one is as old as person two" << endl;
        //end if
    } //end if
} //end of main function 
Last edited on
Thanks
Topic archived. No new replies allowed.