Rewriting a program

Hello people...I'm interested in changing this program so that the user is ask to enter 10 login times (using a loop of 1 - 10) then print out the time difference between the first login and the last login...I made several attempts at it but all in vain...I'm just wondering if it is possible at all...Here is what I started with...Thanks


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
74
75
76
#include "time.h"                         // Main.cpp file
 
int main()
{
	Time begin;
	Time end;
	Time check_in;
	

	int h, m, s;
	begin.set (12,0, 0);
	end.set (18, 0, 0);

	cout << " enter hour, min, sec\n ";
	cin >>h>>m>>s;
	check_in.set (h, m, s);
	
	cout << " The time you checked in ";
	check_in.print ();

	if (check_in.Equal(begin))
		cout << "You are right on time. Lets go to lunch !! ";
	h = 0;
	m = 0;
	s = 0;
	if(check_in.LessThan(begin))
	{
		while(!begin.Equal(check_in))
		{
			check_in.increment();
			s++;
			if (s == 60)
	{ 
			m++;
			s = 0;
			if (m == 60)
	{
			h++;
			m = 0;
	}
			if (h == 24)
			h = 0;
	}
}
			cout << " Good Morning you are early. You have to wait "<<h<<":"<<m<<":"<<s<< " for Lunch !! ";
	}
	if (check_in.Equal(end))
		cout << "You are right on time. Lets have Dinner !! ";
			h = 0;
			m = 0;
			s = 0;
	if(check_in.GreaterThan(begin))
	{
		while(!end.Equal(check_in))
		{
			check_in.increment();
			s++;
			if (s == 60)
	{
			m++;
			s = 0;
			if (m == 60)
	{
			h++;
			m = 0;
	}
			if (h == 24)
			h = 0;
	}
}
		
			cout << " Good Afternoon you are late. You have to wait "<<h<<":"<<m<<":"<<s<< " for Dinner !! ";
	
		return 0;
}
}








Yes it is possible

but I would store all the times in an array then print out time[0] and time [9]
that' s a good possibility...but which loop would work with the array...I tried the do, while and for loops but every time it just loop twice and then stop...
I would do this:

string array[]
string prompt

int i = 0
while(){
getInfo(store in prompt)
array[i] = prompt
}
cout << array[0] << array[9]
Thanks a lot...I think I got it now...then all whats left for me to do is to get the difference from array [0] and array [9] computing in secs, mins, and hours....
yup :)
so people...this is what i got finished...but i'm still having problems getting it to print out what I need...it prints the first output correct but the second one is only in seconds...also I'm trying to make it print the first login time...the last login time...and then the difference of the first and last...ALSO...I have to be able to print out the earliest of the 9 logins plus the latest of the 9 logins then get the difference from that....this is what I have...some help please...



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>                          // Time.h file

using namespace std;

class Time  
{
	public:
		void set(int h, int m, int s);
		void print ();
		void increment();
		int get_hour();
		int get_mins();
		int get_secs();
		bool Equal(Time t);
		bool LessThan(Time t);
		bool GreaterThan(Time t);

	private:    
		int hour;
		int mins;       
		int secs;
};

#include "time.h"                          // Time.cpp file

void Time::set(int h, int m, int s)
{
	hour = h;
	mins = m;
	secs = s;
}
void Time::increment()
{ 
	secs ++;
	if (secs == 60)
	{ mins ++;
	secs = 0;
	if ( mins == 60)
	{ hour ++;
	mins = 0;
	}
	if ( hour == 24)
		hour = 0;
	}
}

void Time::print()
{
	if (hour < 10) cout << '0';
		cout << hour << ":";
	if (mins < 10) cout << '0';
		cout << mins << ":";
	if (secs < 10) cout << '0';
		cout << secs << endl;

}
int Time::get_hour()
{
	return hour;
}
int Time::get_mins()
{
	return mins;
}
int Time::get_secs()
{
	return secs;
}

bool Time::Equal(Time t)
{
	if ( hour == t.hour && mins == t.mins && secs == t.secs)
		return true; 
	else 
		return false;
}

bool Time::LessThan(Time t)
{ 
	if(hour < t.hour || hour == t.hour && mins < t.mins || hour == t.hour && mins == t.mins && secs < t.secs)
		return true;
	else 
		return false;
}

bool Time::GreaterThan(Time t)
{ 
	if(hour > t.hour || hour == t.hour && mins > t.mins || hour == t.hour && mins == t.mins && secs > t.secs)
		return true;
	else 
		return false;
}

#include "time.h"  
 
int main()
{
	int h, m, s;
	int num = 9;
	Time *loop;
	loop = new Time[num];
	
	

	for (int i = 0; i<num; i++)
	{
	
	cout << " enter hour, min, sec\n ";
	cin >>h>>m>>s;
	loop[i].set (h, m, s);

	}
	if (loop[9].LessThan(loop[0]))
		cout << "\n";
	loop[0].print();
	loop[9].print();
	
		
		return 0;
}


// I'm having trouble somewhere around the if statement...


If you do not need to use a class consider the following:
for(i=0;<10 loops;inci)
{
<<enter a time
>> time
array[i] = time
}
timediff=array[9]-array[0]
I changed it up and now it prints the first output okay but the last output is only in seconds and then it terminates.....


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
#include "time.h" 
 
int main()
{
	int h, m, s;
	int num = 9;
	Time *loop;
	loop = new Time[num];
	Time diff;
	diff.set(0, 0, 0);
	

	for (int i = 0; i<num; i++)
	{
	
	cout << " Please enter login # " 
		 <<i + 1<<" Hour, Mins, Secs \n";
	cin >>h>>m>>s;
	loop[i].set (h, m, s);
	}
	cout << "First login is: ";
	loop[0].print();
	cout << "\nLast login is: ";
	loop[9].print();
	cout << endl;
	while (loop[9].Equal(loop[0]) != true)
	{
		loop[0].increment();
		diff.increment();
	}
	cout << "The difference is: ";
	diff.print();
	
		
		return 0;
}
The array' loop' should have a size of 10. In your code the for loop will terminate when i=8.
Consider using a double type for decimal hours rather than h,m,s.
Write a function which will convert h,m,s to decimal hours and visa versa.
Topic archived. No new replies allowed.