c++cvisual-studio-2013elapsed

Finding the elapsed time


This is what I have so far. I am learning structure, and I had to use a structure for the assignment. I don't get what is wrong with my code. When I debug, I get 0 for hour, minute, seconds. Please look at my code.

#include <stdio.h>

struct calculate
{
    int hours;
    int minutes;
    int seconds;
};

struct calculate N1;
struct calculate N2;
int resulthr, resultmin, resultsec;

int substract(struct calculate N1, struct calculate N2)
{
    resultsec = N2.seconds - N1.seconds;
    if (resultsec < 0)
    {
        resultsec = 60 + resultsec;
        --N2.minutes;
    }

    resultmin = N2.minutes - N1.minutes;
    if (resultmin < 0)
    {
        resultsec = 60 + resultmin;
        --N2.hours;
    }

    resulthr = N2.hours - N1.hours;
    if (resulthr < 0)
    {
        resulthr = 24 + resultsec;
    }
    return 0;
}

int main(void)
{
    printf("Enter two times.(hh:mm:ss) ");
    scanf("%i:%i:%i", &N1.hours, &N1.minutes, &N1.seconds);
    scanf("%i:%i:%i", &N2.hours, &N2.minutes, &N2.seconds);

    int substract(calculate N1, calculate N2);

    printf("The elasped time is %i:%i:%i.\n", resulthr, resultmin, resultsec);

    return 0;
}

https://i.sstatic.net/VlYVL.png


Solution

  • call substract(N1, N2); instead of int substract(calculate N1, calculate N2); in the main function.

    You were declaring it, not calling it.