I wanted to give output to the user (from the Age Calculator) such that there is no value of 0.
That is:
The program will give the output of user's age in YEARS, MONTHS, WEEKS and DAYS.
Now I wanted that if, for instance, the user is exactly 16 years and 8 months old, the output will ignore weeks and days.
I used the following code to do so (is is still incomplete)
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
|
//Giving the Age
if (year_age == 0)
{
cout << "You are "<< month_age << " months, "<< week_age << " weeks and " << date_age << " days old."
cout << endl << endl;
}
else if ((month_age == 0) && (week_age == 0))
{
cout << "You are "<< year_age << " years and " << date_age << " days old."<< endl << endl;
}
else if ((month_age == 0) && (date_age == 0))
{
cout << "You are "<< year_age << " years and " << week_age << " weeks old. " << endl << endl;
}
else if ((date_age == 0) && (week_age == 0))
{
cout << "You are "<< year_age << " years and " << month_age << " months old. "<< endl << endl;
}
else if (date == cur_date && month == cur_month && week_age == 0)
{
cout << "You are "<< year_age << " years old."<<endl <<endl;
}
else
{
cout << "You are "<< year_age << " years, " << month_age << " months, "<< week_age << " weeks and ";
cout << date_age << " days old." << endl << endl;
}
|
Now the problem I am facing:
If the users is exactly
x years old (say 13), then the output should be
, but the output is
The program is ignoring the last
else if
command and using the first one.
I encounter the same problem if I try to do so with the program ignoring only one of the 4 values(year, month, week and date).
If I add the conditions for these as well, he compiler ignores the others.
The others is like in the above code if I added
1 2 3 4 5 6 7 8 9 10 11 12
|
if (month_age == 0)
{
cout << "You are " << year_age << " years, " << week_age << " weeks and " << date_age << " days old.";
}
//From the code I posted above
else if ((month_age == 0) && (week_age == 0))
{
cout << "You are "<< year_age << " years and " << date_age << " days old."<< endl << endl;
}
//And further.....
|
And if the Age of th User is 15 YEARS and 5 DAYS old.
It will give the output:
You are 15 years, 0 weeks and 5 days old.
|
So is there any way to get around this?