I had this for classwork and was able to get it to work.. is there a better way to do the right alignments for the cost columns??

When I turned this in, the output returns right, but I was told there was an easier way to get them to align on the decimals, with less code. But I could not find it.

This is what it should look like: ( well it will align when you run the program ).

Description     UnitPrice       Quantity        TotalPrice
----------------------------------------------------------
FlashDrive      $      19.99        10           $   199.90
Ipod            $     300.00         1           $   300.00
Computer        $    1499.00         0           $     0.00
DvdPlayer       $     150.00         2           $   300.00
TV              $     999.00         1           $   999.00
RemoteControl   $       9.99         3           $    29.97
----------------------------------------------------------
Subtotal                                        $  1828.87
Tax [7.5%]                                      $   137.17
----------------------------------------------------------
Total                                           $  1966.04
----------------------------------------------------------


// Description: Write a C++ program to create a customer's bill for an electronic store that sells 6 different products: 
                FlashDrive, IPOD, Computer, DVDPlayer, TV and RemoteControl.  The unit prices are $19.99, $300.00, $1499.00,
                $150.00, 999.00, and $9.99 respectively.  The program should first read the customer’s contact details 
                (name, phone#, and email address) and then read from the keyboard the quantity of each of the product 
                purchased by the customer and calculate the total cost of each item, the subtotal, and the total cost 
                after a 7.5% sales tax.  The customer bill should then display the total amount of payment.
*/

// PRE-PROCESSOR DIRECTIVES
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// CONSTANTS


// main FUNCTION
int main()
{
    // VARIABLE DEFINITIONS
    const double TAX = 0.075;

    string name, phone, email;

    double flashPrice = 19.99, ipodPrice = 300.00, computerPrice = 1499.00, dvdPrice = 150.00, tvPrice = 999.00;
    double remotePrice = 9.99;
    double flashTotal, ipodTotal, computerTotal, dvdTotal, tvTotal, remoteTotal;
    double subTotal, taxAmount, totalAmount;

    int flashQuantity, ipodQuantity, computerQuantity, dvdQuantity, tvQuantity, remoteQuantity;

    // STATEMENTS

    cout << fixed << showpoint << setprecision(2);
    cout << "\t\t******* WELCOME TO OUR ELECTRONIC STORE *******\n\n";
    cout << "Please enter your name: ";
    getline(cin, name);
    cout << "\nPlease enter your phone# (format xxx-xxx-xxxx): ";
    getline(cin, phone);
    cout <<"\nPlease enter your email address: ";
    getline(cin, email);

    cout << "\n\nNow enter the quantity of the products purchased in the order of\n";
    cout << "FlashDrive, IPOD, Computer, DVDPlayer, TV, and RemoteControl\n";
    cout << "(Input all quantities separated by a space): ";
    cin >> flashQuantity >> ipodQuantity >> computerQuantity >> dvdQuantity >> tvQuantity >> remoteQuantity;

    cout << "\n\nCUSTOMER BILL\n";
    cout << "-------------\n";
    cout << "Name:\t" << name << endl;
    cout << "Phone:\t" << phone << endl;
    cout << "Email:\t" << email << endl << endl;

    // compute totals for each item.
    flashTotal = flashPrice * flashQuantity;
    ipodTotal = ipodPrice * ipodQuantity;
    computerTotal = computerPrice * computerQuantity;
    dvdTotal = dvdPrice * dvdQuantity;
    tvTotal = tvPrice * tvQuantity;
    remoteTotal = remotePrice * remoteQuantity;

    cout << "Description\tUnitPrice\tQuantity\tTotalPrice\n";
    cout << "----------------------------------------------------------\n";
    /*cout << left << setw(15) << "FlashDrive"; 
    cout << right << "\t" << setw(5) << "$" << flashPrice << setw(5) << "\t" << flashQuantity
         << "\t" << setw(5) << "$" <<  flashTotal << endl;*/
    cout << left << setw (15) << "FlashDrive" << right << setw(2) << "$" << right << setw(10) << flashPrice 
         << setw (2) << "      " << right << setw(4) << flashQuantity << right << setw(12) << "$" << right << setw(9) << flashTotal << endl;
    cout << left << setw (15) << "Ipod" << right << setw(2) << "$" << right << setw(10) << ipodPrice 
         << setw (2) << "      " << right << setw(4) << ipodQuantity << right << setw(12) << "$" << right << setw(9) << ipodTotal << endl;
    cout << left << setw (15) << "Computer" << right << setw(2) << "$" << right << setw(10) << computerPrice 
         << setw (2) << "      " << right << setw(4) << computerQuantity << right << setw(12) << "$" << right << setw(9) << computerTotal << endl;
    cout << left << setw (15) << "DvdPlayer" << right << setw(2) << "$" << right << setw(10) << dvdPrice 
         << setw (2) << "      " << right << setw(4) << dvdQuantity << right << setw(12) << "$" << right << setw(9) << dvdTotal << endl;
    cout << left << setw (15) << "TV" << right << setw(2) << "$" << right << setw(10) << tvPrice 
         << setw (2) << "      " << right << setw(4) << tvQuantity << right << setw(12) << "$" << right << setw(9) << tvTotal << endl;
    cout << left << setw (15) << "RemoteControl" << right << setw(2) << "$" << right << setw(10) << remotePrice 
         << setw (2) << "      " << right << setw(4) << remoteQuantity << right << setw(12) << "$" << right << setw(9) << remoteTotal << endl;
    cout << "----------------------------------------------------------\n";

    // Compute subtotal, tax and total amounts.
    subTotal = (flashTotal + ipodTotal + computerTotal + dvdTotal + tvTotal + remoteTotal);
    cout << "Subtotal\t\t\t\t\t"  << right << "$" << right << setw(9) << subTotal << endl;

    taxAmount = subTotal * TAX;
    cout << "Tax [7.5%]\t\t\t\t\t" << right << "$" << right << setw(9) << taxAmount << endl;
    cout << "----------------------------------------------------------\n";

    totalAmount =  subTotal + taxAmount;
    cout << "Total\t\t\t\t\t\t" << right << "$" << right << setw(9) << totalAmount << endl;

    cout << "----------------------------------------------------------\n";
    cout << "\t\t******* THANK YOU AND VISIT US AGAIN *******\n\n";

    return 0;
}

Recommended Answers

All 2 Replies

Personally (and I'll get slammed for this :icon_wink:) I'd use printf . It's designed for formatted output, rather than that convoluted cout mess.

the instructor has us using cout and cin only right now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.