CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Sep 2011
    Posts
    2

    [HELP] Converting Seconds 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
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: [HELP] Converting Seconds to Minutes and Hours

    What doesn't work? Did you try to debug this code?
    Victor Nijegorodov

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: [HELP] Converting Seconds to Minutes and Hours

    The logic in this if statement is flawed. Can you think of a number where that won't evaluate to true?

    else if (input >= 60 || input < 3600)

    I'm not really sure why you even need the if statements at all.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [HELP] Converting Seconds to Minutes and Hours

    Quote Originally Posted by Kifoo View Post
    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?
    Before you write one line of code, have you worked out using simple math and logic how to solve the problem?

    What if I give you 6,000 seconds. How does this:
    Code:
    	minute = input / 60;
    give you the number of minutes? According to that line of code, the number of minutes will be 100. Is that correct?

    What I think you're trying to do is to use the if() statements to bail you out of wrong calculations. As GCDEF stated, those if() statements are totally unnecessary -- make the proper calculations first, and then you don't need those if statements.

    Again, work out on paper what the right formula/algorithm is for the number of seconds, minutes and hours. A hint: You work out the hours first, then from the result of that calculation you work out the minutes, and then the result from that, you work out the number of seconds.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 9th, 2011 at 09:06 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured