Formatting

R

Richard

I am new to C++ and I do not understand the formatting code very well. I
need to format my output to look like this:

Movie Name: "Death Grip"
Adult Tickets Sold: 378
Child Tickets Sold: 127
Gross Box Office Profit: $ 2673.00
Net Box Office Profit: $ 534.60
Amount Paid To Movie C.:$ 2138.40

The C++ code are below. I cannot make the result look like the above for
some reason because I do not get the format code:

#include <iostream>

#include <iomanip>

using namespace std;

void main()

{

float AdultTicket=6, ChildrenTicket=3, ATicket, CTicket,
GrossProfit,NetProfit,AmountPaidCompany;

char MovieTitle[80];

// ask for information

cout<<"what is the name of the movie?"<<endl;

cin.getline(MovieTitle,80);

cout<<endl;

cout<<"How many adult tickets were sold?"<<endl;

cin>>ATicket;

cout<<endl;


cout<<"How many Children tickets were sold?"<<endl;

cin>>CTicket;

cout<<endl;


//calculating profit

GrossProfit=(ATicket*AdultTicket)+(CTicket*ChildrenTicket);

NetProfit=GrossProfit* float(.2);

AmountPaidCompany=GrossProfit* float(.8);

//Display data

cout<<setprecision(2);

cout.setf(ios::fixed | ios::showpoint);

cout<<"Movie Name:"<<setw(20)<<MovieTitle<<endl;

cout<<"Adult Tickets Sold:"<<setw(12)<<int(ATicket)<<endl;

cout<<"Children Tickets Sold:"<<setw(9)<<int(CTicket)<<endl;

cout<<"Gross Box Office Profit:"<<setw(7)<<GrossProfit<<endl;

cout<<"Net Box Office Profit:"<<setw(9)<<NetProfit<<endl;

cout<<"Amount Paid to Movie Co.:"<<setw(6)<<AmountPaidCompany<<endl;


}
 
M

mlimber

Richard said:
I am new to C++ and I do not understand the formatting code very well. I
need to format my output to look like this:

Movie Name: "Death Grip"
Adult Tickets Sold: 378
Child Tickets Sold: 127
Gross Box Office Profit: $ 2673.00
Net Box Office Profit: $ 534.60
Amount Paid To Movie C.:$ 2138.40

I presume you want things lined up somehow. You just need to adjust
your field widths below and perhaps play with the ios_base::adjustfield
flag. Give more details on what you want, and we can help you more.
The C++ code are below. I cannot make the result look like the above for
some reason because I do not get the format code:

#include <iostream>
#include <iomanip>

using namespace std;

void main()

int main()
{

float AdultTicket=6, ChildrenTicket=3, ATicket, CTicket,
GrossProfit,NetProfit,AmountPaidCompany;

AdultTicket and ChildrenTicket should be const, and ATicket and CTicket
could be ints (or even unsigned ints) instead of floats because you can
never have fractional ticket sales. Unlike in C, you can declare
variables just before you use them. That generally makes things
clearer, and it would allow GrossProfit, NetProfit, and
AmountPaidCompany to be declared const also. Declare as many things as
possible const.
char MovieTitle[80];

// ask for information

cout<<"what is the name of the movie?"<<endl;

cin.getline(MovieTitle,80);

You could use std::string here instead:

string movieTitle;
//cin >> movieTitle; // Doesn't allow spaces
getline( cin, movieTitle );
cout<<endl;

cout<<"How many adult tickets were sold?"<<endl;

cin>>ATicket;

cout<<endl;


cout<<"How many Children tickets were sold?"<<endl;

cin>>CTicket;

cout<<endl;


//calculating profit

GrossProfit=(ATicket*AdultTicket)+(CTicket*ChildrenTicket);

NetProfit=GrossProfit* float(.2);

AmountPaidCompany=GrossProfit* float(.8);

As per my comments above:

const float GrossProfit = ATicket * AdultTicket
+ CTicket * ChildrenTicket;
const float NetProfit = GrossProfit* 0.2f;
const float AmountPaidCompany = GrossProfit* 0.8f;
//Display data

cout<<setprecision(2);

cout.setf(ios::fixed | ios::showpoint);

cout<<"Movie Name:"<<setw(20)<<MovieTitle<<endl;

cout<<"Adult Tickets Sold:"<<setw(12)<<int(ATicket)<<endl;

cout<<"Children Tickets Sold:"<<setw(9)<<int(CTicket)<<endl;

cout<<"Gross Box Office Profit:"<<setw(7)<<GrossProfit<<endl;

cout<<"Net Box Office Profit:"<<setw(9)<<NetProfit<<endl;

cout<<"Amount Paid to Movie Co.:"<<setw(6)<<AmountPaidCompany<<endl;

return 0;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top