Help please!

Hi,

This is my last assignment for my intro to programming class and it due tomorrow.

If anyone can help me get it to do display the below after inputting the below


Test Run
Time 1:
Enter hour
1
Enter minute
1
Enter second
1
Time 2:
Enter hour
3
Enter minute
3
Enter second
3

display
Time 1= 01:01:01
Time 2= 03:03:03
Diff = 02:02:02



Time 1:
Enter hour
2
Enter minute
0
Enter second
0
Time 2:
Enter hour
1
Enter minute
59
Enter second
59

Display again
Time 1= 02:00:00
Time 2= 01:59:59
Diff = 00:00:01
Time 1:
Enter hour
-1
Bye

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
#include<fstream>
using namespace std;

struct Time
{
int hour;
int minute;
int second;
};

void display(Time t);

int main()
{
	Time Time1;
	Time Time2;

	while (Time1.hour > 0)
	{
	cout << "Input first clocktime... " << endl;
	cout << "Hours: ";
	cin >> Time1.hour;
	cout << endl;
	cout << "Minutes: ";
	cin >> Time1.minute;
	cout << endl;
	cout << "Seconds: ";
	cin >> Time1.second;


	cout << "Input second clocktime... " << endl;
	cout << "Hours: ";
	cin >> Time2.hour;
	cout << endl;
	cout << "Minutes: ";
	cin >> Time2.minute;
	cout << endl;
	cout << "Seconds: ";
	cin >> Time2.second;
	}

}

void display(Time t)
{
if (t.hour < 10)
cout << "0";
cout << t.hour;
cout << ":";
if (t.minute < 10)
cout << "0";
cout << t.minute;
cout << ":";
if (t.second < 10)
cout << "0";
cout << t.second;
cout << endl;
}

int findDiff (Time t1, Time t2)
{
double Time1,Time2,diff;
//conver t1 into seconds
Time1=t1.hour*(3600)+t1.minute*(60)+t1.second;
//convert t2 into seconds
Time2=t2.hour*(3600)+t2.minute*(60)+t2.second;
//find the difference in seconds
diff=Time1-Time2;
//return the difference
return diff;
}
In your function void display(Time t) your if statements need to have braces and you should include an else statement after each if statement that displays hour/minutes/seconds without the leading zero to handle double digit numbers.

Then just add code to convert diff into hours, minutes, and seconds so you can display it formatted correctly and then in main, do something like
1
2
cout << "Time 1 = ";
display(Time t)

and then repeat for Time 2 and Diff.
Hi pnoid,

Thank you so much for the help, I'm going to try it out right now.
I'm getting an error at

display(Time t);

error expected primary expression before t

any ideas?
What's your new code look like?
i had to use else if's in order for the brackets to work and i don't understand why display(Time t); doesn't work since its the same name as the void? Mind you I have only done two other assignments with calling.


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>
#include<fstream>
using namespace std;

struct Time
{
int hour;
int minute;
int second;
};

void display(Time t);

int main()
{
	Time Time1;
	Time Time2;

	while (Time1.hour > 0)
	{
	cout << "Input first clocktime... " << endl;
	cout << "Hours: ";
	cin >> Time1.hour;
	cout << endl;
	cout << "Minutes: ";
	cin >> Time1.minute;
	cout << endl;
	cout << "Seconds: ";
	cin >> Time1.second;


	cout << "Input second clocktime... " << endl;
	cout << "Hours: ";
	cin >> Time2.hour;
	cout << endl;
	cout << "Minutes: ";
	cin >> Time2.minute;
	cout << endl;
	cout << "Seconds: ";
	cin >> Time2.second;
cout<<"Time 1 =";
display(Time t);
	}
	return 0;
}
void display(Time t)
{
if (t.hour < 10){
cout << "0";
cout << t.hour;
cout << ":";}
else if(t.minute < 10){
cout << t.minute;
cout << ":";}
else if(t.second < 10){
cout << "0";
cout << t.second;
cout << endl;}
}

int findDiff (Time t1, Time t2)
{
double Time1,Time2,diff;
//conver t1 into seconds
Time1=t1.hour*(3600)+t1.minute*(60)+t1.second;
//convert t2 into seconds
Time2=t2.hour*(3600)+t2.minute*(60)+t2.second;
//find the difference in seconds
diff=Time1-Time2;
//return the difference
return diff;
cout<<"bye"<<endl;
}
Oh I see what's wrong. You have to pass in the time you want to display in your display function, not Time t. Sorry, I should have been more specific earlier. For this code it would look like this
1
2
cout<<"Time 1 =";
display(Time1);
yes, that got it to work. I had to put the else ifs back to ifs in order for it display correctly. However after i run it and close it. I try to run it again and it just gives me a black screen.

Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.


Last edited on
ok, it seems to be working again i just need to figure out how to display the difference
Sounds like it's not going into your while loop for some reason. It looks like you don't even need the while loop anyways.
it asks for me to enter in again which is ok because of the loop and i need it to for my assignment. but when close out of it and try to run again i get that message.. it went away and came back again.. so frustrating i cant check my code ahhhhhhhhhh
I got it to work again...sigh

is this how i would call the find diff?

cout<<"Diff =";
findDiff(Time1, Time2);
I don't know about it not working after closing it, I feel like your while loop might have something to do with that, I couldn't even run it as it was, I had to make it a do while loop because Time1 was uninitialized.

That's not how you call findDiff, this is cout<<"Diff =" << findDiff(Time1, Time2); , but it returns the time in seconds so you would need to convert it to hours, minutes, and seconds to get it to display how you wanted it.
Last edited on
Also, you need to prototype the findDiff function at the top like you did for your display function.
Thanks for all the help pnoid I greatly appreciate it. I just have to find out how to convert seconds int hours min seconds now. I think i know how its just figuring out how to write it at this point.

many thanks. if there is a way for me to thumbs up you let me know
and it is completed!!!!!!!!!!!!!!
You already converted it from hours and minutes into seconds here
 
Time1=t1.hour*(3600)+t1.minute*(60)+t1.second;
so now you just use division to undo the multiplication to convert it back to hours and minutes.

Something like this:
1
2
3
hours = diff / 3600;
minutes = (diff % 3600) / 60;
seconds = diff - hours * 3600 - minutes * 60;
Last edited on
Yes i did that and turned it in im so excited pnoid you rock!
Topic archived. No new replies allowed.