last thing for this program.

Here is the problem I'm suppose to figure out:
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 displaying a row of asterisks. Each asterisks should represent $100 of sales.

Her is an example of the program's 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: *******************

Here is the code I got for this program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
	int strs, y, x, sales;
	strs = 100;
	for (x = 1; x <= 5; x++)
		cout << "Enter the sales for store\t\n" << x;
		cin >> sales;
	cout << "Sales Bar Chart\n";
	strs = sales/100;
	for (y = 1; y <= strs; y++)
		strs = y/100;
		cout << "*";
	cout << endl;
return 0;
}


Here is the outcome I Get (We have to us nested For Loops):

Enter the sales for store
1Enter the sales for store
2Enter the sales for store
3Enter the sales for store
4Enter the sales for store
5
Sales Bar Chart
*
Press any key to continue ...

Basically I want to know, how do i make the "Enter the sales for store" thing look better and how do i get it where i can enter 5 numbers instead of just 1.
don't forget i have to use nested For Loops.
also how do i put the word store and its number before the asterisks.
cout << "Stores" << x << ":"; i got this but it says store 6 instead of store 1, i put it after cout << "Sales Bar Chart\n";
Last edited on
Hi micah1983
ah the old histogram problem...
there are many ways to do this i will try to give ya some hints:
Initialize your variables;
You can use an array to store the user input;
If your for loop has more than one command then you need to use braces;
1
2
3
4
for(int i = 0; i < arraySize; i++)
{
//do stuff
}


heres a lil histogram to give you an idea your asterisk question;
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
// The lil histogram thingie!!
// displays a histogram of numbers between 0-9
#include <iostream>
using std::cout;
//using std::cin;


int main()
{
	const int arraySize = 10;
	int myArray[arraySize] = {0};
	for(int i = 0; i < arraySize; i++)//initialize myArray number 0-9
		myArray[i] += i;

	cout <<"#'s 0-9     Histogram\n";
	for(int j = 0; j < arraySize; j++)//Number j = 
	{
		cout << "Number " << j << " = ";
		for(int k = 0; k < j; k++)// *'s
		{
			cout << "*";
		}
		cout << '\n';
	}
	return 0;
}


hope this helps!

Jeff
Last edited on
can't use array's for the asterisks, we haven't got that far yet.
i got it good now. all i need to do now is get all five stores instead of one and also get it to say Store 1:, Store 2:, Store 3:, Store 4:, Sotre 5:, instead of store 6. can anybody help me with that. here is what i got so far:
1
2
3
4
5
6
cout << "Sales Bar Chart\n";
	cout << "Stores" << x << ":";
	strs = sales/100;
	for ( y = 1; y <= strs; y++)
		cout << "*";
	cout << endl;

he used y to represent the asterisks. DON"T FORGET CAN'T USE ARRAYS HAVEN'T GOT THERE YET.
hmm hey i told to you the array for the user input not for the asterisks problem...
anyway...
I dont see how this:
1
2
3
4
5
6
7
8
9
10
11
12
my code Output:
#'s 0-9     Histogram
Number 0 =
Number 1 = *
Number 2 = **
Number 3 = ***
Number 4 = ****
Number 5 = *****
Number 6 = ******
Number 7 = *******
Number 8 = ********
Number 9 = *********

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

is so different.
you don't really need a array for that. The answer for your "5 stores" question is right there.
Ok you cant use arrays got it.
since you like caps:
THE ONLY REASON IT IS SAYING 6 INSTEAD OF 1 IS BECAUSE YOU ARE NOT USING BRACES NOTHING IS WRONG WITH THE LOOP.

notice how in my code i have both loops with braces and one of em is inside the other? THATS NESTED FOR LOOPS.

Sorry if i wasn't clear enough in my first post good luck.

Jeff
you want me to use the histogram instead of my y code or after it
i got the thing in there, but instead of 100 for each asterisks. i get something like store 1, get one asterisks, store 2, i got two asterisks, for store 3 i got 3 asterisks, etc. how do i change that. i need it to say like for 1200 i need 12, for 1800 i need 18 asterisks, etc, something like that how do i do that
closed account (z05DSL3A)
micah1983,

Are you saw you have to use nested 'for' loops(one or more 'for' loops inside another), or multiple 'for' loops?

Basicaly you are going to need five variables to hold the five 'sales' figures for the five stores.

When it comes to drawing the graph, the aproriate variable should be used as the second control expressions eg:
1
2
3
4
5
for (int counter = 0; counter < (storeVariable1 /100); counter++)
{
    cout << "*";
}
cout << endl;



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        const int arraySize = 6;
	int myArray[arraySize] = {0};
	cout << "Hello give me 5 numbers and i will give you a histogram.\n";
	for(int i = 1; i < arraySize; i++){
		cout << "Number " << i << " : ";
		cin >> myArray[i];
	}
		
	cout <<"#'s          Histogram\n";
	for(int j = 1; j < arraySize; j++)//Number x = 
	{
		cout << "Number " << myArray[j] << " = ";
		for(int k = 0; k < myArray[j]; k++)// *'s
		{
			cout << "*";
		}
		cout << '\n';
	}


YEAH I KNOW YOU CANT USE ARRAYS.
It's the less complex approach or use 5 variables which is kinda ugly(e.g userInput1, userInput2, ...).
or you could without arrays do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        int userInput = 0;
	cout << "Welcome to Histogram thingie program\n"
		 << "Give me some number and i will give you a histogram. or 0(zero) to exit\n";	
	for(; ;) //for ever
	{
	cout << "Enter a number or 0 (zero) to exit.\n";
	cin >> userInput;
		if(userInput == 0)
			break; // break only exits the loop. return 0; will exit the program
		cout <<"#           Histogram\n";
		cout << "Number " << userInput << " = ";
		for(int k = 0; k < userInput; k++)// *'s
		{
			cout << "*";
		}
		cout << '\n';
	}


just divide the userInput for 100. since it cant be zero its ok to divide right way without checking for it. I used 0(zero) to exit the for ever. I'd check if it was higher than 100 though.
change the for ever to only 5 loops.
Last edited on
Topic archived. No new replies allowed.