function call does not work.

I have a program to take in call times from user, then calculate the call cost.

When i call the callCost(), program gives me 0 as cost irrespective of call times.

I have no idea. Everything builds and compile without problem

 
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Any ideas? Is it my calculations that are not correct?

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
#include<iostream>	
#include<iomanip>	// setw(), setprecition()
#include<string>	
#include<sstream>	//istringstream

using namespace std;
//==========================================================================
//Function prototypes
//void timeCheck(int &hour, char &ch, int &min);
//bool corectTime(int &startHour, int &startMins, int &stopHour, int &stopMins);
double callCost(int &startHour, int &startMins, int &stopHour, int &stopMins);


int main()
{
	
	//Tell user what this program is all about
	cout << endl;
	cout << " Calculate your call cost" << endl; 
	cout << " ------------------------" << endl; 

	//Get start and stop times. 
	string startTime;
	cout << " Enter your stop and start times" << endl << endl;  
	cout << " Start time hh:mm or h:mm: "; 
	getline(cin, startTime);
	
	//Extract the start time from the string
	int hour1, mins1;
	char ch1;
	istringstream startTimeString(startTime);
	startTimeString >> hour1 >> ch1 >> mins1;
	cin.clear();  // clear the stream
	
	cout << " Stop time hh:mm or h:mm: ";
	string stopTime;
	getline(cin, stopTime);

	//Extract the stop time from the string
	int hour2, mins2;
	char ch2;
	istringstream stopTimeString(stopTime);
	stopTimeString >> hour2 >> ch2 >> mins2;
	cin.clear();  // clear the stream

	 
	cout << fixed << setprecision(2);
	cout << " Your call cost is: " << callCost(hour1, mins1, hour2, mins2) << endl; 

	return 0;
}

// fuction definitions

double callCost(int &startHour, int &startMins, int &stopHour, int &stopMins)
{
	// i convert all times in mins by multiplying the hours by 60 and adding the minutes
	    
	int startTimeInMinutes = (startHour * 60) + startMins;
	int stopTimeInMinutes = (stopHour * 60) + stopMins;
	int callTime = stopTimeInMinutes - startTimeInMinutes;
	
	const int pricePerMins = 4; 
	const double vat = 0.25;					// VAT on total price
	const double unconfortableDiscount = 0.65;	// discount applied for calls that
										// fall between 18:30 and 08:00
	const double longCallDiscount = 0.15;		// discount if call time > 30 mins

	double normalPrice = callTime * pricePerMins;
	double normalPricePlusVat = normalPrice * (1 + vat);
	double lowPrice = callTime * (pricePerMins *(1-unconfortableDiscount)); // 65% discount applied for 
		                                                                                                    // calls before 08:00 and after 18:30	
	double lowPricePlusVat = lowPrice * (1 + vat);
	
	// Based on hour of the day, costs are different
	// as mensioned about, all times are converted to mins to get accurate intervals. 
	// eg 18:30 = (18*60 + 30) = 1110 minutes
	double price;
	if (callTime <= 30)  // apply nor mal rate
	{
							
		if (startTimeInMinutes >= 480 && stopTimeInMinutes <= 1110)  // i.e 08:00 to 18:30
			price = normalPricePlusVat;						// normalPrice + normalPrice * 0.25; 		
				
		if (startTimeInMinutes < 480 && stopTimeInMinutes > 1110) // before 08:00 and after 18:30
			price = lowPricePlusVat;
	}

	if (callTime > 30)	// Apply 15% discount to total price
	{
		if (startTimeInMinutes >= 480 && stopTimeInMinutes <= 1110)
			price = normalPricePlusVat*0.85;				// 15% of total price discount applied
												       // price - 0.15*price = price*0.85

		if (startTimeInMinutes < 480 && stopTimeInMinutes > 1110)
			price = lowPricePlusVat*0.85;			
	}
	
	return price; 
}
Last edited on
Actually, I tried it and got an extortionate telephone bill (even by BT standards!)

I haven't checked all your logic, I'm afraid, but if you change BOTH of lines 85 AND 95 to just
else
then I suspect that you will get charged something. Whether that charge is correct or not I haven't checked.
Thanks @Iastchance.
Changing those two lines made the "magic".
Hello blongho,

Without testing the code, I will have to do that later, something you can try.

Line 78 defines price, but does not initialize the variable as it should. Try this and see what happens when you run the program:double price{ 10000 };. If the function returns 10000 then there is a problem in the logic from line 79 after and price is never set.

Hope that helps,

Andy
Hello Andy,
Yes, it was the logic. When i tried initializing price to double price = 1000; and run the code, it gives

 Calculate your call cost
 ------------------------
 Enter your stop and start times

 Start time hh:mm or h:mm: 06:07
 Stop time hh:mm or h:mm: 08:09
 Your call cost is: 1000.00
 
Exit code: 0 (normal program termination)

But as Iastchance proposed, changing both lines 85 and 95 worked perfectly and gives (even with the initialized price) the desired results (below)

Calculate your call cost
 ------------------------
 Enter your stop and start times

 Start time hh:mm or h:mm: 06:07
 Stop time hh:mm or h:mm: 08:09
 Your call cost is: 181.47
 
Exit code: 0 (normal program termination)

Thanks for taking a look at the work.
Last edited on
Topic archived. No new replies allowed.