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;


    }
     
    Richard, Sep 13, 2005
    #1
    1. Advertising

  2. Richard

    mlimber Guest

    Richard wrote:
    > 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;

    >
    > }
     
    mlimber, Sep 13, 2005
    #2
    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,191
    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,283
    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,112
    Oliver Sturm
    Oct 13, 2005
  4. ugly
    Replies:
    0
    Views:
    517
  5. Sonali.NET[MVP]
    Replies:
    2
    Views:
    24,241
    TimDay
    Jul 30, 2006
Loading...

Share This Page