Formatting

Discussion in 'C++' started by Richard, Sep 13, 2005.

  1. Richard

    Richard Guest

    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! ]
     
    Richard, Sep 13, 2005
    #1
    1. Advertising

  2. Richard

    Meador Inge Guest

    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! ]
     
    Meador Inge, Sep 14, 2005
    #2
    1. Advertising

  3. Richard

    kanze Guest

    Meador Inge wrote:

    > 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! ]
     
    kanze, Sep 14, 2005
    #3
  4. Richard

    Meador Inge Guest

    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.

    > 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! ]
     
    Meador Inge, Sep 15, 2005
    #4
  5. Richard

    kanze Guest

    Meador Inge wrote:
    > 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! ]
     
    kanze, Sep 15, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. thaneesh

    Code formatting rules in editor

    thaneesh, Jul 2, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,167
    thaneesh
    Jul 2, 2003
  2. Lee Simpson

    Re: Formatting string in WHERE clause

    Lee Simpson, Jul 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,267
    Lee Simpson
    Jul 18, 2003
  3. Dmitry Bond.

    Hexadecimal formatting with leading zeros?!

    Dmitry Bond., Oct 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    8,055
    Oliver Sturm
    Oct 13, 2005
  4. ugly
    Replies:
    0
    Views:
    508
  5. Sonali.NET[MVP]
    Replies:
    2
    Views:
    24,151
    TimDay
    Jul 30, 2006
Loading...

Share This Page