Phonecall cost calculator with functions, call by reference

Been staring at this code for quite some time.

this program is supposed to calculate the cost of a phonecall depending on day, start time, and length.

No matter what I do I can't seem to get the output method to recognize the value of callCost - it always displays "0.00" - but when I go into callCalculator method and output the value of callCost it displays correctly.

input should be in the form of "Mo 15:34 34" (day abbreviation, time in 24 hour notation, length of call in minute)


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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Description: This program will receive day (as 2 char abbreviation), time (hour:min), and length of the call in minutes for any number of calls 
//				input. The program will display the information for the call and calculate cost of each call and the total cost for all calls.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

const double WEEKDAY_PEAK = .40; // between 8AM and 6 PM
const double WEEKDAY_OFF_PEAK = .25; // before 8AM or after 6PM
const double WEEKEND = .15; // anytime weekend

const int MORNING = 480; // minute value of 8 AM when "off peak" changes to "peak" on a weekday
const int DAY_EVENING = 1080; // minute value of 6 PM when "peak" changes to "off peak" on a weekday
const int MIDNIGHT = 1440; // minute value of midnight


void input(char& first, char& second, int& hour, int& minute, int& length, int& timeToMinutes, bool& valid);
int dayNamer(char first, char second, string& dayName);
void callCalculator(int day, int timeToMinutes, int length, bool& valid, double& totalCost, double& callCost);
void output(string dayName, int hour, int minute, int length, double callCost);



void main(){
	char first; // first letter of day input
	char second; // second letter of day input
	
	int hour = 0; // hour value input
	int minute = 0; // minute value input
	
	int length; // length (in minutes) of the phone call
	int timeToMinutes; // hour*60 + minute = time in minutes at which the call was started
	double callCost = 0; // cost of individual call being computed
	double totalCost = 0; // total cost of all calls computed

	string dayName;
	bool valid = true;

	do {

		input(first, second, hour, minute, length, timeToMinutes, valid);
		
		if ((first != 'x')&&(first != 'X'))
			{
			callCalculator (dayNamer(first, second, dayName), timeToMinutes, length, valid, callCost, totalCost);
			output (dayName, hour, minute, length, callCost);
			}	
	} while ((first != 'x')&&(first != 'X'));
		
	cout << "Total: \t\t\t\t\t$" << totalCost;
	
}

void input(char& first, char& second, int& hour, int& minute, int& length, int& timeToMinutes, bool& valid){
	
	char dummy; // handles colon between 'hour' and minute' values

	cout << "Please enter first two letters of day of the week, followed by "
			"call time in 24-hour notation, followed by call length in minutes.\n\n";
	
	cin >> first;

	if ((first != 'x')&&(first != 'X'))
	{
		cin >> second >> hour >> dummy >> minute >> length;
	
	
		if ((hour>23)||(minute>60))
		{
			cout << "\tInvalid Time";
			valid = false;
		}

	
		if (length > 300) 
		{
			length = 300;
		}
			
		timeToMinutes = hour*60 + minute;
	}


	else 
		cout << "Bye-bye!";
}

int dayNamer(char first, char second, string& dayName){
	
	int day = 9;

			if ((first == 'm')||(first == 'M'))
			{
				if ((second == 'o')||(second == 'O'))
				{
					day = 0;
					dayName = "Monday";
				}
			}

			else if ((first == 't')||(first == 'T'))
			{
				
				if ((second == 'u')||(second == 'U'))
				{
					day = 1;
					dayName = "Tuesday";
				}
				
				else if ((second == 'h')||(second == 'H'))
				{
					day = 3;
					dayName = "Thursday";
				}
				
			}

			else if ((first == 'w')||(first == 'W'))
			{
				if ((second == 'e')||(second == 'E'))
				{
					day = 2;
					dayName = "Wednesday";
				}
				
			}

			else if ((first == 'f')||(first == 'F'))
			{
				if ((second == 'r')||(second == 'R'))
				{
					day = 4;
					dayName = "Friday";
				}
			}

			else if ((first == 's')||(first == 'S'))
			{
				if ((second == 'a')||(second == 'A'))
				{
					day = 5;
					dayName = "Saturday";
				}

				else if ((second == 'u')||(second == 'U'))
				{
					day = 6;
					dayName = "Sunday";
				}

			}
			
			return day;
		}

