Time Calculation and Format

Hello,

Im Really lost on a project im playing around with, could you provide me with a good set of code to use?


Here is a list of things it has to have.



• No error checking is necessary. All data in the input file will be as described.
• For 12-hour clock times, the letter that designates whether the time is before or after
noon can be either upper or lowercase (A, a, P, or p).
• There will be 1 or more blanks between the clock type (12 or 24) and the start time. There
will be 1 or more blanks between the start time and the end time.
• There will be 0 or more blanks between the minutes and the before or after noon letter in
a 12-hour time. For example, 5:15p 10:07 A
• The start time will always be less than (precede) or equal to the end time. (Maximum time
interval is 23:59).



Declare all variables inside functions. Only named constants should be declared at the
global level.
• Pass parameters to communicate values between functions.
• Program MUST implement at least 2 meaningful functions (in addition to main).
• Never use goto statements in a program.




Data File
The data file for the program will consist of several sets of data. The first piece of data in each set
will be an integer (12 or 24) that indicates whether the start and end times for an interval will be
expressed using the 12-hour clock or the 24-hour clock.
If the first piece of data is a 12, then it will be followed by 2 times given in the 12-hour clock form.
The first time will mark the start of the time interval and the second time will mark the end. Each
time will be in the form: h:mm or hh:mm. Each time will be followed by an upper or lowercase 'a'
or 'p' to complete the time. All times will be valid.
Sample data sets: 12 4:05P 07:35p 12 10:45A 3:17p
If the first piece of data is a 24, then it will be followed by 2 times given in the 24-hour clock form
that indicate the start and end of a time interval. Each time will be in the form: h:mm or hh:mm.
All times will be valid.
Sample data sets: 24 16:05 19:35 24 10:45 15:17

Correct Out put



Start Time: 03:30pm          End Time: 06:30pm             Enterval Lenght: 03:00




I was playing around with the calculation and ran into a lot of issues.

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
 

#include <iostream>
#include <iomanip>

using namespace std;

int main() 
{
    
    
	double x= 3;
	double y= 30;
	double z= (x * (3600)+ y * (60)) ;
	
	double i= 6;
	double j= 30;
	double l= (i * (3600)+ j * (60)) ;
	
	 cout    << left  << setw(10) << "Start Time: "<< z 
             << right  << setw(20) << "End Time: "<<  l
             << right << setw(30) <<"Enterval Lenght: "<< (z - l)/ 3600
             << endl;



}
Last edited on
For the output you may use strftime:

http://www.cplusplus.com/reference/ctime/strftime/?kw=strftime

Hence you may use the struct tm to store the data:

http://www.cplusplus.com/reference/ctime/tm/

For the input you may use fstream and the operator>>:

http://www.cplusplus.com/reference/fstream/fstream/?kw=fstream
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

at the first step read all space delimited entries to an int for the fromat and std::string for the time.

In order to break the time from the previously read time string into its pieces you may use stringstream and getline():

http://www.cplusplus.com/reference/sstream/stringstream/stringstream/
http://www.cplusplus.com/reference/string/string/getline/?kw=getline
thank you for the help full links. In class we are using a redirection command to feed the data file into the program.


i know i still have to add a while loop to run each line in the file but while testing just one line of input its not working properly.

I was testing with an input of.

12 8:28 a 11:35 A

and getting this out put.



Start Time: 08:028am          End Time: 011:035am             Enterval Lenght: -3.11667



could you help fix it so the output and calculation show correctly?



Start Time: 08:28am          End Time: 11:35am             Enterval Lenght: 03:07




here is the code I need your fixes on, 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

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

using namespace std;

