C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Formatting

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Richard
Guest





PostPosted: Tue Sep 13, 2005 4:47 pm    Post subject: Formatting Reply with quote



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(.Cool;

//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





PostPosted: Wed Sep 14, 2005 7:24 am    Post subject: Re: Formatting Reply with 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;


[ 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





PostPosted: Wed Sep 14, 2005 9:50 am    Post subject: Re: Formatting Reply with quote



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





PostPosted: Thu Sep 15, 2005 1:17 am    Post subject: Re: Formatting Reply with quote

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





PostPosted: Thu Sep 15, 2005 10:14 am    Post subject: Re: Formatting Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.