Having issues with a project.

I am trying to do a simple calculation. I just started C++ and learning about decisions. My program should calculate number of tickets and apply discount based on the number of tickets purchased. It will only execute one of the decisions.

#include <iostream>

using namespace std;

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
int main()
{
double qty; // qty of tickets
double discount; // % of discount.


	cout << "Special Group Pricing is available on 20+ tickets " <<endl << endl;
	cout << "How many tickets to the LA Sparks game would you like? "; // # of tickets on same line.
	cin >> qty;
	cout << endl << endl << endl; // three spaces
	

	qty *= 9.75; //  qty times price.
	discount = qty * .22, qty * .045; // configured the discount amount

	cout << "Retail price for your tickets is $" << qty << endl << endl;

	cout << "The discounted price for your tickets is $" << qty - discount<< endl << endl;
	
	if (qty >= 20)
	{
		qty *= .22;
	}
	
	else
	{
		qty *= .045;
	}

	cout << "Your savings were $" << qty  << endl << endl;
	

	return 0;
}

I am hoping that someone can help.

Thanks,
Last edited on
Use code tags!
cout << endl << endl << endl;
More efficient is:
cout << "\n\n\n";

Arithmatic error? Multiplying by smaller numbers gives smaller answers.

1
2
3
4
if (qty >= 20)
{
qty *= .22;
}


This is higher than the price for getting less than 20 tickets!
1
2
3
4
else
{
qty *= .045;
}


Basically, you want to do something like this:
1
2
3
4
5
6
7
8
9
if(qty >= 20)
{
    //Do everything to do with discounts here.
}
else
{
    qty = qty * 0.22;
    cout << "Price of your tickets is " << qty << "\n";
}
Last edited on
I appreciate your help.

My requirement for this little assignment is;

Required:

You are to use an if / else statement to calculate the appropriate discount (Some of you may think of this as the amount saved).
The discount should be computed within the if / else statement.
We are trying to eliminate as much duplicate code as possible so the dollar amount should only be computed one time after the if / else statement used to compute the discount.
No cin or cout statements within the if / else blocks
1
2
3
4
5
6
7
8
9
10
11
12
//Get value from the user here 

if(qty >= 20)
{
    //Do all calculations to do with discounts here.
}
else
{
    qty = qty * 0.22;
}

cout << "Price of your tickets is " << qty << "\n";
Okay, so I simplified my code and it still does not execute properly. It does not give me the correct discounted price for less than 20 tickets.

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
#include <iostream>

using namespace std;

int main()
{
double qty; // qty of tickets
float discount; // % of discount.


	cout << "Special Group Pricing is available on 20+ tickets " <<endl << endl;
	cout << "How many Tickets to the LA Sparks game would you like? "; // # of tickets on same line.
	cin >> qty;
	cout << endl << endl << endl; // three spaces

	qty *= 9.75; //  qty times price.
	
	if (qty >= 20)
	{
		discount = qty * .22;
	}
	
	else
	{
		discount = qty * .045;
	}
	
	cout << "Retail price for your tickets is $" << qty << endl << endl;
	
	cout << "The discounted price for your tickets is $" << qty - discount<< endl << endl;

	cout << "Your savings were $" << discount  << endl << endl;
	

	return 0;
}
Last edited on
You are doing qty *= 9.75 BEFORE you see if qty >= 20. Better is to do:
1
2
3
4
5
6
 
price = qty * 9.75; 

if(qty >= 20)  //Then this comparison is not affected... 
{
    etc...


And please use code tags. ;)
Last edited on
At that point would I then have to declare what "price" was?

"double price;"

I understand the basics but the trouble shooting part isn't my cup of tea.

I've already declared two doubles, qty and discount, if I try to do "price = qty * 9.75;"

Wouldn't it give me an error because I did not declare what "price" was?

Is there a version for C++ extreme newbies that gives breakdowns?
Yes of course you have to declare any variable before you use it.
Topic archived. No new replies allowed.