[HELP] Converting Secoonds to Minutes and Hours

This is a discussion on [HELP] Converting Secoonds to Minutes and Hours within the C++ Programming forums, part of the General Programming Boards category; So I'm supposed to make a program which converts seconds into the equivalent hours, minutes, and seconds (if that makes ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    10

    [HELP] Converting Secoonds to Minutes and Hours

    So I'm supposed to make a program which converts seconds into the equivalent hours, minutes, and seconds (if that makes sense)

    For example, if I put in 65 seconds, the result would be 1 minute and 5 seconds.

    I made this codes but I can't seem to make the hours work. Help please?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main (){
    	int input, hour, minute, second;
    
    	cout << "Please enter number of seconds: ";
    		cin >> input;
    
    	minute = input / 60;
    	hour = input / 3600;
    	second = minute % 60;
    
    
    	if (input <= 59)
    		cout << input << " seconds is equivalent to 0 hours, 0 minutes, and " << input << " seconds."  << endl;	
    
    	else if (input >= 60 || input < 3600)
    		cout << input << " seconds is equivalent to 0 hours, " << minute << " minutes and " << second << " seconds" << endl;
    
    	else if (input >= 3600)
    		cout << input << " seconds is equivalent to " << hour << " hours, " << minute << " minutes and " << second << " seconds" << endl;
    
    	
    return 0;
    }
    All help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,879
    Let's look at what your code does for input of 3601

    minute, hour, and second will have the values 60, 1, and 0 respectively, but from your description they should have the value 0, 1, and 1 respectively. So the only value you have computed correctly is number of hours.

    Try computing number of hours first. Then subtract that number of hours from the input, to get the number of remaining seconds. From that, compute the number of minutes and subtract that number of minutes from input, to get the remaining seconds.

    Yes, I've deliberately described in words one option to do what you need to do in code. You could also work back the other way (compute number of seconds using the modulo operator, then minutes, then hours).

    Incidentally, you should not need separate output statements for different values of input.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    Thank you! I got it to work. I kept thinking that I needed to use else if when all I needed was % and /.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main (){
    	int input, input2, input3, minute, second, hour;
    
    	cout << "Please enter number of seconds: ";
    			cin >> input;
    
    	hour = input / 3600;
    	input2 = input % 3600;
    	minute = input2 / 60;
    	input3 = input2 % 60;
    	second = input3;
    	
    	cout << input << " seconds is equivalent to " << hour << " hour(s), " << minute << " minute(s), and " << second << " second(s)." << endl;
    
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-05-2009, 12:19 AM
  2. Algo for create hours and minutes with given seconds
    By BianConiglio in forum C Programming
    Replies: 6
    Last Post: 05-30-2004, 06:50 PM
  3. Difference b/ween hours into minutes??
    By CodeFuse in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2003, 01:01 AM
  4. switch hours and minutes
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 07-08-2002, 11:30 PM
  5. Converting MInutes to Hours and MInutes
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-30-2001, 08:07 PM

Tags for this Thread


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