void callCalculator(int day, int timeToMinutes, int length, bool& valid, double& totalCost, double& callCost)
{
	double callCost2 = 0;
		
			switch (day)
			{

				case 0: // MO
			
				case 1: // TU
				
				case 2: // WE
			
				case 3: // TH
				{ 
					if (timeToMinutes < MORNING){ // start time is before 480 (8 AM)
						if (timeToMinutes + length < MORNING) // end time is before 480 (8 AM)
						{
							callCost = length * WEEKDAY_OFF_PEAK;
						}
						else // end time is after 480
						{
							callCost = ((MORNING - timeToMinutes) *WEEKDAY_OFF_PEAK);
							callCost2 = (((timeToMinutes + length) - MORNING) *WEEKDAY_PEAK);
							
							callCost = callCost + callCost2;
						}
					}
					else if ((timeToMinutes > MORNING)&&(timeToMinutes < DAY_EVENING)){ // start time is between 8 AM and 6 PM
						if (timeToMinutes + length < DAY_EVENING) // end time is before 6 PM
						{
							callCost = length * WEEKDAY_PEAK;
						}
						else // end time is after 6 PM
						{
							callCost = ((DAY_EVENING - timeToMinutes) * WEEKDAY_PEAK); 
							callCost2 = (((timeToMinutes + length) - DAY_EVENING) * WEEKDAY_OFF_PEAK);
							
							callCost = callCost + callCost2;
						}
					}
					else // start time is after 6 PM
					{
							callCost = length * WEEKDAY_OFF_PEAK;
					}
					break;
				}
				
				case 4: // F
				{ 
					if (timeToMinutes < MORNING){ // start time is before 480 (8 AM)
						if (timeToMinutes + length < MORNING) // end time is before 480 (8 AM)
						{
							callCost = length * WEEKDAY_OFF_PEAK;
				
						}
						else // end time is after 480
						{
							callCost = ((MORNING - timeToMinutes) *WEEKDAY_OFF_PEAK);
							callCost2 = (((timeToMinutes + length) - MORNING) *WEEKDAY_PEAK);
							
							callCost = callCost + callCost2;
				
						}
					}
					else if ((timeToMinutes > MORNING)&&(timeToMinutes < DAY_EVENING)){ // start time is between 8 AM and 6 PM
						if (timeToMinutes + length < DAY_EVENING) // end time is before 6 PM
						{
							callCost = length * WEEKDAY_PEAK;
					
						}
						else // end time is after 6 PM
						{
							callCost = ((DAY_EVENING - timeToMinutes) * WEEKDAY_PEAK); 
							callCost2 = (((timeToMinutes + length) - DAY_EVENING) * WEEKDAY_OFF_PEAK);
							
							callCost = callCost + callCost2;
							
						}
					}
					else // start time is after 6 PM
					{
						if (timeToMinutes + length < MIDNIGHT) //Ends before midnight
						{
							callCost = length * WEEKDAY_OFF_PEAK;
							
						}
						else // goes into weekend
						{
							callCost = ((MIDNIGHT - timeToMinutes) * WEEKDAY_OFF_PEAK);
							callCost2 = (((timeToMinutes + length) - MIDNIGHT) * WEEKEND);
							
							callCost = callCost + callCost2;
						
						}
					}
					break;
				}
				case 5: //SA
				{
					callCost = length* WEEKEND;
					
					break;
				}

				case 6: //SU
				{
					if ((timeToMinutes + length) < MIDNIGHT)
					{
						callCost = length * WEEKEND;
					
					}
					else // call goes into Monday
					{
						callCost = ((MIDNIGHT - timeToMinutes) * WEEKEND); 
						callCost2 = (((timeToMinutes + length) - MIDNIGHT)*WEEKDAY_OFF_PEAK);
						
						callCost = callCost + callCost2;
						
					}
					break;
					
				}
				default:
				{
					cout << "Invalid Day" << endl;
					valid = false;
				}
		
			totalCost += callCost;


	}
}

void output(string dayName, int hour, int minute, int length, double callCost){
	

			cout.setf(ios::fixed);
			cout.setf(ios::showpoint);
			cout.precision(2);

			cout << dayName << "\t"; 

			if (hour > 12)
				cout << (hour - 12) << ":" << minute << " PM\t" << length << "\t$" << callCost << endl;
			else if (hour == 12)
				cout << hour << ":" << minute << " PM\t" << length << "\t$" << callCost << endl;
			else 
				cout << hour << ":" << minute << " AM\t" << length  << "\t$" << callCost << endl;  
}





line 46 you've switched callcost and totalcost.
Topic archived. No new replies allowed.