How to compare two hours? I tried with the code below but it gives me two times true, but it should give false and true:
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
bool earlierEqual(int h1, int m1, int s1, int h2, int m2, int s2)
{
if(h1 <= h2)
{
return true;
}
else
{
if(m1 <= m2)
{
return true;
}
else
{
if(s1 <= s2)
{
return true;
}
else
return false;
}
}
}
bool laterEqual(int h1, int m1, int s1, int h2, int m2, int s2)
{
if(h1 >= h2)
{
return true;
}
else
{
if(m1 >= m2)
{
return true;
}
else
{
if(s1 >= s2)
{
return true;
}
else
return false;
}
}
}
int main()
{
int h1 = 12, m1 = 4, s1 = 29;
int h2 = 11, m2 = 12, s2 = 1;
// false
cout << earlierEqual(h1, m1, s1, h2, m2, s2) << "\n";
// true
cout << laterEqual(h1, m1, s1, h2, m2, s2) << "\n";
return 0;
}
Your
elsebranch should be activated only if the hours are equal. Otherwise, the comparison of minutes will decide even if the hourh1is greater than the hourh2. You should change your code into the following: