Home > Mobile >  Having trouble with school assignment c Time Calculator
Having trouble with school assignment c Time Calculator

Time:10-09

I know there should be something wrong with the calculations that finds how the days, hours, minutes, and seconds are found.

#include <iostream>
using namespace std;
int main ();

{
int inputSeconds = 643600, days, remainingSeconds, hours, minutes, seconds;

days = inputSeconds / 86400;
remainingSeconds = inputSeconds % 86400;
hours = remainingSeconds / 3600;
remainingSeconds = inputSeconds % 3600;
minutes = remainingSeconds / 60;
seconds = inputSeconds % 60;

the if\else statements are also giving me trouble, however this might be due to the calculations.

  if (inputSeconds >= 86400)
{
  cout << inputSeconds << " seconds is "<< days << " days, " << hours << " hours, " 
     << minutes << " minutes, and " << seconds << " seconds." << endl;
}
  else if (inputSeconds >= 3600)
{
  cout << inputSeconds << " seconds is "<< hours << " hours, " 
       << minutes << " minutes, and " << seconds << " seconds." << endl;
}
  else if (inputSeconds >= 60)
{
  cout << inputSeconds << " seconds is "<< seconds << " seconds." << endl;
}
  else
{
  cout << "Invalid entry. Enter a positive number for seconds. Try the program again." << 
       endl;
}

return 0;
}

When it is given 984561 seconds it gives the right output of

984561 seconds is 11 days, 9 hours, 29 minutes, and 21 seconds.

but when given 13 seconds the output is wrong and gives

Invalid entry. Enter a positive number for seconds. Try the program again. 

I'm looking for

13 seconds is 13 seconds.

CodePudding user response:

add another if else statement after

else if (inputSeconds >= 60)
   {
       cout << inputSeconds << " seconds is "<< seconds << " seconds." << endl;
   }

so we can check numbers between 1-60 before negative numbers

CodePudding user response:

#include <iostream>

using namespace std;

int main ()
{
    int inputSeconds     = 13;
    int remainingSeconds = 0;

    int days         = inputSeconds / 86400;
    remainingSeconds = inputSeconds % 86400;

    int hours        = remainingSeconds / 3600;
    remainingSeconds = remainingSeconds % 3600;

    int minutes      = remainingSeconds / 60;
    int seconds      = remainingSeconds % 60;

    if ( inputSeconds >= 86400 ) {
        cout << inputSeconds << " seconds is "
             << days << " days, "
             << hours << " hours, "
             << minutes << " minutes, and "
             << seconds << " seconds."
             << endl;
    } else if ( inputSeconds >= 3600 ) {
        cout << inputSeconds << " seconds is "
             << hours << " hours, "
             << minutes << " minutes, and "
             << seconds << " seconds."
             << endl;
    } else if ( inputSeconds >= 60 ) {
        cout << inputSeconds << " seconds is "
             << seconds << " seconds."
             << endl;
    } else if ( inputSeconds >= 0 ) {
        cout << inputSeconds << " seconds is "
             << seconds << " seconds." << endl;
    } else {
        cout << "Invalid entry. Enter a positive number for seconds. Try the program again." << endl;
    } 

    return 0;
}
  •  Tags:  
  • c
  • Related