Passing struct to function (homework)

Hello :)

We're learning structs in my c++ class, and I'm having some issues with this program.

This program is supposed to take 2 times in military format(24:60:60), add them together, and store all 3 values in struct. It should then output the sum of the 2 times. All the cin/cout statements are supposed to be in main, and the math should be done in a function which should "pass" the answer back.

The error im getting is "expected primary-expression before 'times'" in main, when I call the function. (line 31). I suspect I'm doing something wrong with passing my parameters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 #include <iostream>
using namespace std;

struct Time{
	int hour, min, sec;
} times[3];

void add (Time times[3]);

int main(){
	
	//get input + safeties
	for (int i=0; i<2; i++)
	{
	cout<<"Enter the "<<i<<" time (hh:mm:ss): ";
	cin>>times[i].hour>>times[i].min>>times[i].sec;
		while(times[i].hour>=24){
			cout<<"Hours must be less than or equal to 24.";
			cin>>times[i].hour;
		}
		while(times[i].min>60){
			cout<<"Minutes must be less than or equal to 60.";
			cin>>times[i].min;
		}
		while(times[i].sec>60){
			cout<<"Seconds must be less than or equal to 60.";
			cin>>times[i].sec;
		}
	}
	
	add (Time times[3]);
	
	cout<<"Sum of times: "<<times[2].hour<<":"<<times[2].min<<":"<<times[2].sec;
	
	return 0;
}

void add (Time times[3]){
	//add times together
	for (int i=0; i<2; i++){
		times[2].hour+=times[i].hour;	
		times[2].min+=times[i].min;
		times[2].sec+=times[i].sec;
	}

	while (times[2].hour>=24){
		times[2].hour-=24;
	}
	while (times[2].min>60){
		times[2].hour+=1;
		times[2].min-=60;
	}
	while (times[2].sec>60){
		times[2].min+=1;
		times[2].sec-=60;
	}
}
When passing a variable to a function you should only write out the variable name.

 
add(times);
This program is supposed to take 2 times in military format(24:60:60),

Then why is your array of size 3? By the way this can probably be done easier without the array.

add them together, and store all 3 values in struct

What are you to add together? Perhaps the two times?

What 3 values are you to store in a struct? Perhaps the hour, min, and seconds?

All the cin/cout statements are supposed to be in main,

I looks like you have this correct, for now anyway. However you may want to look closer at your validation logic. Can the hours ever be equal to 24?

and the math should be done in a function which should "pass" the answer back.

This doesn't look correct. Where are you returning the value?

I suggest you think about something more like: Time add_time(Time time1, Time time2);



Lines 46-56: Your while loops are in the wrong order. You should check seconds for overflow first, then minutes, and lastly hours. As you have it, when checking seconds, it could cause minutes to overflow, but you've already checked minutes for overflow. Ditto for minutes causing hours to overflow after having checked hours.

It works! Thanks so much guys. anything else incorrect please feel free to let me know.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
using namespace std;

struct Time{
	int hour, min, sec;
} times[3];

int add (Time times[3]);

int main(){
	
	//get input + safeties
	for (int i=0; i<2; i++)
	{
	cout<<"Enter the "<<i<<" time (hh:mm:ss): ";
	cin>>times[i].hour>>times[i].min>>times[i].sec;
		while(times[i].hour>=24){
			cout<<"Hours must be less than 24.";
			cin>>times[i].hour;
		}
		while(times[i].min>60){
			cout<<"Minutes must be less than 60.";
			cin>>times[i].min;
		}
		while(times[i].sec>60){
			cout<<"Seconds must be less than 60.";
			cin>>times[i].sec;
		}
	}
	
	add (times);
	
	cout<<"Sum of times: "<<times[2].hour<<":"<<times[2].min<<":"<<times[2].sec;
	
	return 0;
}

int add (Time times[3]){
	//add times together
	for (int i=0; i<2; i++){
		times[2].hour+=times[i].hour;	
		times[2].min+=times[i].min;
		times[2].sec+=times[i].sec;
	}

	while (times[2].sec>=60){
		times[2].min+=1;
		times[2].sec-=60;
	}
	while (times[2].min>=60){
		times[2].hour+=1;
		times[2].min-=60;
	}
	while (times[2].hour>=24){
	times[2].hour-=24;
	}
}
Topic archived. No new replies allowed.