user4013823
user4013823

Reputation:

Converting seconds to hours and minutes and seconds

#include <cstdio>
#include <iostream>
using namespace std;
int main ()
{
    int seconds, hours, minutes;
    cin >> seconds;
    hours = seconds/3600;
    cout << seconds << " seconds is equivalent to " << int(hours) << " hours " << seconds%(hours*60) 
         << " minutes " << (seconds%(hours*3600))-((seconds%(hours*60))*60) << " seconds.";
}

For some reason, this program works with only numbers above 3600. Does anyone know how to fix this problem? Whenever I do a number below 3600, the screen shows up with a message from Windows saying that the program has stopped working.

Upvotes: 21

Views: 152807

Answers (11)

Abdullah Samy
Abdullah Samy

Reputation: 1

Try this

int h,m,s;
    cout << "Time in Seconds = ";
    cin >> s;
    cout << "H:M:S - " << s / 3600 << ":" << (s % 3600) / 60 << ":" << s % 60;    

Input:

Time in Seconds = 25300

Output:

H:M:S - 7:1:40

Upvotes: 0

user2232395
user2232395

Reputation: 491

Until now none of the presented solution have in count the day duration. I had to write a clock to use in arduino, the millis() function return milliseconds since turn on, to get seconds just divide by 1000.

void showTime(unsigned long  seconds){
  
   printf( "seconds : %lu ", seconds); 
   int hh = (seconds/3600) %24;
   int mm = (seconds/60) %60;
   int ss = seconds%60;
   printf( " Time : %2d:%02d:%02d\n", hh, mm,ss);

   }

   showTime( 0*24*3600 +  4*3600+35*60+25); // 0d+4hs+35m+25s; 
   showTime( 2*24*3600 +  1*3600+45*60+59); // 2d+1hs+45m+59s; 
   showTime(40*24*3600 + 10*3600+ 5*60+10); // 40d+10hs+5m+10s;

wath print:

seconds : 189325  Time :  4:35:25
seconds : 6359  Time :  1:45:59
seconds : 3492360  Time : 10:06:00

Upvotes: 1

Alan Birtles
Alan Birtles

Reputation: 36568

With c++20 this gets pretty easy using std::chrono::hh_mm_ss:

#include <iostream>
#include <chrono>

int main ()
{
    int seconds;
    std::cin >> seconds;
    std::chrono::hh_mm_ss time{std::chrono::seconds(seconds)};
    std::cout << seconds << " seconds is equivalent to " << time.hours().count() << " hours " << time.minutes().count() 
         << " minutes " << time.seconds().count() << " seconds.";
}

Upvotes: 2

Anatoly Boldin
Anatoly Boldin

Reputation: 11

One string convertion from seconds to hh:mm:ss format:

sprintf(tempBuffer, "%02u:%02u:%02u", _time / 3600, (_time % 3600) / 60, _time % 60);

Upvotes: 1

brc-dd
brc-dd

Reputation: 13074

Using std::chrono::duration_cast, one can simply write (refer std::chrono::duration for more options):

#include <chrono>
#include <iostream>

using namespace std::chrono;

int main() {
  int s; std::cin >> s;
  seconds sec(s);
  std::cout << duration_cast<hours>(sec).count() << ':'
            << duration_cast<minutes>(sec).count() % 60 << ':'
            << sec.count() % 60;
}

Upvotes: 2

ahmed
ahmed

Reputation: 1

Have a look at this code:

#include <iostream>
using namespace std;

int main()
{
  int seconds;
  cout << "Enter an integer for seconds: ";
  cin >> seconds;
  int minutes = seconds / 60;
  int remainingSeconds = seconds % 60;
  cout << seconds << " seconds is " << minutes <<
    " minutes and " << remainingSeconds << " seconds " << endl;

  return 0;

}

Upvotes: -1

Hani Shams
Hani Shams

Reputation: 361

Try this:

int totalSecond;
cin >> totalSecond;

int hour = totalSecond / 3600;
int minute = (totalSecond % 3600) / 60;
int second = totalSecond % 60;

cout << hour << ":" << minute << ":" << second << endl;

Assuming that totalSeconds is the number of seconds since midnight and is less than 86400

Upvotes: 2

sarmand mahmood
sarmand mahmood

Reputation: 21

by using function;

#include<iostream>

    using namespace std;
    int hour(int h)
    {
        int second;
        //second=(h/3600);
        if (h>3600)
            second=h/3600;
        else 
           second=(h/3600);
        return (second);
    }

    int minute(int m)
    {
        int second2;
        second2=(   );
        return(second2);
    }

    int second(int s)
    {
        int second3;
        second3=((s-3600)%60);
        return (second3);
    }

    void main()
    {
        int convert;
        cout<<"please enter seconed to convert it to hour\b";

        cin>>convert;
        cout<<"hr : min : sec \n";
        cout<<hour(convert)<<":"<<minute(convert)<<":"<<second(convert)<<endl;
        system("pause");
    }

Upvotes: 1

T.C.
T.C.

Reputation: 137394

seconds/3600 is integer division, so for seconds < 3600, hours is 0, then things like seconds%(hours*3600) becomes seconds % 0, causing a division-by-zero.


Let's first get the logic right. Suppose you want to write 5000 seconds as x hours y minutes z seconds, such that all three are integers and neither y nor z is greater than 59. What do you do?

Well, you can first write it as q minutes z seconds, such that both are integers and z is not greater than 59. That's easy:

q = 5000 / 60 = 83  // integer division
z = 5000 % 60 = 20

So 5000 seconds is 83 minutes 20 seconds. Now how do you write 83 minutes into x hours y minutes, such that both are integers and y is no greater than 59? You do the same thing:

x = 83 / 60 = 1
y = 83 % 60 = 23

OK, let's generalize this:

int total, seconds, hours, minutes;
cin >> total;
minutes = total / 60;
seconds = total % 60;
hours = minutes / 60;
minutes = minutes % 60;
cout << total << " seconds is equivalent to " << hours << " hours " << minutes 
     << " minutes " << seconds << " seconds.\n" ;

Upvotes: 18

Shadow
Shadow

Reputation: 4016

Try this out instead, tested and works:

int seconds, hours, minutes;
cin >> seconds;
minutes = seconds / 60;
hours = minutes / 60;
cout << seconds << " seconds is equivalent to " << int(hours) << " hours " << int(minutes%60) 
     << " minutes " << int(seconds%60) << " seconds.";

Because minutes is seconds/60, dividing it by 60 again is equivalent to diving seconds by 3600, which is why it works.

Upvotes: 29

Makoto
Makoto

Reputation: 106498

You've got a divide-by-zero problem here:

seconds % (hours*60);

hours is 0 by virtue of integer division.

hours = seconds/3600;

From what you're trying to do, you should consider conditional logic to print the minutes if the total number of seconds is greater than 3600. You'll also want to investigate similar logic in the next part of your print stream.

My C++ is rusty, so forgive if this isn't exactly valid syntax:

cout << (seconds > 3600 ? seconds % (hours*60) : seconds) << endl;

Upvotes: 2

Related Questions