secs into hours mins and remainin secs

This is a discussion on secs into hours mins and remainin secs within the C++ Programming forums, part of the General Programming Boards category; Can anyone tell me a good algorithm on how to convert seconds into hours, minutes and remaining secs ? I ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    secs into hours mins and remainin secs

    Can anyone tell me a good algorithm on how to convert seconds into hours, minutes and remaining secs ?

    I have this source code, but when you enter a bigger value it gets slowed..

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
    	int secondsInput, seconds, hours = 0, minutes = 0;
    	cout << "enter number of seconds: ";
    	cin >> secondsInput;
    
    	seconds = secondsInput;
    
    	int hrs = 3600;
    	int mins = 60;
    
    	for ( int x = 1; x <= secondsInput; x++ ) {
    
    		if ( x == hrs) {
    			hours += 1;
    			seconds -= 3600;
    			hrs += 3600;
    		}
    	}
    
    	for ( int y = 1; y <= seconds; y++ ) {
    
    		if ( y == mins ) {
    			minutes += 1;
    			mins += 60;
    			}
    	}
    
    	int z = secondsInput - (( 3600 * hours ) +  ( 60 * minutes )); // z = number of seconds
    
    	cout << "\n" << secondsInput << " equals to " << hours << " hours " << minutes << " minutes " << z << " seconds";
        return 0;
    }
    Thanks

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,421
    Code:
    int nSeconds = 3930;
    int nMinutes = nSeconds / 60;
    nHours = nMinutes / 60;
    nSeconds -= nMinutes * 60;
    nMinutes -= nHours * 60;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    something simple like this should do
    Code:
    int main() {
        int secs = 1234567;
        int h,m,s;
        h = secs / 3600;
        m = (secs - h * 3600) / 60;
        s = secs - h*3600 - m*60;
    }
    Kurt

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21