c++ code using arrays and functions(Need URGENT help !)

I have to make two functions using array. I am stuck. I put the code that I tried.

getSales – prompts the user to enter sales for 5 stores and fills the array.
showBarGraph - displays a bar graph.

Here is an example of the programs output.
Enter today's sales for store 1: 1050  Show 10 *s
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: 1990  Show 19 *s
SALES BAR CHART
( Each * = $ 100)
Store 1: **********
Store 2: ************
Store 3: ******************
Store 4: ********
Store 5: *******************

I am bad at arrays. Can someone tell me whats wrong with the code.

#include <iostream>
#include <iomanip>

using namespace std;

const int storesales = 5;

void getSales(int i,int storesales[]);
void showBarGraph(int storesales[],double stars);

int main()

{
int i,number;
double stars;

getSales(i,storesales);
showBarGraph(storesales,stars);

return 0;
}

void getSales(int i,int storesales[])
{
for (int i = 0; i < 4; i++)
{
cout <<"\nEnter today's sales for store " << i + 1 << ": ";
cin >> storesales[i];
}
}

void showBarGraph(int storesales[],double stars)
{
stars = (storesales []/100);

for (int i = 0; i < stars; i++)
{
cout <<"\nStore " << i + 1 << ": " ;
cout << "*";
}


Last edited on
Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.

http://www.cplusplus.com/forum/beginner/1/#msg6680
I got you.
Please use code tags.
http://www.cplusplus.com/articles/jEywvCM9/

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
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <iomanip>

using namespace std;

const int storesales = 5;

// same names ^^^^^      vvvvvvvvvv  ???
void getSales(int i, int storesales[]);
void showBarGraph(int storesales[], double stars);

int main()
{
	int i, number;	// what is the point of i and number?
					// number is unused
	double stars;

	getSales(i, storesales);
	showBarGraph(storesales, stars);

	return 0;
}

void getSales(int i, int storesales[])
{
	// you need to loop through 5 stores
	// this will iterate through i = 0, 1, 2, 3 -> only 4
	for (int i = 0; i < 4; i++)
	{
		cout <<"\nEnter today's sales for store " << i + 1 << ": ";
		cin >> storesales[i];
	}
}

void showBarGraph(int storesales[], double stars)
{
	stars = (storesales []/100);	// you didn't specify which element to acess
					// within the [], so I don't know how this compiled

	for (int i = 0; i < stars; i++)
	{
		// access each store in the storesales array in here
		storesales[i];	// like so (this expression does nothing)
		cout <<"\nStore " << i + 1 << ": " ;
		cout << "*";
	}
}	// missing closing brace 
Last edited on
Topic archived. No new replies allowed.