Can't get apply_discount function to work properly.

In my function definitions under apply_discount, when I enter a 1 or 2 for the discountQues to see if the customer has a coupon, the program thinks I'm entering a ticket type (1=toddler, 2=junior, 3=senior). Can you tell me what I'm doing wrong? I already tried making discountQues a global variable, but that didn't help. I also tried moving the discount question code into main and that got me nowhere as well. The code is supposed to process a formula when I enter whether or not the customer has a discount coupon. Instead, it asks me to enter in the number of tickets again.

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 <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <locale>

using namespace std;


//function prototype
double get_ticket_type();
double find_ticket_price(double ticType);
double apply_discount(double price, double ticType);
void disp_purchase();
void disp_final_report ();

const double  JUNIOR_TIC_PRICE       = 7.50; 
const double  SENIOR_TIC_PRICE       = 11.50;
const double  TODDLER_TIC_PRICE      = 0.0;
const double  DISCOUNT               = 0.25;
const double  MAX_SEAT_COUNT         = 10.0;
      double  discountQues           = 0.0;

int main()

{
	//declare variables
	    
	double        discountQues           = 0.0;
	double        extraTic               = 0.0;
        double        juniorTicTotal         = 0.0;
	double        numTic                 = 0.0;
	double        seniorTicTotal         = 0.0;
	double        ticType                = 0.0;
	double        toddlerTicTotal        = 0.0;
	double        totalCost              = 0.0;
	double        totalNumTic            = 0.0;
	double        totalRevenue           = 0.0;
	double        discountPrice          = 0.0;

 	//welcome the user and explain the purpose of the software
	int consoleWidth = 40;
        std::cout << setw(consoleWidth / 2) << " " << "       PRIOLEAU    
        PROGRAMMING, INC  " << endl;
	std::cout << setw(consoleWidth / 2) << " " << "SUMMERVILLE SEAWORLD 
        TICKETING SOFTWARE 1.0" << endl << endl;
	std::cout << "This program is designed to control, manage and track "
	"ticket sales for Summerville Seaworld. Please input information when "
	"you are prompted." << endl << endl;	
	
	//choose a type of ticket
	ticType = get_ticket_type();//call function to get ticket type
		
	while (ticType == 1 || ticType == 2 || ticType == 3 && ticType != 4)
	{	
	
	//find ticket price
	double price = find_ticket_price(ticType);

	//receive ticket cost, apply discount and return discounted cost
	discountPrice = apply_discount(price, ticType);
	
	//display final report once all seats are sold
	disp_final_report ();

	//display purchase information for 1 completed sale
	disp_purchase();

	} //end while
		
	//choose a type of ticket
	double get_ticket_type(double ticType);
		
	//find ticket price
	double find_ticket_price(double ticType, double TODDLER_TIC_PRICE, 
		double JUNIOR_TIC_PRICE, double SENIOR_TIC_PRICE);

	while (ticType == 4)
	{//while ticket type is toddler
		extraTic = MAX_SEAT_COUNT -totalNumTic;
		
	//display final report
	disp_final_report ();
	
	std::cout << endl;
	system("pause");
	return 0;
	}//end while
	
	std::cout << endl;
	system("pause");
	return 0;
}	//end of main function

//*****function definitions*****
double get_ticket_type()
{
	double ticType; 
	std::cout << "Please select from the following types of tickets: " <<  
        endl << endl;
	std::cout << "NUMBER   TICKET TYPE   AGE RANGE   COST" << endl;
	std::cout << "1        Toddler       0-5 years   Free" << endl;
	std::cout << "2        Junior        6-16 years  $7.50" << endl;
	std::cout << "3        Senior        17 and up   $11.50" << endl;
	std::cout << "4        Quit Program" << endl;
	std::cout << endl << endl;
	
	std::cout << "Which type of ticket would you like to purchase? Enter "
	"1, 2 or 3 to purchase tickets. Enter 4 to Quit:" << endl;
	std::cin >> ticType;
	return ticType;
	std::cout << endl;
}	//end of get_ticket_type function

