lundi 9 mars 2020

Only half of if/else statement works C++ (seconds to days, hours, minutes, seconds)

My code is not operating the other half of the if-else statement. It only displays minutes and seconds and I couldn't figure out the bug for the last 2 hours. The program requirement is to display only the value if it doesn't equal 0.

For example, if it's 2 days 1 hour 0 minutes 0 seconds it should display 2 days 1 hour. If it's 0 days 1 hour 1 minute 0 seconds it should display 1 hour 1 minute

using namespace std;
#include <iostream>

int
main ()
{
  using namespace std;

  long long int seconds;
  long long int days, minutes, hours, Seconds;

  cout << "Enter seconds" << endl;
  cin >> seconds;
  cout << "Total seconds: " << seconds << endl << endl;

  days = seconds / 60 / 60 / 24;
  hours = (seconds / 60 / 60) % 24;
  minutes = (seconds / 60) % 60;
  Seconds = seconds % 60;

  if (seconds < 0)
    {
      cout << "Total seconds must be greater than zero" << seconds << endl;
    }
  else
    {
      if (seconds < 60)
    {
      cout << Seconds << " second(s)" << endl;
    }
      else if (60 <= seconds < 3600)
    {
      cout << minutes << " minute(s)" << endl;
      cout << Seconds << " second(s)" << endl;
    }
      else if (3600 <= seconds < 86400)
    {
      cout << hours << " hour(s)" << endl;
      cout << minutes << " minute(s)" << endl;
      cout << Seconds << " second(s)" << endl;
    }
      else
    {
      cout << days << " day(s)" << endl;
      cout << hours << " hour(s)" << endl;
      cout << minutes << " minute(s)" << endl;
      cout << Seconds << " second(s)" << endl;
    }
    }
  return (0);
}

Aucun commentaire:

Enregistrer un commentaire