age calculator

hi i am new to programming ans i need help with this question can someone please help me out

Ernie and Bert are arguing about which of them is older, and they need your help to figure it out. Write a C++ program that asks the user for the birthdays of both Ernie and Bert, and responds by indicating which person is older (or if they are the same age). Here's a demonstration, copy/pasted from the Console window.

Assignment 3 Question 2: Birthdays
Please enter Ernie's birthday, using YYYY MM DD: 1984 6 16
Please enter Bert's birthday, using YYYY MM DD: 1975 11 6
Bert is older than Ernie.

Note that the user typed the numbers that are displayed using green colored text.

You should do some problem solving before you start. What will your algorithm be? Having a good plan before you start will make you more productive.

Your task involves the following steps:

Before you start designing your program, you might think that there are 3 possibilities: 2 cases in which one person is older, and 1 case that the people are the same age. However, there are actually more than 3 possible scenarios that you will have to consider in your design, and your program must answer correctly for all of them. We call such scenarios "test cases", especially if we use them to test the hypothesis that a program is correct.
Hint: You'll need 7 test cases (examples) to cover the possibilities. If you can't imagine why you need 7 test cases, you haven't fully understood the problem.
Note: Assume your user will type integers as requested. You do not have to worry about the user entering other kinds of data, like characters or floats.
Note: You may assume that all data given by the user is error-free; they will not try to enter a day beyond the end of the month (ex: 2 40 [Feb 40]), a month that does not exist, a negative year, or any other kind of error like that.

int main() {
string name1;
string name2;
int currentyear, currentmonth , currentday;
int birthyear1, birthmonth1, birthday1;
int birthyear2, birthmonth2, birthday2;
int calc;

cout << "name of first person ";
cin >> name1;
cout << name1 << " birth day YYYY MM DD ";
cin >> birthyear1 >> birthmonth1 >> birthday1;

cout << "name of second person ";
cin >> name2;
cout << name2 << " birth day YYYY MM DD ";
cin >> birthyear2 >> birthmonth2 >> birthday2;

cout << "please enter current year ";
cin >> currentyear;

calc = (currentyear - birthyear1);
calc = (currentyear - birthyear2);

if (name1 > name2)
{
cout << "then: " << name1 << " is older";
}
else
{
cout << "then " << name2 << " is older";
}

if (name1 == name2)

{
cout << "they are the same age" << endl;
}

return 0;
}
@jjj

First problem. You cannot use the same variable, 'calc', to check both ages. After finding the first, that info will be overwritten with the second check. Maybe use calc1 and calc2, instead. Second problem. You're NOT checking ages, but checking names. Which, of course, has nothing to do with one being older or younger, than the other.
Third problem. What if both people were born in the same year, but different months? Or even same months, but different days? Those have to be checked also.
Last edited on
Look at the sample input. Your program should follow this form exactly:
- You don't need to prompt for the names
- You should use the exact same prompts given in the example.
- You don't need to prompt for the current year because it is irrelevant.

Have you learned about classes? You could make this easier by creating a birthday class:
1
2
3
4
class Birthday {
public:
    int year, month, day;
};


Think about how to compare two birthdays to tell if one is less than the other. Then write a function to do it:
1
2
// return -1, 0, or 1 depending on whether b1 is less than, equal to, or greater than b2
int birthdayCmp(Birthday &b1, Birthday &b2);

It might be easier to do this as a method, but let's leave it as a function for now

As whitenite1 said, you have to take into account the year, month, and day. Writing code like this is actually fairly common and it's comvenient to follow this pattern:
1
2
3
4
5
6
7
8
9
10
11
12
int birthdayCmp(Birthday &b1, Birthday &b2) {
    if (b1. year < b2.year) return -1;
    if (b1.year > b2.year) return 1;

    // years must be equal. Move on to compare months
    // using the same pattern

    // if months are equal then compare days.

    // if days are equal then return 0
    // to indicate that the birthdays are equal
}


Once you have this function working, your main program can prompt for Bert and Ernie's birthdays and then compare them like this:
1
2
3
4
5
6
7
8
9
10
11
switch(birthdayCmp(bert, ernie) {
case -1:
    cout << "Ernie is older than Bert\n";
    break;
case 0:
    cout << Bert and Ernie are the same age\n";
    break;
case 1:
    cout << "Bert is older than Ernie\n";
    break;
} 
Topic archived. No new replies allowed.