Hi Im trying to write a program where I write and use my own functions to calculate time in seconds.
I need to calculate time in seconds of two separate user imputed times.
It requires me to change my time into seconds from hours and minutes. Also I need to calculate the time from 12:00 for each time, and the difference in the two times.
This is my code so far and when I build it I keep getting errors.
//Name functions that we will write later
//tell functions what kind and how many variables will be inside
int calcSec(int, int, int);
int calcTime(int, int);
int main()
{
int totalsecs1;
int totalsecs2;
int h1, min1, sec1;
int h2, min2, sec2;
int calcSec(int hr1, int min1, int sec1)
{
int hour = hr1 * 3600;// <= typo hr1 not h1
int min = min1 * 60;
int totalseconds1 = hour + min + sec1;
return totalseconds1;
}
You'll find it to be much clearer if you use general names for the variables in the function. For instance you dont have to carry the same names through from calling the function to the return value. This makes it easier to narrow down any errors and also make your code clearer for a human to read:
1 2 3 4 5 6 7 8
int calcSec(int hours, int minutes, int seconds)
{
int hrsAsSecs = hours * 3600;
int minAsSecs = minutes * 60;
int totalseconds = hrsAsSecs + minAsSecs + seconds;
return totalseconds;
}