-3

I am trying to get a duration of time into minutes from a string. I am given a string like this: "1:50". And I need to extract the minutes and seconds from this strings into int variables and then return the duration in minutes. So I wrote this:

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <conio.h>
    #include <string.h>

    int main()
    {
     char time[6]="01:30";
     int duration=0, minutes=0, seconds=0;
     int buffermin[3];
     int buffersec[3];
     int i=0;

     while(i<2)
     {
       sscanf(time[i],"%d%d",&buffermin[i]); //Get the first two characters in the string and store them in a intger array
       i++;
     }
     i=3;
     while(i<5)
     {
      sscanf(time[i],"%d%d",&buffersec[i]); //Get the last two characters in the                       string and store them in a integer array
      i++;
     }

     printf("%d %d %d %d", buffermin[0], buffermin[1], buffersec[0], buffersec[1]);

     getch();

     minutes=(buffermin[0]*10)+buffermin[1]; //Put values in array to one variable
     seconds=(buffersec[0]*10)+buffersec[1]; //Same as above

     seconds=seconds/60; //Turn the number of seconds to minutes

     duration=seconds+minutes; //Get total duration

     printf("The total duration is: %d\n",duration);  //Output total duration

     getch();
     exit(0);
     }    

Why is this not working and how could I fix this. Any examples would be really very appreciated. If you have the time to explain how the example works, please do so. Still poor at programming as you can see.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
user3047289
  • 35
  • 1
  • 3
  • You wrote that and what happened? Please edit the question. – Stefano Sanfilippo Jan 26 '14 at 17:49
  • 1
    And it does what? It produces what output? In what way does its behavior defy your expectations? And what is your question? – DavidO Jan 26 '14 at 17:49
  • 2
    This question appears to be off-topic because it is not a question. –  Jan 26 '14 at 17:49
  • and your question is?……… – zmo Jan 26 '14 at 17:50
  • Forgot to add the actual question, sorry. Updated. – user3047289 Jan 26 '14 at 17:52
  • 1
    Your code demostrate the lack of a minimal understanding of how `sscanf` works and the difference between floating point and integer arithmetic. C is very easy to learn (at a basic-intermediate level), but you have to _learn_ it. You should really get a good C book and study it before coding. It will save you a lot of headaches. – Stefano Sanfilippo Jan 26 '14 at 18:12

1 Answers1

1

You should really learn how to use sscanf properly. Basically, what you want to achieve is this:

#include <stdio.h>

int main() {
    char time[] = "01:27";

    int minutes;
    int seconds;

    // Must be double, not integer, otherwise decimal digits will be truncated
    double duration;

    // String has format "integer:integer"
    int parsed = sscanf(time, "%d:%d", &minutes, &seconds);

    // Check if input was valid    
    if (parsed < 2) {
        // String had wrong format, less than 2 integers parsed
        printf("Error: bad time format");
        return 1;
    }

    // Convert to minutes (mind the floating point division)
    duration = (seconds / 60.0) + minutes;

    printf("Duration: %.2f minutes\n", duration);

    return 0;
}
Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
  • Thank you, I would upvote you if I could. What does the parsed value do? – user3047289 Jan 26 '14 at 18:12
  • It counts how many numbers were parsed. They must be 2, minutes and seconds. If less then 2 numbers were found, then the string has an incorrect format. – Stefano Sanfilippo Jan 26 '14 at 18:14
  • To your comment above; any recommendations on c books? – user3047289 Jan 26 '14 at 19:09
  • Unfortunately I have no recommendation because I first learnt C on K&R (_Kernighan and Ritchie_), a book from the 70s which refers to an outdated version of C, and I kept up-to-date with the new additions from C89 and C99. I can recommend you _not to read_ any book from Herbert Schildt and any book specific to Windows (their specific APIs are quite non-standard), but **apart from that you can start [from this answer](http://stackoverflow.com/a/562377/2344584) to get some titles and then search for opinions on those.** – Stefano Sanfilippo Jan 26 '14 at 19:18
  • The first in the beginner list seems to be the best one for you. – Stefano Sanfilippo Jan 26 '14 at 19:23
  • Honestly appreciate your help, thank you. – user3047289 Jan 26 '14 at 19:36