 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Richard Guest
|
Posted: Tue Sep 13, 2005 4:47 pm Post subject: Formatting |
|
|
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?"<
cin.getline(MovieTitle,80);
cout<
cout<<"How many adult tickets were sold?"<
cin>>ATicket;
cout<
cout<<"How many Children tickets were sold?"<
cin>>CTicket;
cout<
//calculating profit
GrossProfit=(ATicket*AdultTicket)+(CTicket*ChildrenTicket);
NetProfit=GrossProfit* float(.2);
AmountPaidCompany=GrossProfit* float(. ;
//Display data
cout<
cout.setf(ios::fixed | ios::showpoint);
cout<<"Movie Name:"<
cout<<"Adult Tickets Sold:"<
cout<<"Children Tickets Sold:"<
cout<<"Gross Box Office Profit:"<
cout<<"Net Box Office Profit:"<
cout<<"Amount Paid to Movie Co.:"<
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Meador Inge Guest
|
Posted: Wed Sep 14, 2005 7:24 am Post subject: Re: Formatting |
|
|
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! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Wed Sep 14, 2005 9:50 am Post subject: Re: Formatting |
|
|
Meador Inge wrote:
| Quote: | 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! ]
|
|
| Back to top |
|
 |
Meador Inge Guest
|
Posted: Thu Sep 15, 2005 1:17 am Post subject: Re: Formatting |
|
|
kanze wrote:
| Quote: | 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.
| Quote: | 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! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Thu Sep 15, 2005 10:14 am Post subject: Re: Formatting |
|
|
Meador Inge wrote:
| Quote: | kanze wrote:
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.
|
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! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|