Converting minutes to seconds in C?

Discussion in 'Programming General' started by SexayMistahBee, Apr 15, 2014.

Converting minutes to seconds in C?
  1. Unread #1 - Apr 15, 2014 at 3:43 AM
  2. SexayMistahBee
    Joined:
    Feb 28, 2006
    Posts:
    2,410
    Referrals:
    0
    Sythe Gold:
    27
    Discord Username:
    SexayMistahBee

    SexayMistahBee Sexiest Bee On Earth
    $50 USD Donor New

    Converting minutes to seconds in C?

    I'm trying to convert the decimals of minutes as a float to seconds.

    So if it were 50.429 minutes, it would be 50 minutes and 25 seconds

    I've tried converting the float value to int and subtracting it from the original float and multiplying it by 60

    ex)
    ((50.429-(int)50.429)*60)

    but this gives me a 26 seconds instead of 25.

    I really need to get back to the basics; what's going on here?

    Is there a better way to do this? (without using floor)

    thanks :/
     
  3. Unread #2 - Apr 15, 2014 at 5:02 AM
  4. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,071
    Referrals:
    465
    Sythe Gold:
    5,271
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    Converting minutes to seconds in C?

    You can emulate the behaviour of floor using integer division.

    Code:
    	float input = 50.429;
    	int m = input / 1;
    	int s = ((input - m) * 60) / 1;
    	printf("%d %d", m, s);
    
    Look up "integer division" in C for details.
     
  5. Unread #3 - Apr 15, 2014 at 5:46 AM
  6. SexayMistahBee
    Joined:
    Feb 28, 2006
    Posts:
    2,410
    Referrals:
    0
    Sythe Gold:
    27
    Discord Username:
    SexayMistahBee

    SexayMistahBee Sexiest Bee On Earth
    $50 USD Donor New

    Converting minutes to seconds in C?

    Thanks, dividing by 1 helped clear up my code a bit.

    This was for a simple programming assignment, and here's the product :p

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	float input;
    	int hour, min, sec;
    
    	printf("How many minutes did the trains take to meet?\n");
    	scanf("%f",&input);
    
    	hour = (int)input/60;
    	min = ((int)input/1)%60;
    	sec = ((input-(int)input)*60)/1;
    	printf("%d hours %dminutes %dseconds\n",hour,min,sec);
    
    	return 0;
    }
    
     
  7. Unread #4 - Apr 15, 2014 at 4:25 PM
  8. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    Converting minutes to seconds in C?

    Math.h also has a floor function if you wanted to use standard libraries instead
     
  9. Unread #5 - Apr 15, 2014 at 8:35 PM
  10. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,071
    Referrals:
    465
    Sythe Gold:
    5,271
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    Converting minutes to seconds in C?

    Some tips:
    Scanf is a shitty function prone to all sorts of unintended behaviour. You are much better off capturing the input into a buffer then using sscanf. In this case the input is so simple that not much can go wrong, but it's good practice all the same.

    For an overflow-safe read from STDIN use fgets:
    As the manpage says, the fgets function will behave as a read function should: reading up to the number of chars specified, a new line char or EOF, which ever comes first. The function also provides a return value indicating the success of the read. If the read fails (i.e. EOF has been reached or broken pipe etc.) then it returns null.

    You should also note that all scanf functions return the number of elements in the format string that they successfully scanned. So for example if you had "%f %f %f" but your input string was "50.0 5.1" then it would return 2.

    So for example (untested):

    Code:
    char buffer[51];
    if (fgets(buffer, 50, stdin)) {
        float x = 0;
        if (sscanf(buffer, "%f", &x) == 1) {
            // float was read from input do calcs here
        } else {
            fprintf(stderr, "input did not contain a float\n");
        }
    } else {
        fprintf(stderr, "read from stdin failed\n");
    }
    
     
< Command to choose suggested code in Xcode? | Need little help with delphi >

Users viewing this thread
1 guest


 
 
Adblock breaks this site