int main() 
{
    string time; // this will add the zero
    string meridiemA; // this will add the AM
    string meridiemP; // this will add the PM
    char ampm1,ampm2; // this will read the A or P
    char colon1, colon2; // this will pick up the colon between the hour and minute. 
    int htype; // this will check if the file has a 12 or a 24 listed. 
  
	double hours1, hours2; // hours 1  is for the start time and hours 2 is for the end time
	double minutes1, minutes2; // minutes 1  is for the start time and minutes 2 is for the end time
	double timesec1; // Start time to seconds 
	double timesec2; // End time to seconds 
	double Interval; // finds the amount of time that has gone by. 
	
	cin>>htype; // this will read the file if it is a 12 or 24
	
	// this section will format the 12 or 24 hour time

    if (htype== 12)
	{
	    //this wiill read the start time and the AMPM as well as the end time. 
	    cin >> hours1 >> colon1 >> minutes1>> ampm1 >> hours2 >> colon2 >> minutes2>> ampm2;
	    
	    // this will add the zero in front of the hour or minute. 
	   if (hours1 < 10 || hours2<10 && minutes1<10 || minutes2<10)
    	{
    	    
    	    time = "0";
    	    
    	}

    // this will check what the a or p should be converted too. 
    	if (ampm1 == 'A'||ampm1 == 'a' || ampm2 == 'A'||ampm2 == 'a')
    	{
    	    meridiemA= "am";
    	    
    	}
    	
	
		if (ampm1 == 'P' || ampm1 == 'p' || ampm2 == 'P' || ampm2 == 'p')
    	{
    	    meridiemP= "pm";
    	    
    	}
    	
        timesec1= (hours1 * (3600)) + minutes1 * (60) ; // coverts the start time to seconds 
	    timesec2= (hours2 * (3600))+ minutes2 * (60) ; // coverts the  end time to seconds
    	Interval = (timesec1 - timesec2) / 3600;// time difference. 
    	
             cout<< left<< setw(10) << "Start Time: "<<time<<hours1<<":"<<time<<minutes1<<meridiemA<<meridiemP
                 << right << setw(20) << "End Time: "<<time<<hours2<<":"<<time<<minutes2<<meridiemA<<meridiemP
                 << right << setw(30) << "Enterval Lenght: "<<Interval
                 << endl;
    	
	    
	}
	
	if (htype== 24)
	{
	    //this wiill read the start time s well as the end time. 
	    cin >> hours1 >> colon1 >> minutes1>>hours2 >> colon2 >> minutes2; 
	    
	    // this will add the zero in front of the hour or minute. 
	    if (hours1 < 10 || hours2<10 || minutes1<10 || minutes2<10)
    	{
    	    
    	    time = "0";
    	    
    	}
    	
    	timesec1= (hours1 * (3600)) + minutes1 * (60) ; // coverts the start time to seconds 
	    timesec2= (hours2 * (3600))+ minutes2 * (60) ; // coverts the  end time to seconds
    	Interval = (timesec1 - timesec2) / 3600;// time difference. 
	    
	       cout  << left<< setw(10) << "Start Time: "<<time<<hours1<<":"<<time<<minutes1<<meridiemA<<meridiemP
                 << right << setw(20) << "End Time: "<<time<<hours2<<":"<<time<<minutes2<<meridiemA<<meridiemP
                 << right << setw(30) << "Enterval Lenght: "<<Interval
                 << endl;
	    
	}
	
	
	
	return 0;


}

Interval is a double, so dividing by 3600 produces a float (with a decimal). You could take that .11667/1 and cross multiply to solve for x with x/60. So (60*.11667)/1=7.0002. That's still a float and since the 667 was rounded up of an irrational 666666666666 probably it wasn't accurately 7.0000000.

