Need to confirm whether my solution is correct or not

Q11. There are three seating categories at a stadium. For a softball game, Class A seats cost $15, Class B seats cost $12, and Class C seats cost $9. Write a program that asks how many tickets for each class of seats were sold, then displays the amount of income generated from ticket sales. Format your dollar amount in fixed-point notation, with two decimal places of precision, and be sure the decimal point is always displayed.

Code:
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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    float A, B, C, ticketsA, ticketsB, ticketsC, total;

    A = 15;
    B = 12;
    C = 9;

    cout << "Enter how many class A tickets were sold" << endl;
    cin >> ticketsA;

    cout << "Enter how many class B tickets were sold" << endl;
    cin >> ticketsB;

    cout << "Enter how many class C tickets were sold" << endl;
    cin >> ticketsC;

    total = (A * ticketsA) + (B * ticketsB) + (C * ticketsC);

    cout << setprecision(2) << fixed;
    cout << "The total amount of income generated is $" << total << endl;

    return 0;
}


Is my code correct according to the question? I was looking about this online and someone said that if you input (ticketsA = 10, ticketsB = 8, ticketsC = 6) the total amount should be displayed as $30.00 something rather than $300.00
Yes, it is correct.
scyp101 wrote:
I was looking about this online and someone said that if you input (ticketsA = 10, ticketsB = 8, ticketsC = 6) the total amount should be displayed as $30.00 something rather than $300.00


Why is the automatic reaction of students to get answers from elsewhere rather than doing their own work? This should at least teach you that there is some garbage online.

I'm sure that you can work out what (10 x 15) + (8 x 12) + (6 x 9) is (in your head). Courage, mon ami!



Unless you plan to sell fractions of a ticket, ticketsA, ticketsB and ticketsC should be ints. The default floating-point type would be double, rather than float, though it's hardly going to matter here.

Other than that, there doesn't appear to be much wrong with your code.
$300.00 is correct (150 + 96 + 54). A small improvement would be to define and initialise at the same time. Also, if the contents of a variable isn't supposed to change, then they can be marked as const which prevents this.

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

using namespace std;

int main()
{
	const float A = 15;
	const float B = 12;
	const float C = 9;

	float ticketsA, ticketsB, ticketsC;

	cout << "Enter how many class A tickets were sold" << endl;
	cin >> ticketsA;

	cout << "Enter how many class B tickets were sold" << endl;
	cin >> ticketsB;

	cout << "Enter how many class C tickets were sold" << endl;
	cin >> ticketsC;

	const float total = (A * ticketsA) + (B * ticketsB) + (C * ticketsC);

	cout << setprecision(2) << fixed;
	cout << "The total amount of income generated is $" << total << endl;

	return 0;
}

Last edited on
Why is the automatic reaction of students to get answers from elsewhere rather than doing their own work? This should at least teach you that there is some garbage online.


I wasn't looking for answers online, I was confused with formatting so I Google about that only. The post I read meanwhile researching confused me even more so I asked here. I know the maths and the difference between $30.00 and $300.00 is visible to me but the problem was that I wasn't quite getting the formatting part of the question. I thought that the question is asking me to format the answer differently
Thank you seeplus for the tip, I will edit that in my program
Registered users can post here. Sign in or register to post.