944,035 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1401
  • C++ RSS
Oct 23rd, 2007
0

Better way to make the columns align?

Expand Post »
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 ).

[code=c++]

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
----------------------------------------------------------

C++ Syntax (Toggle Plain Text)
  1. // Description: Write a C++ program to create a customer's bill for an electronic store that sells 6 different products:
  2. FlashDrive, IPOD, Computer, DVDPlayer, TV and RemoteControl. The unit prices are $19.99, $300.00, $1499.00,
  3. $150.00, 999.00, and $9.99 respectively. The program should first read the customer’s contact details
  4. (name, phone#, and email address) and then read from the keyboard the quantity of each of the product
  5. purchased by the customer and calculate the total cost of each item, the subtotal, and the total cost
  6. after a 7.5% sales tax. The customer bill should then display the total amount of payment.
  7. */
  8.  
  9. // PRE-PROCESSOR DIRECTIVES
  10. #include <iostream>
  11. #include <iomanip>
  12. #include <string>
  13. using namespace std;
  14.  
  15. // CONSTANTS
  16.  
  17.  
  18. // main FUNCTION
  19. int main()
  20. {
  21. // VARIABLE DEFINITIONS
  22. const double TAX = 0.075;
  23.  
  24. string name, phone, email;
  25.  
  26. double flashPrice = 19.99, ipodPrice = 300.00, computerPrice = 1499.00, dvdPrice = 150.00, tvPrice = 999.00;
  27. double remotePrice = 9.99;
  28. double flashTotal, ipodTotal, computerTotal, dvdTotal, tvTotal, remoteTotal;
  29. double subTotal, taxAmount, totalAmount;
  30.  
  31. int flashQuantity, ipodQuantity, computerQuantity, dvdQuantity, tvQuantity, remoteQuantity;
  32.  
  33. // STATEMENTS
  34.  
  35. cout << fixed << showpoint << setprecision(2);
  36. cout << "\t\t******* WELCOME TO OUR ELECTRONIC STORE *******\n\n";
  37. cout << "Please enter your name: ";
  38. getline(cin, name);
  39. cout << "\nPlease enter your phone# (format xxx-xxx-xxxx): ";
  40. getline(cin, phone);
  41. cout <<"\nPlease enter your email address: ";
  42. getline(cin, email);
  43.  
  44. cout << "\n\nNow enter the quantity of the products purchased in the order of\n";
  45. cout << "FlashDrive, IPOD, Computer, DVDPlayer, TV, and RemoteControl\n";
  46. cout << "(Input all quantities separated by a space): ";
  47. cin >> flashQuantity >> ipodQuantity >> computerQuantity >> dvdQuantity >> tvQuantity >> remoteQuantity;
  48.  
  49. cout << "\n\nCUSTOMER BILL\n";
  50. cout << "-------------\n";
  51. cout << "Name:\t" << name << endl;
  52. cout << "Phone:\t" << phone << endl;
  53. cout << "Email:\t" << email << endl << endl;
  54.  
  55. // compute totals for each item.
  56. flashTotal = flashPrice * flashQuantity;
  57. ipodTotal = ipodPrice * ipodQuantity;
  58. computerTotal = computerPrice * computerQuantity;
  59. dvdTotal = dvdPrice * dvdQuantity;
  60. tvTotal = tvPrice * tvQuantity;
  61. remoteTotal = remotePrice * remoteQuantity;
  62.  
  63. cout << "Description\tUnitPrice\tQuantity\tTotalPrice\n";
  64. cout << "----------------------------------------------------------\n";
  65. /*cout << left << setw(15) << "FlashDrive";
  66. cout << right << "\t" << setw(5) << "$" << flashPrice << setw(5) << "\t" << flashQuantity
  67. << "\t" << setw(5) << "$" << flashTotal << endl;*/
  68. cout << left << setw (15) << "FlashDrive" << right << setw(2) << "$" << right << setw(10) << flashPrice
  69. << setw (2) << " " << right << setw(4) << flashQuantity << right << setw(12) << "$" << right << setw(9) << flashTotal << endl;
  70. cout << left << setw (15) << "Ipod" << right << setw(2) << "$" << right << setw(10) << ipodPrice
  71. << setw (2) << " " << right << setw(4) << ipodQuantity << right << setw(12) << "$" << right << setw(9) << ipodTotal << endl;
  72. cout << left << setw (15) << "Computer" << right << setw(2) << "$" << right << setw(10) << computerPrice
  73. << setw (2) << " " << right << setw(4) << computerQuantity << right << setw(12) << "$" << right << setw(9) << computerTotal << endl;
  74. cout << left << setw (15) << "DvdPlayer" << right << setw(2) << "$" << right << setw(10) << dvdPrice
  75. << setw (2) << " " << right << setw(4) << dvdQuantity << right << setw(12) << "$" << right << setw(9) << dvdTotal << endl;
  76. cout << left << setw (15) << "TV" << right << setw(2) << "$" << right << setw(10) << tvPrice
  77. << setw (2) << " " << right << setw(4) << tvQuantity << right << setw(12) << "$" << right << setw(9) << tvTotal << endl;
  78. cout << left << setw (15) << "RemoteControl" << right << setw(2) << "$" << right << setw(10) << remotePrice
  79. << setw (2) << " " << right << setw(4) << remoteQuantity << right << setw(12) << "$" << right << setw(9) << remoteTotal << endl;
  80. cout << "----------------------------------------------------------\n";
  81.  
  82. // Compute subtotal, tax and total amounts.
  83. subTotal = (flashTotal + ipodTotal + computerTotal + dvdTotal + tvTotal + remoteTotal);
  84. cout << "Subtotal\t\t\t\t\t" << right << "$" << right << setw(9) << subTotal << endl;
  85.  
  86. taxAmount = subTotal * TAX;
  87. cout << "Tax [7.5%]\t\t\t\t\t" << right << "$" << right << setw(9) << taxAmount << endl;
  88. cout << "----------------------------------------------------------\n";
  89.  
  90. totalAmount = subTotal + taxAmount;
  91. cout << "Total\t\t\t\t\t\t" << right << "$" << right << setw(9) << totalAmount << endl;
  92.  
  93. cout << "----------------------------------------------------------\n";
  94. cout << "\t\t******* THANK YOU AND VISIT US AGAIN *******\n\n";
  95.  
  96. return 0;
  97. }
Similar Threads
Reputation Points: 14
Solved Threads: 3
Junior Poster in Training
chizy2 is offline Offline
61 posts
since Apr 2005
Oct 23rd, 2007
0

Re: Better way to make the columns align?

Personally (and I'll get slammed for this ) I'd use printf. It's designed for formatted output, rather than that convoluted cout mess.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Oct 23rd, 2007
0

Re: Better way to make the columns align?

the instructor has us using cout and cin only right now.
Reputation Points: 14
Solved Threads: 3
Junior Poster in Training
chizy2 is offline Offline
61 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: HELP! I really need some resources.
Next Thread in C++ Forum Timeline: expert! how to do this? PHONEBOOK!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC