Trouble with program

I have some trouble here with the program, my specific problem is that I don't know how to display the asterisks at all. I don't know how to finish it.

Question: 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.
Here 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: *******************

My plan was:

- Make for loop asking today's sales for 5 stores.
- Get answer for variable "Store 1" and divide by 100 to get asterisks.
- For every 100, display asterisk.

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
  #include <iostream>
using namespace std;

int main()
{
	int store1;
	int stars;

	for (int i = 1; i <= 5; i++)
	{
		cout << "Enter today's sales for store: " << i << endl;
		cin >> store1;

		stars = (store1 / 100);
	}
	
	for (int b = 1; b <= 5; b++)
	{
		cout << "For Store: " << b << endl;
		{

		}
	}
	

	return 0;
}
Last edited on
It looks as though you will need an array to store the values entered by the user.

Displaying the asterisks, you need to calculate how many are needed. Then you could do it the long way, by using a loop to display one asterisk at a time. Or more simply, create a string of the required length, filled with the asterisk character.

std::string(7, '*') will create a string like this: "*******".
See string constructor 6, fill.
http://www.cplusplus.com/reference/string/string/string/
Last edited on
We have not stumbled upon arrays yet, but is there any way to do this without arrays?
We have not stumbled upon arrays yet, but is there any way to do this without arrays?
Well, you could do it using five separate variables. But then it becomes extremely difficult to use a loop, since each iteration will need to access a different variable. It makes the code harder.

There are all sorts of other possible approaches, such as the use of a std::stringstream or the use of a switch case construct. You might even create functions for the input and output, then call the first function five times followed by calling the second function five times. It's matter of which of these complex approaches you prefer.

But I wouldn't recommend any of those.

If you haven't yet used arrays, this does seem like an ideal opportunity to introduce them. Note that array subscripts start from 0, not 1, hence the loop variable i starts from 0.
1
2
3
4
5
6
7
8
9
10
    
    const int size = 5;
    
    int store[size];
    
    for (int i = 0; i < size; i++)
    {
        cout << "Enter today's sales for store: " << i+1 << endl;
        cin >> store[i];
    }


Then you'd output the headings, followed by the second loop
1
2
3
4
5
    for (int i = 0; i < size; i++)
    {
        cout << "Store " << i+1 << ": ";
        // do something with store[i] and display asterisks
    }

See tutorial
http://www.cplusplus.com/doc/tutorial/arrays/

Hello Bayan Khorshidi,

Based on what you started with consider this:

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>

int main()
{
	int store1{ 0 };
	int stars1{ 10 }, stars2{ 12 }, stars3{ 18 }, stars4{ 8 }, stars5{ 19 };

	for (int i = 1; i < 6; i++)
	{
		std::cout << "Enter today's sales for store: " << i << " ";
		std::cin >> store1;

		switch (i)
		{
		case 1:
			stars1 = (store1 / 100);
			break;
		case 2:
			stars2 = (store1 / 100);
			break;
		case 3:
			stars3 = (store1 / 100);
			break;
		case 4:
			stars4 = (store1 / 100);
			break;
		case 5:
			stars5 = (store1 / 100);
			break;
		default:
			break;
		}

	}  //  End for

	std::cout << "\n SALES BAR CHART\n (Each * = $100)" << std::endl;

	for (int b = 1; b < 6; b++)
	{
		switch (b)
		{
		case 1:
			std::cout << " For Store: " << b << " " << std::string(stars1, '*') << std::endl;
			break;
		case 2:
			std::cout << " For Store: " << b << " " << std::string(stars2, '*') << std::endl;
			break;
		case 3:
			std::cout << " For Store: " << b << " " << std::string(stars3, '*') << std::endl;
			break;
		case 4:
			std::cout << " For Store: " << b << " " << std::string(stars4, '*') << std::endl;
			break;
		case 5:
			std::cout << " For Store: " << b << " " << std::string(stars5, '*') << std::endl;
			break;
		default:
			break;
		}
	}  //  End for

	std::cout << "\n\n\n\n Fin Press any key to continue -- > ";
	cin >> store1;
}  //  End main 


If you are not up to using "switch" these can be replaced with if statements. Also if you are not up to using "strings" as Chervil suggested this could be replaced with a for loop.

Hope that helps,

Andy
Hey guys thanks so much for the help, but believe it or not, my teacher right now will take off points if I go ahead. He really wants us to only use what we have learned so far, so I can't use arrays. I will try to do what @Chervil suggested and do this in separate variables, and post here soon.

Thanks so much for the helps/tip btw. :)
No strings, no arrays.
Building from Andy's solution
Also if you are not up to using "strings" as Chervil suggested this could be replaced with a for loop.


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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>

// to display the stars
// stars = number of stars
// storeNum = store number
void displayStars(int stars, int storeNum); 

int main()
{
        const int stores{5};
	int store1{ 0 };
	int stars1{ 0 }, stars2{ 0}, stars3{ 0 }, stars4{ 0 }, stars5{ 0};

	for (int i = 0; i < stores;)
	{
		std::cout << "Enter today's sales for store: " << ++i << " ";
		std::cin >> store1;

		switch (i)
		{
		case 1:
			stars1 = (store1 / 100);
			break;
		case 2:
			stars2 = (store1 / 100);
			break;
		case 3:
			stars3 = (store1 / 100);
			break;
		case 4:
			stars4 = (store1 / 100);
			break;
		case 5:
			stars5 = (store1 / 100);
			break;
		default:
			break;
		}

	}  //  End for

	std::cout << "\n SALES BAR CHART\n (Each * = $100)" << std::endl;
	
	// display stars
	// if you have not covered functions then just copy displayStars here five times. 
	displayStars(stars1, 1);  // display stars for store 1
	displayStars(stars2, 2);
	displayStars(stars3, 3);
	displayStars(stars4, 4);
	displayStars(stars5, 5);
 
 return 0;
}  //  End main 

void displayStars(int stars, int storeNum)
{
    if(stars > 0){ // if there are some values for the stars
        std::cout << stars << " stars for Store " << storeNum << ":  ";
        for(int i = 0; i < stars; i++){
            std::cout << "*" ;
       }
        std::cout << std::endl;
    }
    else{
        std::cout << "Nothing to display for store " << storeNum << std::endl;
    }
    
}


Last edited on
A couple of the other approaches I suggested - though it's unlikely either of these would be acceptable.
1. Using functions.
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
#include <iostream>

using namespace std;

int store = 1;

int input()
{
    cout << "Enter today's sales for store " << store++ << ": ";
    int sales;
    cin >> sales;    
    return sales;
}

void output(int sales)
{
    cout << "Store " << store++ << ": ";
    int stars = sales/100;
    for (int i=0; i<stars; ++i)
        cout << '*';
    cout << '\n';   
}

int main()
{
    int a = input();    
    int b = input();    
    int c = input();    
    int d = input();    
    int e = input();    
    
    cout << "SALES BAR CHART\n(Each * = $100)\n";
    store = 1;
    output(a);    
    output(b);    
    output(c);    
    output(d);    
    output(e);        	
}


2. Using a stringstream to defer the output until needed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
#include <sstream>

using namespace std;

int main()
{
    ostringstream os;
    os << setfill('*');
    
    for (int i=1; i<=5; ++i)
    {
        cout << "Enter today's sales for store " << i << ": ";
        int sales;
        cin >> sales;
        os << "Store " << i << ": " << setw(sales/100+1) << '\n'; 
    }
    
    cout << "SALES BAR CHART\n(Each * = $100)\n" << os.str();
}
Topic archived. No new replies allowed.