Attempting to write functions, getting errors

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.

#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;

//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;

cout << "Input first clock time... \n ";
cout << "Hours: "; cin >> h1;
cout << "\n Minutes: "; cin >> min1;
cout << "\n Seconds: "; cin >> sec1;

cout << "Input second clock time... \n ";
cout << "Hours: "; cin >> h2;
cout << "\n Minutes: "; cin >> min2;
cout << "\n Seconds: "; cin >> sec2;



totalsecs1 = calcSec (h1, min1, sec1);

totalsecs2 = calcSec (h2, min2, sec2);

int time = calcTime (totalsecs1, totalsecs2);

return 0;
}



int calcSec(int hr1, int min1, int sec1)
{
int hour = h1 * 3600;
int min = min1 * 60;
int totalseconds1=hour+min+sec1;
return totalseconds1;

}

int calcTime(int totalsecs1, int totalsecs2)
{
int time=totalsecs1-totalsecs2;
return time;
}
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
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;

}
Last edited on
Topic archived. No new replies allowed.