Required to create a C++ program for homework but stuck on the second half of the program

Viewed 49

Write a program that asks the user to enter today's sales for five stores. The program should then display a bar graph comparing each store's sales. Create each bar in the graph by displaying a row of asterisks. Each asterisk should represent $100 of sales.

Here is an example of the program's output:

Enter today's sale for store 1:  1000 (enter)

Enter today's sale for store 2:  1200 (enter)

Enter today's sale for store 3:  1800 (enter)

Enter today's sale for store 4:  800 (enter)

Enter today's sale for store 5:  1900 (enter)

SALES BAR CHART

(Each * = $100)

Store 1:  **********

Store 2:  ************

Store 3:  ******************

Store 4:  ********

Store 5:  *******************

My Current Code:

#include <iostream> 
using namespace std;

int main()
{
    int stores;
    int i = 1;//counter variables
    int sale;
    int j = 1;//counter variables

    // Get input from the user to see how many stores you are listing
    cout << "Please enter how many stores you are inputting data for.\n";
    cin >> stores;
    
    
    while(j<= stores)
    {
        cout << "Enter today's sale for store "<< i << ": "; 
        cin >> sale;
        j++;
        i++;
    }
    
    cout << "SALES BAR CHART:" << endl;
    cout << "Each * = $100" << endl;
}

Note: Only allowed to use if/else statements as well as loops.

1 Answers

Here is the solution:

#include <iostream>
using namespace std;

int main()
{
    int stores;

    // Get input from the user to see how many stores you are listing
    cout << "Please enter how many stores you are inputting data for.\n";
    cin >> stores;

    int* sales = new int[stores];
    for (int i = 0; i < stores; ++i) {
        cout << "Enter today's sale for store "<< i + 1 << ": ";
        cin >> sales[i];
    }

    cout << "SALES BAR CHART:" << endl;
    cout << "Each * = $100" << endl;

    for (int i = 0; i < stores; ++i) {
        cout << "Store " << i + 1 << ": ";
        for (int j = 0; j < sales[i]; j += 100) {
            cout << '*';
        }
        cout << "\n";
    }

    delete[] sales;
}
Related