Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have a project in my coding class where I have to use if statement for admissions to a park depending on age, I did ok on that part but he then told us to add a sale to for 20% off if you buy 10 or more tickets. When I tried the code it came out like this
Please put down a number of tickets
12
please enter an age
9
Admission is $120
24
admission is $48
240

I am only trying to have one answer and i don't even know how it is putting more than one answer

here is my code so far

C++
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
	int a, b;
	cout << "Please put down a number of tickets" << endl;
	cin >> b;
	cout << "Please enter an age" << endl;
	cin >> a;
	
	if (a<=5)
	{
		cout << "admission is free" << endl;
	}
	
	if (a>=5&& a<=10) 
		cout << " Admission is $";
	{		
		cout << (b * 10) << endl;	
	}
		
	{
			cout << (b * 10) *.2 << endl;
		}

	if (a>10&& a <= 21)
	{ 
		cout << "admission is $" ;
		if (b>10) 
		{ 
			cout << (b *20) * .2 << endl;
		}
		cout << (b*20) << endl;
	}
	if (a<=55)
	{
		cout << "admission is $";
		if (b>10)
		{ 
			cout << (b * 20) * .2 << endl;
		}
		cout << (b*20) << endl;
	}
	if (a>21&& a<55)
	{
		cout << "admission is $";
		if (b>10)
		{ 
			cout << (b* 35) * .2 << endl;
		}
		cout << (b*35) << endl;
	}
	system("pause");
	}


What I have tried:

rearranging my colons, using else statements, I tried using then statements but the program im using wouldn't recognize it. I am missing a header for it? I know the math for the sale is wrong but I will fix it as soon as I know how to fix this
Posted
Updated 10-Sep-19 0:41am
v5
Comments
Richard MacCutchan 9-Sep-19 11:23am    
Please add proper <pre> tags (use the "code" button) around your code, and fix the indentation. Only then will it be readable so we can see where things are going wrong.
Member 13566383 9-Sep-19 15:37pm    
a and b are bad names for variables. Rename them for example into "age" and "ticketCount".

Every time you use cout it will print. It then continues to the next line and executes that.
So when your code does this:
cout << "admission is $";
if (b>10)
{
    cout << (b* 35) * .2 << endl;
}
cout << (b*35) << endl;
It will print two values.

Take out all the
if (b>10)
{
    cout << (b* 35) * .2 << endl;
}
code, and also remove all the preceding couts - replace them with a variable that you calculate:
C++
double toPay = 0.0;
	if (a<=5)
	{
		toPay = 0.0;
	}
Repeat that for all the if conditions, with the appropriate calculations.
Then at the end, check if there are 10 or more people, and apply the discount to the toPay value.
Then print a single line using cout.

If you want to make the code look prettier and easier to read, use else if instead of adding more conditions.
	if (a <= 5)
	{
...
	}
	else if (a <= 10) 
	{
...
	}
	else if (a <= 21)
	{ 
...
	}
	else if (a <= 55)
	{
...
	}
	else 
	{
...
	}
 
Share this answer
 
Comments
CPallini 9-Sep-19 16:04pm    
5.
There are several problems in your code, but for now I'll focus on the 'if' statements:

1. 'if' statements can take any of these forms:
C++
if (/*condition*/)
    ; // empty expression; this ends the if statement without doing anything
if (/*condition*/)
    a = b; // some more or less meaningful expression; the if statement ends at the ';'
if (/*condition*/)
{  // this starts a command block which is executed if 'condition' is true
    a = b;
    c = d;
}  // end of command block, and end of if statement


2. for any of these forms, you can choose to append an 'else' statement. 'else' statements follow the exact same syntax as 'if' statements:
C++
if (/*condition*/)
    ; // end of if
else
    ; // empty statement
if (/*condition*/)
    ; // end of if
else
    a = d; // simple command; the ';' ends the 'else' statement
if (/*condition*/)
    ; // end of if
else
{  // else block with multiple commands
    a = d;
    c = b;
}  // ends the else block


3. As OG pointed out in solution 1, you can nest 'if' statements within an 'if' or 'else' statement. If you choose to do that, it helps if you use the syntax of the last form (i. e. surrounding '{}'):
C++
if (/*condition 1*/)
    c = d; // this will be executed if condition 1 is true
else
{
    if (/*condition 2*/)
        c = a; // this will be executed if condition 1 is false and condition 2 is true
    else
        c = b; // this will be executed if both conditions are false
}

You can choose to omit the surrounding '{}' in that last example, because there is only one statement (the 'if' and following 'else' statement only count as one), but with the brackets it's a little easier to see where each 'else' belongs to.

4. Regarding your code, that second if statement looks a little odd: There is one command and two command blocks following the if, but since the if statement ends after that first command, the two following blocks are executed unconditionally. However, it's clear that these blocks must not be executed always; the if or else conditions are missing. You have to look at the command and the two blocks after the if statement and decide which if them are to be executed under what condition. Then add the corresponding condition (or else) before each command or block.

5. As a more general advice, I suggest separating the conditional pricing from the discount. There is no need to test all combinations of age and group size - you can treat them separately and store the result of your testing in local variables (e. g. base_price based on age, and discount based on group size). Once you know the correct base price and discount, you can do the calculation and output and don't need to repeat variants all over your code. Here's a simpified version:
C++
float base_price;
if (a <= 5)
    base_price = 0.0;
else
{
    if (a <= 10)
        base_price = 10.0;
    else
        base_price = 20.0;
}
float discount = 0.0;
if (b >= 10)
    discount = 0.2;
float fee = base_price * b * (1.0 - discount);
cout << "admission fee = $" << fee << endl;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900