Instead you could use modulus % to get the remainder and use int instead of float like this (and you could use secondsToHours(int seconds) as one of the required functions if you want:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

int main(){
  int seconds=11220;
  int minutes=seconds/60;
  int remainingseconds=seconds%60;
  int hours=minutes/60;
  int remainingminutes=minutes%60;
  std::cout<<hours<<"hours "<<remainingminutes<<"remaining minutes "<<remainingseconds<<"remaining seconds ";
}

When you divide int by int it cuts off the remainder. so 11/3=3 and there's 2 remaining. The modulus 11%3 = that 2 remaining. On the other hand 11.0/3.0=3.66666666

The extra 0 is there cuz you cout time before minutes. My guess is you need 2 potential zero variables, one for hours as well as minutes. As is, it seems if hours is <10, you create a zero variable to put on both hours and minutes.
Last edited on
Thank you for the break down on my mistake with using double. My fix was to use a convertion of hours to seconds and the same for the minuets.

could you help me with the formatting/spacing issue im having as well as the last end time in the file adding the ampm. lastly some of the interval times are not showing correctly.

test file.

12 8:28 a 11:35A
24 5:30 14:52
24 00:00 23:59
12 06:05A 4:01 p

my code and compiler (https://onlinegdb.com/ry-YsniJz)



my out put



Start Time: 08:28am          End Time: 11:35am             Enterval Lenght: 03:07
Start Time: 05:30          End Time: 14:52             Enterval Lenght: 09:022
Start Time: 00:00          End Time: 23:59             Enterval Lenght: 023:059
Start Time: 06:05am          End Time: 04:01ampm             Enterval Lenght: 02:04



correct out put should be.


Start Time: 08:28am	End Time: 11:35am	Enterval Lenght: 03:07
Start Time: 05:30	End Time: 14:52		Enterval Lenght: 09:22
Start Time: 00:00	End Time: 23:59		Enterval Lenght: 23:59
Start Time: 06:05am	End Time: 04:01pm	Enterval Lenght: 02:04




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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

int main() 
{
    string time; // this will add the zero
    string timeh1; // this will add the zero
    string timeh2; // this will add the zero
    string timem1; // this will add the zero
    string timem2; // this will add the zero
    string meridiemA1; // this will add the AM
    string meridiemA2; // this will add the AM
    string meridiemP1; // this will add the PM
    string meridiemP2; // this will add the PM
    char ampm1,ampm2; // this will read the A or P
    char colon1, colon2; // this will pick up the colon between the hour and minute. 
    int htype; // this will check if the file has a 12 or a 24 listed. 
  
	double hours1, hours2; // hours 1  is for the start time and hours 2 is for the end time
	double minutes1, minutes2; // minutes 1  is for the start time and minutes 2 is for the end time

	
	cin>>htype; // this will read the file if it is a 12 or 24
	

while (!cin.eof())
{
	
	// this section will format the 12 or 24 hour time

    if (htype== 12)
	{
	    //this wiill read the start time and the AMPM as well as the end time. 
	    cin >> hours1 >> colon1 >> minutes1>> ampm1 >> hours2 >> colon2 >> minutes2>> ampm2;
	    
	    // this will add the zero in front of the hour or minute. 
	   if (hours1 < 10 )
    	{
    	    
    	    timeh1 = "0";
    	    
    	}
    	if ( hours2 < 10)
    	{
    	    
    	    timeh2 = "0";
    	    
    	}
    	if (minutes1<10)
    	{
    	    
    	    timem1 = "0";
    	    
    	}
    	if (minutes2<10)
    	{
    	    
    	    timem2 = "0";
    	    
    	}

    // this will check what the a or p should be converted too. 
    	 if (ampm1 == 'A'||ampm1 == 'a')
    	{
    	    meridiemA1= "am";
    	    
    	}
    	 if (ampm2 == 'A'||ampm2 == 'a')
    	{
    	    meridiemA2= "am";
    	    
    	}
    	
    	if (ampm1 == 'P' || ampm1 == 'p')
    	{
    	    meridiemP1= "pm";
    	    
    	}
	
		if (ampm2 == 'P' || ampm2 == 'p')
    	{
    	    meridiemP2= "pm";
    	    
    	}
    	
    	int timesec1h; // coverts the start hour  to seconds 
	    int timesec2h; // coverts the  end hour to seconds
	    int timesec1m; // coverts the start minutes to seconds 
	    int timesec2m; // coverts the  end minutes to seconds
    	double Intervalh;// time difference for hours. 
    	double Intervalm;// time difference for minutes.
    	string intervaltimem;
    	string intervaltimeh;
    	
    	
        timesec1h= (hours1 * (3600)); // coverts the start hour  to seconds 
	    timesec2h= (hours2 * (3600)); // coverts the  end hour to seconds
	    timesec1m= minutes1 * (60) ; // coverts the start minutes to seconds 
	    timesec2m= minutes2 * (60) ; // coverts the  end minutes to seconds
    	Intervalh = (timesec1h - timesec2h) /3600;// time difference for hours. 
    	Intervalm = (timesec1m - timesec2m) /60 ;// time difference for minutes. 
    	
    	
    if (Intervalh < 10)
    {
        intervaltimeh = "0";
    }
    
        if (Intervalm < 10)
    {
        intervaltimem = "0";
    }
    
    
    	
             cout<< left<< setw(10) << "Start Time: "<<timeh1<<hours1<<":"<<timem1<<minutes1<<meridiemA1<<meridiemP1
                 << right << setw(20) << "End Time: "<<timeh2<<hours2<<":"<<timem2<<minutes2<<meridiemA2<<meridiemP2
                 << right << setw(30) << "Enterval Lenght: "<<intervaltimem<<abs(Intervalh)<<":"<<intervaltimem<<abs(Intervalm)
                 << endl;
    	
	    
	}
	
	if (htype== 24)
	{
	    //this wiill read the start time s well as the end time. 
	    cin >> hours1 >> colon1 >> minutes1>>hours2 >> colon2 >> minutes2; 
	    
	    // this will add the zero in front of the hour or minute. 
	   if (hours1 < 10 )
    	{
    	    
    	    timeh1 = "0";
    	    
    	}
    	if ( hours2 < 10)
    	{
    	    
    	    timeh2 = "0";
    	    
    	}
    	if (minutes1<10)
    	{
    	    
    	    timem1 = "0";
    	    
    	}
    	if (minutes2<10)
    	{
    	    
    	    timem2 = "0";
    	    
    	}
    	
    	int time1hmilitaryh1 ; //  start time to hours 
	    int time2hmilitaryh2; //  end time to hours
	    int time1militarym1;// coverts the start minutes to seconds 
	    int time1militarym2;// coverts the end minutes to seconds 
    	double Intervalmilitaryh;// time difference for hours. 
    	double Intervalmilitarym;// time difference for hours. 
    	string militaryintervaltimem;
    	string militaryintervaltimeh;
    	
    	time1hmilitaryh1= hours1 ; //  start time to hours 
	    time2hmilitaryh2= hours2 ; //  end time to hours
	    time1militarym1= minutes1 * 60;// coverts the start minutes to seconds 
	    time1militarym2= minutes2 * 60;// coverts the end minutes to seconds 
 
    	Intervalmilitaryh = (hours1 - hours2);// time difference for hours. 
    	Intervalmilitarym = (time1militarym1 - time1militarym2) / 60;// time difference for hours. 
    	
    if (Intervalmilitaryh < 10)
    {
        militaryintervaltimeh = "0";
    }
    
    if (Intervalmilitarym < 10)
    {
        militaryintervaltimem = "0";
    }
	    
             cout<< left<< setw(10) << "Start Time: "<<timeh1<<hours1<<":"<<timem1<<minutes1
                 << right << setw(20) << "End Time: "<<timeh2<<hours2<<":"<<timem2<<minutes2
                 << right << setw(30) << "Enterval Lenght: "<<militaryintervaltimeh<<abs(Intervalmilitaryh)<<":"<<militaryintervaltimem<<abs(Intervalmilitarym)
                 << endl;
	    
	}
	
cin>>htype;

}



	return 0;


}

Last edited on
Topic archived. No new replies allowed.