fork download
  1. //Devin Scheu CS1A Chapter 5, P. 297, #17
  2. //
  3. /**************************************************************
  4. *
  5. * DISPLAY SALES BAR CHART
  6. * ____________________________________________________________
  7. * This program determines a bar chart comparing sales for five
  8. * stores based on user input.
  9. *
  10. * Computation is based on a loop converting sales amounts to
  11. * asterisks, with each asterisk representing $100 of sales
  12. * ____________________________________________________________
  13. * INPUT
  14. * storeSales : The sales amount for each of the five stores
  15. *
  16. * OUTPUT
  17. * barChart : The bar chart representation of sales
  18. *
  19. **************************************************************/
  20.  
  21. #include <iostream>
  22. #include <iomanip>
  23.  
  24. using namespace std;
  25.  
  26. int main () {
  27.  
  28. //Variable Declarations
  29. const int NUM_STORES = 5; //OUTPUT - Number of stores
  30. double storeSales[NUM_STORES]; //INPUT - The sales amount for each of the five stores
  31. string barChart; //OUTPUT - The bar chart representation of sales
  32.  
  33. //Prompt for Input
  34. for (int store = 0; store < NUM_STORES; store++) {
  35. cout << "Enter today's sales for store " << (store + 1) << ": ";
  36. cin >> storeSales[store];
  37. while (storeSales[store] < 0) {
  38. cout << "\nError: Please enter a non-negative number: ";
  39. cin >> storeSales[store];
  40. }
  41. }
  42.  
  43. //Display Header
  44. cout << "\nSALES BAR CHART" << endl;
  45. cout << "(Each * = $100)" << endl;
  46.  
  47. //Display Bar Chart
  48. for (int store = 0; store < NUM_STORES; store++) {
  49. int asterisks = static_cast<int>(storeSales[store] / 100);
  50. cout << "Store " << (store + 1) << ": ";
  51. for (int i = 0; i < asterisks; i++) {
  52. cout << "*";
  53. }
  54. cout << endl;
  55. }
  56.  
  57. return 0;
  58. } //end of main()
Success #stdin #stdout 0.01s 5272KB
stdin
1000
1200
1800
800
1900
stdout
Enter today's sales for store 1: Enter today's sales for store 2: Enter today's sales for store 3: Enter today's sales for store 4: Enter today's sales for store 5: 
SALES BAR CHART
(Each * = $100)
Store 1: **********
Store 2: ************
Store 3: ******************
Store 4: ********
Store 5: *******************