Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,380 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,493 people online right now. Registration is fast and FREE... Join Now!




Formatting Output

 
Reply to this topicStart new topic

Formatting Output, Help formatting output

DewK
5 Feb, 2008 - 04:27 PM
Post #1

New D.I.C Head
*

Joined: 25 Sep, 2007
Posts: 22


My Contributions
I am having trouble figuring out how to format the output towards the end. The only thing I can think of doing is adding spaces. Is there a better way?



CODE

#include <iostream>  
#include <cmath>
#include <iomanip>
using namespace std;
        
        
        
int main()
{
  //Declared Variables
  int tvsold, vcrsold, remsold, cdsold, tapesold;
  double tvcost, vcrcost, remcost, cdcost, tapecost, subtotal, salestax;
  double tv = 400.00, vcr = 220.00, rem = 35.20, cd = 300.00, tape = 150.00;
  double tax = .065;
        
        //Ask for input and accept input.
        cout << "How many TVs were sold? ";
        cin >> tvsold;
        cout << "How many VCRs were sold? ";
        cin >> vcrsold;
        cout << "How many remote controllers were sold? ";
        cin >> remsold;
        cout << "How many CDs were sold? ";
        cin >> cdsold;
        cout << "How many tape recorders were sold? ";
        cin >> tapesold;

        //Determine Costs
        tvcost = tv * tvsold;
        vcrcost = vcr * vcrsold;
        remcost = rem * remsold;
        cdcost = cd * cdsold;
        tapecost = tape * tapesold;
        subtotal = tvcost + vcrcost + remcost + cdcost + tapecost;
        salestax = subtotal * tax;

        //Display output
        cout << "Qty     Description         Unit     Total" << endl;
        cout << "                       Price     Price    " << endl;
        cout << "---     -----------         ----     -----" << endl;
        cout << tvsold <<"        TV "<< tv << tvcost << endl;
        cout << vcrsold <<"        VCR      " <<  vcr << "    " << vcrcost << endl;
                cout << remsold << "        REMOTE CTRLR      " << rem << "   "  << remcost << endl;
        cout << cdsold << "        CD PLAYER     " << cd << "    " <<  cdcost << endl;
        cout << tapesold << "        TAPE RECORDER      " << "   " << tape << "    " <<  tapecost << endl;
        cout << "                               ----------" << endl;
        cout << "                       SUBTOTAL "  <<  subtotal << endl;
        cout << "                       TAX " <<  salestax << endl;
        
return 0;
}
        

User is offlineProfile CardPM
+Quote Post

KYA
RE: Formatting Output
5 Feb, 2008 - 04:29 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,847



Thanked: 159 times
Dream Kudos: 1375
My Contributions
There are a variety of escape characters that you could use:

CODE

\n        //new line
\t        //tab



//so on and so forth


--KYA
User is online!Profile CardPM
+Quote Post

ddollar
RE: Formatting Output
5 Feb, 2008 - 07:19 PM
Post #3

New D.I.C Head
*

Joined: 16 Jan, 2008
Posts: 38



Thanked: 1 times
My Contributions

setw(width)

A bunch of them too.

cout << setw(5) << "Qty" << setw(20) << "Description";
cout << setw(10) << "Unit" << setw(10) << "Total" << endl;

and all the lines that follow. You already included iomanip, so you can use setprecision, fixed and left manipulators. You might like left because normal setw is right.

cout << left << setw(width) << Description; // you might want description left justified.


QUOTE(DewK @ 5 Feb, 2008 - 05:27 PM) *

I am having trouble figuring out how to format the output towards the end. The only thing I can think of doing is adding spaces. Is there a better way?



CODE

#include <iostream>  
#include <cmath>
#include <iomanip>
using namespace std;
        
        
        
int main()
{
  //Declared Variables
  int tvsold, vcrsold, remsold, cdsold, tapesold;
  double tvcost, vcrcost, remcost, cdcost, tapecost, subtotal, salestax;
  double tv = 400.00, vcr = 220.00, rem = 35.20, cd = 300.00, tape = 150.00;
  double tax = .065;
        
        //Ask for input and accept input.
        cout << "How many TVs were sold? ";
        cin >> tvsold;
        cout << "How many VCRs were sold? ";
        cin >> vcrsold;
        cout << "How many remote controllers were sold? ";
        cin >> remsold;
        cout << "How many CDs were sold? ";
        cin >> cdsold;
        cout << "How many tape recorders were sold? ";
        cin >> tapesold;

        //Determine Costs
        tvcost = tv * tvsold;
        vcrcost = vcr * vcrsold;
        remcost = rem * remsold;
        cdcost = cd * cdsold;
        tapecost = tape * tapesold;
        subtotal = tvcost + vcrcost + remcost + cdcost + tapecost;
        salestax = subtotal * tax;

        //Display output
        cout << "Qty     Description         Unit     Total" << endl;
        cout << "                       Price     Price    " << endl;
        cout << "---     -----------         ----     -----" << endl;
        cout << tvsold <<"        TV "<< tv << tvcost << endl;
        cout << vcrsold <<"        VCR      " <<  vcr << "    " << vcrcost << endl;
                cout << remsold << "        REMOTE CTRLR      " << rem << "   "  << remcost << endl;
        cout << cdsold << "        CD PLAYER     " << cd << "    " <<  cdcost << endl;
        cout << tapesold << "        TAPE RECORDER      " << "   " << tape << "    " <<  tapecost << endl;
        cout << "                               ----------" << endl;
        cout << "                       SUBTOTAL "  <<  subtotal << endl;
        cout << "                       TAX " <<  salestax << endl;
        
return 0;
}
        



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:31AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month