The task is to compare two data with each other. The output is then, for example: The first date is before the second date. I am only allowed to solve this with if-else. My problem is that the other else-if conditions are ignored, because the first condition is probably true. How do I fix this? The date is not entered together but individually as an integer.
int D;
int M;
int Y;
int D2;
int M2;
int Y2;
cout << "Please enter the day of the first date:";
cin >> D;
cout << endl;
cout << "Please enter the month of the first date";
cin >> M;
cout << endl;
cout << "Please enter the year of the first date";
cin >> Y;
cout << endl;
cout << "Please enter the day of the second date";
cin >> D2;
cout << endl;
cout << "Please enter the month of the second date";
cin >> M2;
cout << endl;
cout << "Please enter the year of the second date";
cin >> Y2;
cout << endl;
if (D == D2 && M == M1 && Y == Y2)
{
cout << "Both dates are the same" << endl;
}
else if (Y2 >= Y)
{
cout << "The first date is before the second date" << endl;
}
else if (Y >= Y2)
{
cout << "The second date is before the first date" << endl;
}
else if (D2 >= D)
{
cout << "The first date is before the second date" << endl;
}
else if (D >= D2)
{
cout << "The second date is before the first date" << endl;
}
else if (M2 >= M)
{
cout << "The first date is before the second date" << endl;
}
else if (M >= M2)
{
cout << "The second date is before the first date" << endl;
}
CodePudding user response:
you have some problems with logic, you need to restructure your if-else:
if (Y2 > Y)
cout << "date 2 after date 1" << endl;
else if (Y2 < Y)
cout << "date 2 before date 1" << endl;
else {
// here we know that Y2 == Y, so we need to continue with month/day
if (M2 > M)
cout << "date 2 after date 1" << endl;
else if (M2 < M)
cout << "date 2 before date 1" << endl;
else {
// here we know that Y2 == Y AND M2 == M, so we need to continue with day
... here you can continue
}
}
CodePudding user response:
The problem is that when you use else if
you ignore everything after once something is True. The fix is very simple, just take away the elses
, if you keep them, the moment something is true all the following code wil be ignored. Also I strongly reccomed you use a for loop to keep your code more organised.
For extra info on conditionals I recomend you read: https://www.w3schools.com/c/c_conditions.php
CodePudding user response:
I'd combine the information for each date:
// Yes, the multipliers look peculiar; this allows for
// 1-based data ranges. Of course, this calculation should
// be done in its own function.
unsigned long long date0 = (Y * 13ULL M) * 32 D;
unsigned long long date2 = (Y2 * 13ULL M2) * 32 D2;
if (date0 < date2)
std::cout << "The first date is before the second date\n";
else if (date0 == date2)
std::cout << "Both dates are the same.\n";
else
std::cout << "The second date is before the first date\n";
For the straight brute force code, you want to end up with only three possible output statements; don't write the same output multiple times. That means you have to look carefully at all the possible ways of getting one of the outputs.
The first date is before the second one if the first year is less than the second one, or if the two years are the same and the first month is before the second one, or if the two years are the same and the two months are the same and the first day is before the second day. The first date is the same as the second one if all three values are the same. The second date is before the first one if neither of the previous cases is true.
Now just write code to do that. Here's a start:
if (Y < Y2
|| Y == Y2 && M < M2
|| /* left as an exercise for the student */)
std::cout << "The first date is before the second date\n";