Saturday, December 24, 2016

Programming Challenge 5.17 - Sales Bar Chart

/* Sales Bar Chart - This program asks the user to enter today's sales for
   five stores. The program displays a bar graph comparing each store's
   sales. A bar chart in the bar graph is displaying a row of asterisks.
   Each asterisk represents $100 of sales.

   Example output:

   * Enter today's sales for store 1: 1000 [Enter]
   * Enter today's sales for store 2: 1200 [Enter]
   * Enter today's sales for store 3: 1800 [Enter]
   * Enter today's sales for store 4:  800 [Enter]
   * Enter today's sales for store 5: 1900 [Enter]

   SALES BAR CHART
   (Each *= $100)
   Store 1: **********
   Store 2: ************
   Store 3: ******************
   Store 4: ********
   Store 5: ******************* */

#include "Utility.h"

int main()
{
    /* Constant: Maximum number of stores */
    const int STORES_MAX = 5;

    /* Variables: Number of stores (counter variable), Bar chart
       (counter variable) */
    int barChart = 1,
        numStores = 1;

    /* Variables: Stores, Sales figure (1 ... 5) */
    double salesFigure = 0,
           salesFigure2 = 0,
           salesFigure3 = 0,
           salesFigure4 = 0,
           salesFigure5 = 0;

    /* For loop: As long as the number of stores is lower than or equal
       to STORES_MAX, this loop will iterate */
    for (numStores = 1; numStores <= STORES_MAX; numStores++)
    {
        /* Display: Ask the user to enter data for each store */
        cout << "Enter today's sales figure for store "
            << numStores << ": ";

        /* Switch Statement::Input: Until the sales figures for stores 1
           to 5 are entered, this statement will be executed */
        switch (numStores)
        {
        case 1:
            cin >> salesFigure;
            salesFigure /= 100;
            break;

        case 2:
            cin >> salesFigure2;
            salesFigure2 /= 100;
            break;

        case 3:
            cin >> salesFigure3;
            salesFigure3 /= 100;
            break;

        case 4:
            cin >> salesFigure4;
            salesFigure4 /= 100;
            break;

        case 5:
            cin >> salesFigure5;
            salesFigure5 /= 100;
            break;
        }
    }

    /* Display: Table header */
    cout << "\nSales Bar Chart\n";
    cout << "(Each * = $100)";

    /* For loop: As long as the store number is lower than or equal to
       STORES_MAX (5), this loop will iterate */
    for (numStores = 1; numStores <= STORES_MAX; numStores++)
    {
        /* Display: Store number */
        cout << "\nStore " << numStores << ":";
      
        /* Nested For loop: As long as bar chart is smaller than
           or equal to sales figure 1 through 5, and the store
           number equals store 1 through 5, this loop will iterate */
        for (barChart = 1;
             barChart <= salesFigure && numStores == 1 ||
             barChart <= salesFigure2 && numStores == 2 ||
             barChart <= salesFigure3 && numStores == 3 ||
             barChart <= salesFigure4 && numStores == 4 ||
             barChart <= salesFigure5 && numStores == 5;
             barChart++)
        {
            cout << '*';
        }  
    }
    cout << endl;

    pauseSystem();
    return 0;
}

No comments:

Post a Comment