Thread: Convert minutes to hours in C

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    11

    Convert minutes to hours in C

    Creating and using my own function I have to convert minutes into hours. The program that I have created compiles without any errors however when I enter the minutes it does not give the correct output.

    For example if I enter 135 minutes the output is this:
    Enter the local time in minutes: 135
    localTime = 2686792

    hours = 0
    min = 2147344384



    Can anyone help me figure out how to get the output to display the correct information?

    Code:
     9 
    10 #include <stdio.h>
    11 #include <math.h>
    12 
    13 int minToHours(long time, int* min, int* hours);
    14 int main ()
    15 {
    16 
    17         
    18    
    19       int min = 0;
    20       int hours =  0;
    21       int minToHours = 0;
    22       long localTime;
    23       long time;
    24       
    25    
    26       printf("Enter the local time in minutes: "); 
    27       scanf("%d", &min);
    28       
    29       printf("localTime = %d\nhours = %d\nmin = %d\n");
    30       
    31    
    32       localTime = time;
    33       min = localTime % 60;
    34       localTime = localTime / 60;
    35       
    36       min = localTime % 60;
    37       
    38       hours = localTime / 60;
    39       
    40          
    41        if(hours > 24)
    42          return 0;
    43          
    44       else
    45          return 1;
    46              
    47    
    48       printf("\nTime in Hours is: \n", minToHours); 
    49       
    
       return 0;
    58 }
    59 
    60 int minToHours(long time1, int* min1, int* hours1)
    61 
    62 {
    63    long localTime;
    64    *min1 = localTime % 60;
    65    *hours1 = localTime / 60;
    66    localTime *= localTime / 60;
    67    return localTime; 
    68    
    69 }
    
    

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    O_________O

    Your question is not quite clear and your example input output makes zero sense

    For example if I enter 135 minutes the output is this:
    Enter the local time in minutes: 135
    localTime = 2686792
    hours = 0
    min = 2147344384
    'localtime' should not be related to the input - at least I don't see how its related.

    Given 135 minutes - You have two hours and 15 minutes, so I don't see how how you get the output your are getting.

    On the other paw, I doubt you even wrote the above code.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    I am a newbie which should be obvious from my code I'm looking for assistance on how to correct my errors and get the program to work properly. If you can't assist me then please either leave me alone or point me in the right direction.

  4. #4
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Staja24 View Post
    I am a newbie which should be obvious from my code I'm looking for assistance on how to correct my errors and get the program to work properly. If you can't assist me then please either leave me alone or point me in the right direction.
    Well, ignoring your code, can you describe the problem you want to solve clear because as I said in post #1 the output you say you expect does not make sense.

  5. #5
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    Ok, I get the number of minutes from the user and then I need to convert that into the total number of hours, so for 135 min it would be 2.25 hours. I haven't figured out how to do this correctly and this is also where I became confused. And my code may be calculating hours and minutes rather than just hours.

    I know that I would need to use the modulus operator because I have a remainder, and I was writing my own function to use in the program that you can see at the bottom, but I know there are errors with that and the code in general.

    Also localTime is just an arbitrary name so I can change it to something that makes more sense like minToHours

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    First, the way to get the answer in hours only is to use decimal division. Decimal numbers are represented in floats and doubles in C.

    Something like double answer = minutes / 60.0; computes the number of hours. Then you just have to print it with %f. This may help too:

  7. #7
    Registered User
    Join Date
    Apr 2015
    Posts
    11
    Ah ok that makes sense, Thank you very much I will check out those links

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert seconds to hours and minutes
    By LearnOnTheFly in forum C Programming
    Replies: 16
    Last Post: 03-28-2012, 08:27 AM
  2. Convert seconds to hours, minutes and seconds
    By kkk in forum C Programming
    Replies: 2
    Last Post: 07-26-2011, 10:47 AM
  3. Difference b/ween hours into minutes??
    By CodeFuse in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2003, 02: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