Homework help please

Hi,

First let me start off by saying this is a homework assignment. It is my last one and I'm stuck. Below are the instructions provided by my professor and what I have come up with so far.

Where I'm stuck at is void keeps giving me an error, I have changed it around several times and I can get past it but then it tells me t wasn't declared.

I don't know why it doesn't work when the professor provides that as what to use.

I'm going to sleep but will be on tomorrow to see if anyone has explained why I'm getting an error any suggestions on how to get past it.

Thank You in advance.

Description

The program will input two different times, time 1 and time 2, from the user. The user may enter the two times in any order i.e. either of the two times may come before the other. The program will ask the user to input each time in hours, minutes, and seconds on a 24 hour clock so that the user won’t need not to specify am or pm. The program will find the difference between the two times. Then it will display the original two times and the difference between them. It will display the difference in hours, minutes, and seconds. It will do this repeatedly till user inputs a negative number when prompted to input hours for the first time.

Implementation

Use a structure Time to hold each of the two times. The structure Time will be made up of three components: int hour, int minute, and int second.



Create the following functions:

//input hour, minute, and second value and return Time

Time input ( ) {}

//display time into hour, minute, and second

void display (Time t) {}

//return the difference in second given two times.

int findDiff (Time t1, Time t2) {}

//convert second into Time

Time convert (int seconds) {}


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
 #include <iostream>
using namespace std;

double time;
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 Time(int, int, int);
int main(int argc, char** argv)
{int hours, minutes, seconds;
	int T1 = 0;
	int T2 = 0;
cout << "Input first clocktime...";
cout << "Hours: ";
cin >> hours;
cout << "Minutes: ";
cin >> minutes;
cout << "Seconds: ";
cin >> seconds;
T1 = Time(hours, minutes, seconds);
cout << "Input second clocktime...";
cout << "Hours: ";
cin >> hours;
cout << "Minutes: ";
cin >> minutes;
cout << "Seconds: ";
cin >> seconds;
  cout<<"Time 1="<<T1<<endl;
       cout<<"Time 2="<<T2<<endl;
T2 = Time(hours, minutes, seconds);
	
       cout<<"Time 1="<<T1<<endl;
       cout<<"Time 2="<<T2<<endl;
	return 0;
}
int Time(int hours, int minutes, int seconds)
{
	return hours*(3600) + minutes*(60) + seconds;
	
}
Use a structure Time to hold each of the two times. ...
I made the code you gave us match the specification closer, and also implemented the structure correctly. I'm not sure what you're trying to do with your Time function, more details would be helpful. Anyway, here is the code I have. Note that most of what I've seen is that structs are declared in header files, and not with the actual code, however if your instructor wants you to only use one file, we just have to work with that.
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
#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;
	return 0;
	}
}

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


The reason I'm going to assume your instructors function was breaking, is that it was expecting a data item of type Time to be passed in, and you were instead passing in an integer value, which you thought was the correct value.
Hi Zqwerty,

Thank you for your response. I'm not trying to manipulate your code so that I can display the two times. I had it displaying both of the times but then I tried to call display(); and it stopped working.

And by stopped working I mean it doesn't ask for input anymore. So I removed the display - build and it wont work still.

In the end I'm attempting to display the time as HH:MM:SS and calc the difference and display the new time.

My code at the bottom was incomplete I was going to find the difference between the two times there.

I was so close =\

How I had it before it stopped displaying all together
Time 1= 1:1:1
Time 2= 3:3:3

How he wants it is below
Time 1:

Enter hour

1

Enter minute

1

Enter second

1

Time 2:

Enter hour

3

Enter minute

3

Enter second

3

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

I don't know how but I got it to work again, I just need to make it call the display without breaking it again =X

and then find the diff in seconds.

Time 1:

Enter hour

2

Enter minute

0

Enter second

0

Time 2:

Enter hour

1

Enter minute

59

Enter second

59

Time 1= 02:00:00

Time 2= 01:59:59

Diff = 00:00:01
EDIT : nvm I fixed it but need help calling the functions so it doesn't break =\

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;
}
Last edited on
Topic archived. No new replies allowed.