Better way to make the columns align?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 61
Reputation: chizy2 is an unknown quantity at this point 
Solved Threads: 3
chizy2's Avatar
chizy2 chizy2 is offline Offline
Junior Poster in Training

Better way to make the columns align?

 
0
  #1
Oct 23rd, 2007
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
----------------------------------------------------------

  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Better way to make the columns align?

 
0
  #2
Oct 23rd, 2007
Personally (and I'll get slammed for this ) I'd use printf. It's designed for formatted output, rather than that convoluted cout mess.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 61
Reputation: chizy2 is an unknown quantity at this point 
Solved Threads: 3
chizy2's Avatar
chizy2 chizy2 is offline Offline
Junior Poster in Training

Re: Better way to make the columns align?

 
0
  #3
Oct 23rd, 2007
the instructor has us using cout and cin only right now.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC