Then know that this is exactly what is happening when we use your style. You can't say a code is working because most conditions work, and one or two don't.
Why does not my code account for this? Which other scenarios can you think of in the last "else" that remains? So you completely ignored this "else". And that has been your problem throughout.
It is not about RECOMMENDATION. The question was, to SEE if things can work this way FOR THIS PARTICULAR CODE, since this style of nested else-if CANNOT at all work past the first else-if, but since I might be forgetting something. Like I said in my opening post: "Though the point that should be taken here is that it won't work this way, and which is all that should matter." The word "recommendation " can apply only where the option is ALWAYS available. Here, this option can be had only when there are far too less conditions.
I was hoping to guide you to the answer but at this point it may be better to just give you the answer so you can see what I'm talking about. This is the solution using nested ifs.
Code:
string name;
int day, month, year, b_day, b_month, b_year;
cout <<"Enter your first name: ";
cin >>name;
cout <<"H! " <<name <<", what is the date today? Enter day (1 to 31): ";
cin >>day;
cout <<"Now enter the month (1 to 12): ";
cin >>month;
cout <<"And the year? (4 digits): ";
cin >>year;
cout <<"\nNow input your birth date the same way. Day: ";
cin >>b_day;
cout <<"Month: ";
cin >>b_month;
cout <<"Year: ";
cin >>b_year;
if (month < b_month) {
cout <<"\nYour age is: " <<year - b_year - 1 <<" as of today on " <<day <<"-" <<month <<"-" <<year <<".";
} else if (month == b_month) {
if (day < b_day) {
cout <<"\nYour age is: " <<year - b_year - 1 <<" as of today on " <<day <<"-" <<month <<"-" <<year <<".";
} else {
if (day == b_day) cout <<"\nHey... you became " <<year - b_year <<" years old today on " <<day <<"-" <<month <<"-" <<year <<"... Happy Birthday!!";
}
} else {
cout <<"\nYour age is " <<year - b_year <<" as of today on " <<day <<"-" <<month <<"-" <<year <<".";
}
Comment