Program help

Problem: A movie theater only keeps a percentage of the revenue earned from ticket sales. The
remainder goes to the movie distributor. Write a program that calculates a theater’s
gross and net box office profit for a night. The program should ask for the name of the
movie, and how many adult and child tickets were sold. (The price of an adult ticket is
$10.00 and a child’s ticket is $6.00.) It should display a report similar to
Movie Name: “Wheels of Fury”
Adult Tickets Sold: 382
Child Tickets Sold: 127
Gross Box Office Profit: $ 4582.00
Net Box Office Profit: $ 916.40
Amount Paid to Distributor: $ 3665.60

--------------------------------

I have the basic concept down actually, and my output is correct. My only issue is with regards to the "setw" stuff. I find it really annoying I need to match it perfectly, but I was wondering just for the part on the "SETW" if I did it correct.

Also, in some other programs that were similar to the ones I saw doing, they put "SETW(10)" lets say, with "Right" or "Left". I am confused why they even needs to do this, I just used SETW and it came out correct. Do I need to use both? Or is my program fine the way it is and I am over-thinking it. Thanks a bunch. :)

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
	double gross;
	double box_Office_Profit;
	double Amount_Paid_To_Distributor;
	string Movie_Name;
	double adult_Tickets;
	double child_Tickets;
	const double Adult_Ticket_Price = 10.00;
	const double child_Ticket_Price = 6.00;
	const double PERCENT_PROFIT = .20;

	cout << "What is the name of the movie you wish to see?";;
	getline(cin, Movie_Name);

	cout << "How many adult tickets were sold? ";
	cin >> adult_Tickets;
	cin.ignore();

	cout << "How many child tickets were sold? ";
	cin >> child_Tickets;
	cin.ignore();

	adult_Tickets *= Adult_Ticket_Price;
	child_Tickets *= child_Ticket_Price;
	gross = (adult_Tickets + child_Tickets);
	box_Office_Profit = (gross * PERCENT_PROFIT);
	Amount_Paid_To_Distributor = gross - box_Office_Profit;

	cout << setprecision(2) << showpoint << fixed;
	cout << "Movie Name: " << setw(35) << Movie_Name << endl;
	cout << "Adult Tickets Sold: " << setw(25) << adult_Tickets << endl;
	cout << "Child Tickets Sold: " << setw(25) << child_Tickets << endl;
	cout << "Gross Box Office Profit: " << setw(15) <<  "$" << gross << endl;
	cout << "Net Box Office Profit: " << setw(17) << "$" << box_Office_Profit << endl;
	cout << "Amount Paid to Distributor: " << setw(12) << "$" << Amount_Paid_To_Distributor << endl;

return 0;
}
closed account (48T7M4Gy)
right and left places the value left or right justified within the column of width set by setw.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
cout << setprecision(2) << showpoint << fixed;
    
    cout << "                Movie Name: " << Movie_Name << endl;
    cout << "        Adult Tickets Sold: $" << setw(10) << right << adult_Tickets << endl;
    cout << "        Child Tickets Sold: $" << setw(10) << right << child_Tickets << endl;
    cout << "   Gross Box Office Profit: $" << setw(10) << right << gross << endl;
    cout << "     Net Box Office Profit: $" << setw(10) << right << box_Office_Profit << endl;
    cout << "Amount Paid to Distributor: $" << setw(10) << right << Amount_Paid_To_Distributor << endl;


Or nothing at all except dollars and cents precision:

1
2
3
4
5
6
7
8
cout << setprecision(2) << showpoint << fixed;
    
    cout << "Movie Name: " << Movie_Name << endl;
    cout << "Adult Tickets Sold: $" << adult_Tickets << endl;
    cout << "Child Tickets Sold: $" << child_Tickets << endl;
    cout << "Gross Box Office Profit: $" << gross << endl;
    cout << "Net Box Office Profit: $" << box_Office_Profit << endl;
    cout << "Amount Paid to Distributor: $" << Amount_Paid_To_Distributor << endl;


closed account (48T7M4Gy)
https://stackoverflow.com/questions/6373513/currency-formatting-with-c
I am still a bit confused on this, is there any way with the current code I have I can implement right? The way you did it, it came out perfectly and if I enter a huge number for both adult tickets and child tickets, it's lined up perfectly, while with my code if I enter a huge number, the lining is bad but still correct. Thank you.

1
2
3
4
5
6
7
        cout << setprecision(2) << showpoint << fixed;
	cout << "Movie Name: " << setw(35) << Movie_Name << endl; 
	cout << "Adult Tickets Sold: " << setw(25) << adult_Tickets << endl;
	cout << "Child Tickets Sold: " << setw(25) << child_Tickets << endl;
	cout << "Gross Box Office Profit: " << setw(15) <<  "$" << gross << endl;
	cout << "Net Box Office Profit: " << setw(17) << "$" << box_Office_Profit << endl;
	cout << "Amount Paid to Distributor: " << setw(12) << "$" << Amount_Paid_To_Distributor << endl;
closed account (48T7M4Gy)
The problem you are having seems to be because it's not clear to you that setw(xyz) creates the next column width and right or left determines how the data is displayed in that column.

