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.
#include <iostream>
#include <iomanip>
usingnamespace 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
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.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
constfloat A = 15;
constfloat B = 12;
constfloat 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;
constfloat total = (A * ticketsA) + (B * ticketsB) + (C * ticketsC);
cout << setprecision(2) << fixed;
cout << "The total amount of income generated is $" << total << endl;
return 0;
}
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