Sales Bar Chart

I'm doing my homework assignment for beginning C++ Class. I need to 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 bar graph by displaying a row of asterisks. Each asterisk should represent $100 of sales.

Example:

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

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

So I'm getting it a little. We have to use loops, no arrays. I have the following as my code. I'm able to input store data and it does display a sales bar chart but like this :
SALES BAR CHART
(Each * = $100)
Store 1:
Store 2:
Store 3:
Store 4:
Store 5:
******

Any help/advice would be greatly appreciated! I'm new the forums as well. I'm so thankful to have found this. I'm a nurse with NO programming experience, this is a whole new world to me!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <iostream>
using namespace std;

int main()
{
int store;
int profit;
char stars ='*';


cout <<"Enter today's sales for five different stores." <<endl;

for (int  count=1; count<=5; count++)
{
cout<<"\n Enter today's sales for store "<<count<<":";
cin>> profit;

}

{
	cout<<"SALES BAR CHART"<<endl;

	cout<<"Each * = $100"<<endl;

	for(int count = 1; count<= 5; count++)

	cout << "Store " << count << ": "<<endl;
	for (int stars = 0; stars < profit/ 100; stars++)
		cout << "*";
	cout << endl;

system ("pause");
}
return 0;
}
Did your instructor explicitly say not to use arrays? The thing is, in your first loop, where you ask the user to input the sale data, the profit variable will always be overwritten once the loop restarts, because the user enters new data - effectively making the sales of all stores equal to the final profit entered.
The directions for the assignment say "The program MUST use for loops to display the bar charts."

Hmm, so that's why I'm only getting a the bar chart to display store 5 data in the chart. Can you point me in the direction of writing this loop correctly?
Sure. I would use for loops as well as arrays.

You'll use one integer array with five elements to represent the sales - one for each store.

I've written the first part for you, let's see if you can figure out the rest.
I've tried to keep it simple:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main() {
	const int num_stores = 5;

	int sales[num_stores];

	for(int i=0; i<num_stores; ++i) {
		std::cout << "Enter the number of sales for store #" << i << ":\t";
		std::cin >> sales[i];
	}
Last edited on
Topic archived. No new replies allowed.