You haven't defined the width of the first column.
Try it by starting each row in your original program with cout << setw(25) << left ... etc and play around with the first column.

Last edited on
closed account (48T7M4Gy)

1
2
3
4
5
6
7
8
9
10
11
int col1 = 30; // width of column 1
    int col2 = 15;
    
    cout << setprecision(2) << showpoint << fixed;
    
    cout << setw(col1) << left << "Movie Name: " << Movie_Name << endl;
    cout << setw(col1) << right << "Adult Tickets Sold: $" << setw(col2) << right << adult_Tickets << endl;
    cout << setw(col1) << right << "Child Tickets Sold: $" << setw(col2) << right << child_Tickets << endl;
    cout << setw(col1) << right << "Gross Box Office Profit: $" << setw(col2) << right << gross << endl;
    cout << setw(col1) << right << "Net Box Office Profit: $" << setw(col2) << right << box_Office_Profit << endl;
    cout << setw(col1) << right << "Amount Paid to Distributor: $" << setw(col2) << right << Amount_Paid_To_Distributor << endl;


What is the name of the movie you wish to see? Mickey Mouse
How many adult tickets were sold? 100000
How many child tickets were sold? 200000
Movie Name:                   Mickey Mouse
         Adult Tickets Sold: $     1000000.00
         Child Tickets Sold: $     1200000.00
    Gross Box Office Profit: $     2200000.00
      Net Box Office Profit: $      440000.00
 Amount Paid to Distributor: $     1760000.00
Program ended with exit code: 0
Ok, so after playing around with it, I still have some question on it. Here is my code so far. Why is it that left setw(40) is bigger than right setw(20)? Also, I still played around with it and I actually removed the Right keyword, and it still came out correct? Is right really needed? Thanks!

1
2
3
4
5
6
        cout << left << setw(40) << "Movie Name: " << setw(20) << Movie_Name << endl;
	cout << left << setw(40) << "Adult Tickets Sold: " <<  setw(20) << adult_Tickets << endl;
	cout << left << setw(40) << "Child Tickets Sold: " <<  setw(20) << child_Tickets << endl;
	cout << left << setw(40) << "Gross Box Office Profit: " << setw(20) << gross << endl;
	cout << left << setw(40) << "Net Box Office Profit: " << setw(20) << box_Office_Profit << endl;
	cout << left << setw(40) << "Amount Paid to Distributor: " << setw(20) << Amount_Paid_To_Distributor << endl;


Can I just think about it like this? Every time there is something like this, the left side of SETW must be bigger than the right? As in your code, col1 is bigger than col2 obviously but after playing around with your original code, I made it the right output. Col1 is still bigger than col2 no matter what. This may be a bad strategy but it works, even though I don't know why.

1
2
3
4
5
6
7
8
9
10
11
int col1 = 35; // width of column 1
	int col2 = 15;

	cout << setprecision(2) << showpoint << fixed;

	cout << setw(col1) << left << "Movie Name: " << setw(col2) << right << Movie_Name << endl;
	cout << setw(col1) << left << "Adult Tickets Sold: $" << setw(col2) << right << adult_Tickets << endl;
	cout << setw(col1) << left << "Child Tickets Sold: $" << setw(col2) << right << child_Tickets << endl;
	cout << setw(col1) << left << "Gross Box Office Profit: $" << setw(col2) << right << gross << endl;
	cout << setw(col1) << left << "Net Box Office Profit: $" << setw(col2) << right << box_Office_Profit << endl;
	cout << setw(col1) << left << "Amount Paid to Distributor: $" << setw(col2) << right << Amount_Paid_To_Distributor << endl;


Also with the same code as above, I removed the "right" and it still worked, as to why I am still confused.

1
2
3
4
5
6
7
8
9
10
11
int col1 = 35; // width of column 1
	int col2 = 15;

	cout << setprecision(2) << showpoint << fixed;

	cout << setw(col1) << left << "Movie Name: " << setw(col2) << right << Movie_Name << endl;
	cout << setw(col1) << left << "Adult Tickets Sold: $" << setw(col2) << right << adult_Tickets << endl;
	cout << setw(col1) << left << "Child Tickets Sold: $" << setw(col2) << right << child_Tickets << endl;
	cout << setw(col1) << left << "Gross Box Office Profit: $" << setw(col2) << right << gross << endl;
	cout << setw(col1) << left << "Net Box Office Profit: $" << setw(col2) << right << box_Office_Profit << endl;
	cout << setw(col1) << left << "Amount Paid to Distributor: $" << setw(col2) << right << Amount_Paid_To_Distributor << endl;
closed account (48T7M4Gy)
I'm not a 100% sure what you mean but the bottom line is the columns have to be wide enough to accommodate the data. Have a play around with the column widths and see what you get. In particular with the numbers.

Try using the (ridiculous) 100,000 adults and make col2 = 2. the structure gets overridden because the column is too narrow.

Same thing for left and right or no command, try it out, preferably with sensible/realistic values though.

This goes into great detail with links to right and left filling.
http://en.cppreference.com/w/cpp/io/manip/setw
Last edited on
Thank you, with the links you provided and the example you gave me, I managed to understand it. I will mark as resolved. :)
Topic archived. No new replies allowed.