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;


}





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
M

Meador Inge

I believe it is becuase you are only using setw for the second field in
your line, thus the beginning of the second field starts at a different
place on each line. I would set the field width for each field output
and ensure that the widths of each corresponding field on each line are
the same. This should fix your problem. For example:
// Start output on the left end of the field
cout << left;
cout << setw(30) << "Movie Name:" << setw(20) << MovieTitle << endl;
cout << setw(30) << "Adult Tickets Sold:" << setw(20) << int(ATicket)
<< endl;
cout << setw(30) << "Children Tickets Sold:" << setw(20) <<
int(CTicket) << endl;
cout << setw(30) << "Gross Box Office Profit:" << setw(20) <<
GrossProfit << endl;
cout << setw(30) << "Net Box Office Profit:" << setw(20) << NetProfit
<< endl;
cout << setw(30) << "Amount Paid to Movie Co.:" << setw(20) <<
AmountPaidCompany << endl;


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
K

kanze

Meador said:
I believe it is becuase you are only using setw for the second
field in your line, thus the beginning of the second field
starts at a different place on each line. I would set the
field width for each field output and ensure that the widths
of each corresponding field on each line are the same. This
should fix your problem. For example:
// Start output on the left end of the field
cout << left;
cout << setw(30) << "Movie Name:" << setw(20) << MovieTitle << endl;
cout << setw(30) << "Adult Tickets Sold:" << setw(20) << int(ATicket)
<< endl;
cout << setw(30) << "Children Tickets Sold:" << setw(20) <<
int(CTicket) << endl;
cout << setw(30) << "Gross Box Office Profit:" << setw(20) <<
GrossProfit << endl;
cout << setw(30) << "Net Box Office Profit:" << setw(20) << NetProfit
<< endl;
cout << setw(30) << "Amount Paid to Movie Co.:" << setw(20) <<
AmountPaidCompany << endl;

Given that the strings are constants, the easiest solution would
probably be to just pad them out manually.

More generally, I tend to use user-defined logical markup. So I
would define manipulators like label, money, etc., and write
something like:

std::cout << label << "Gross Box Office Profit:"
<< money( 20 ) << GrossProfit << std::endl ;

That way, if I decide later that I want to display the monetary
values without cents, I only have to change the code in one
place.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
M

Meador Inge

kanze said:
Given that the strings are constants, the easiest solution would
probably be to just pad them out manually.
Assumming that the string constants don't ever change in the program
text, otherwise it will be a pain to manually repad them everytime they
do change. By having fixed width fields you can just define the width
of the fields in a constant somewhere and change those if need be.
More generally, I tend to use user-defined logical markup. So I
would define manipulators like label, money, etc., and write
something like:

std::cout << label << "Gross Box Office Profit:"
<< money( 20 ) << GrossProfit << std::endl ;

That way, if I decide later that I want to display the monetary
values without cents, I only have to change the code in one
place.
Cool, that's a good idea.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
K

kanze

Assumming that the string constants don't ever change in the
program text, otherwise it will be a pain to manually repad
them everytime they do change. By having fixed width fields
you can just define the width of the fields in a constant
somewhere and change those if need be.

It depends. Typically, I would expect to find such strings in a
table of labels, and not isolated. The table would then look
something like:

char const* labels[] =
{
"Adult Tickets Sold: ",
"Child Tickets Sold: ",
"Gross Box Office Profit: ",
"Net Box Office Profit: ",
"Amount Paid To Movie C.: ",
} ;

When editing, you just use replace mode, instead of insert mode,
in your editor. (And make sure, of course, that you don't
accidentally overwrite one of the closing ".)

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top