Showpoint fixed and other stuff

This is a discussion on Showpoint fixed and other stuff within the C++ Programming forums, part of the General Programming Boards category; Code: // September 27, 2003 // Takes input numbers for a register and calculates cost #include <iostream.h> #include <iomanip.h> #define ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    26

    Showpoint fixed and other stuff

    Code:
    // September 27, 2003
    // Takes input numbers for a register and calculates cost
    
    #include <iostream.h>
    #include <iomanip.h>
    #define __USE_STD_IOSTREAM
    
    int main(void)
    
    {
    
    
    	// Declarations
    	int TV = 0;
    	int VCR = 0;
    	int CTRLR = 0;
    	int CDS = 0;
    	int RCRDR = 0;
    	float subtotal = 0;
    	float sales_tax = 0;
    	float total = 0;
    	float TVcost = 0;
    	float VCRcost = 0;
    	float CTRLRcost = 0;
    	float CDScost = 0;
    	float RCRDRcost = 0;
    
    	// Output / Input
    	cout << "\n\nHow many TVs were sold? ";
    	cin >> TV;
    	cout << "\nHow many VCRs were sold? ";
    	cin >> VCR;
    	cout << "\nHow many remote controls were sold? ";
    	cin >> CTRLR;
    	cout << "\nHow many CDs were sold? ";
    	cin >> CDS;
    	cout << "\nHow many tape recorders were sold? ";
    	cin >> RCRDR;
    	cout << "\n\n";
    	
    	// Calculations
    	TVcost = TV * 400;
    	VCRcost = VCR * 220;
    	CTRLRcost = CTRLR * 35.20;
    	CDScost = CDS * 300;
    	RCRDRcost = RCRDR * 150;
    	subtotal = TVcost + VCRcost + CTRLRcost + CDScost + RCRDRcost;
    	sales_tax = subtotal * .825;
    	total = subtotal + sales_tax;
    
    	// Output
    
    	cout << "QTY  DESCRIPTION    UNIT PRICE   TOTAL PRICE\n"
    	     << "---  -----------    ----------   -----------\n"
    	     << TV << setw(3) << "     TV 400.00";
    	cout << setprecision(2) << showpoint << fixed << TVcost << "\n";
    	cout << VCR << setw(3) << "     VCR 220.00" << VCRcost << "\n"
    	     << CTRLR << setw(3) <<"     REMOTE CTRLR 35.20" << CTRLRcost << "\n"
    	     << CDS << setw(3) << "     CD PLAYER 300.00" << CDScost << "\n"
    	     << RCRDR << setw(3) << "     TAPE RECORDER 150.00"<< RCRDRcost << "\n"
    	     << setw(45) << "-----------\n"
    	     << "SUBTOTAL " << setfill('*') << setw(7) << subtotal << "\n"
    	     << "TAX" << setfill(' ') << setw(15) << sales_tax << "\n"
    	     << "TOTAL" << setw(13) << total << "\n\n";
    
    	return 0; // Terminate main
    }
    Yes I know thats messy. The program is supposed to display a table of what quantity you entered, description, and cost, etc. I have a few problems I can't seem to fix. If theres more than single digits inside the quantity column the description won't line up. Heres sorta what I'm talking about:

    Code:
    QTY  DESCRIPTION
    ---   ------------------
    1      TV
    10       VCR
    That and I can't get fixed and showpoint to work. And once I get them working I can't figure out how to get them to only work on the total price of things so they will display two decimals, and not have the quantity showing decimals.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You could use SetConsoleCursorPosition() to move the cursor and then write your text, that way spacing wouldnt matter...Check out
    adrian's tutorials
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    26
    That completely lost me.

    *Edit*

    This is making absolutely no sense to me. Heres the table section of code I currently have.

    Code:
    cout << "QTY   DESCRIPTION    UNIT PRICE   TOTAL PRICE\n"
    	     << "---   -----------    ----------   -----------\n"
    	     << TV << setw(14) << "TV 400.00" << TVcost << "\n"
    	     << VCR << setw(14) << "VCR 220.00" << VCRcost << "\n"
    	     << CTRLR << setw(14) <<"REMOTE CTRLR 35.20" << CTRLRcost << "\n"
    	     << CDS << setw(14) << "CD PLAYER 300.00" << CDScost << "\n"
    	     << RCRDR << setw(14) << "TAPE RECORDER 150.00"<< RCRDRcost << "\n"
    	     << setw(46) << "-----------\n"
    	     << "SUBTOTAL " << setfill('*') << setw(7) << subtotal << "\n"
    	     << "TAX" << setfill(' ') << setw(15) << sales_tax << "\n"
    	     << "TOTAL" << setw(13) << total << "\n\n";
    And it displays this after I compile:
    Code:
    QTY   DESCRIPTION    UNIT PRICE   TOTAL PRICE
    ---   -----------    ----------   -----------
    1     TV 400.00400
    1    VCR 220.00220
    1REMOTE CTRLR 35.2035.2
    1CD PLAYER 300.00300
    1TAPE RECORDER 150.00150
                                      -----------
    SUBTOTAL *1105.2
    TAX         911.79
    TOTAL      2016.99
    Why aren't the setw working?
    Last edited by OttoDestruct; 09-28-2003 at 03:43 PM.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    22

    try this

    format for one number at a time...

    Code:
    cout << setiosflags(ios::fixed) // fixed
            << setiosflags(ios::showpoint) // always show decimal
            << setprecision(2) // two decimal places
            << setw (14) // field with to 14
            << number // the number you want to display
            << endl;
    you should also format certain things to be left and right justified...

    this might help to...you need to unset flags after (like justification)

    eg
    cout.unsetf(ios::left)

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    26
    Thanks for the help the setflag stuff worked and I figured out the other problems.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21