double find_ticket_price(double ticType)
{
	if (ticType == 1)
	{//if purchase toddler ticket
		std::cout << fixed << setprecision(2);
		std::cout << "You have selected to purchase a Toddler ticket. "
		"Toddler tickets are $" << TODDLER_TIC_PRICE << " each. ";
		return TODDLER_TIC_PRICE;
	}
	
	else if (ticType == 2)
	{//if purchase junior ticket
		std::cout << fixed << setprecision(2);
		std::cout << "You have selected to purchase a Junior ticket. "
		"Junior tickets are $" << JUNIOR_TIC_PRICE << " each. ";
		return JUNIOR_TIC_PRICE;
	}

	else if (ticType == 3)
	{//if purchase senior ticket
		std::cout << fixed << setprecision(2);
		std::cout << "You have selected to purchase a Senior ticket. "
		"Senior tickets are $" << SENIOR_TIC_PRICE << " each. ";
		return SENIOR_TIC_PRICE;
	}
	else;
}	//end find_ticket_price

double apply_discount(double price, double ticType)
{
	double numTic = 0.0;
	double discountQues = 0.0;
	double totalRevenue = 0.0;
	double totalCost = 0.0;
	double discountPrice = 0.0;
	double totalNumTic = 0.0;
	double toddlerTicTotal = 0.0;
	double juniorTicTotal = 0.0;
	double seniorTicTotal = 0.0;

	if (ticType == 1)
	{//if purchase toddler ticket
		std::cout << "How many Toddler tickets would you like to buy?";
		std::cout << " Please enter the number of tickets (1-10): " << 
                endl;
		std::cin >> numTic;
		std::cout << endl;
	}	
	
	if (numTic > 0 && numTic <= 10)
	{//if number of toddler tickets is between 1-10
		std::cout << "Do you have a discount coupon today?" << endl;
		std::cout << "Enter 1 for YES or 2 for NO: " << endl;
		std::cin >> discountQues;
		std::cout << endl;
	}
		else
		{//else the number of tickets is not between 1-10
			std::cout << "The maximum number of tickets that can be 
                        sold "
			" is 10. Please enter a number between 1-10." << endl;
			std::cin >> numTic;
			std::cout << endl;
		}
			if (discountQues == 1 && ticType == 1)
			{//if customer has a discount coupon
				totalCost = TODDLER_TIC_PRICE * numTic;
				discountPrice = totalCost - (totalCost * 
                                DISCOUNT);
			}
			
			else
			{//else the customer does not have a discount coupon
				totalCost = TODDLER_TIC_PRICE * numTic;
			}
	
	//counter and accumulator
	totalNumTic += numTic;
	totalRevenue += totalCost;
	toddlerTicTotal += totalNumTic;

	
	return discountPrice;

}	//end apply_discount
Why are you using a double float to store the ticket type? It can only take integer values. Much safer to use an int, especially since you're comparing it to integer values.

You have a while loop in your main function. As far as I can tell, once it enters the loop, it will loop infinitely, because ticType never changes.

Also, your loop check doesn't make a lot of sense:

(ticType == 1 || ticType == 2 || ticType == 3 && ticType != 4)

The final "&& ticType != 4" is redundant - if you just said:
 
while (ticType == 1 || ticType == 2 || ticType == 3)


then a ticType of 4 would fail to enter the loop anyway.

Edit: Also, what are you trying to achieve with the second while loop in your main function? Why bother using a loop, when you're returning from the function at the end of the first iteration? If can only ever run once or not at all, so why use a while loop?
Last edited on
I have a second loop because I need a certain set of instructions to be processed as a loop every time someone enters a 4. Entering a 4 will show the final display.

This is not my problem, however. The sections you mentioned work fine. I'm having trouble with the apply_discount function. Can you help me with that?
But your second loop isn't processing anything "as a loop". The moment it reaches the end of the first iteration, it hits the return statement and will exit the program (since it's returning from main() ). It will only ever run the code within the loop once at most.
Ok. I can remove the 2nd while loop and leave it as an "if" statement. Now, the important question. What do I change to get the apply_discount function to work?
Topic archived. No new replies allowed.