PLEASE, PLEASE URGENT help with a CHART

TONIGHT, I need to finish tonight!! I need help!! I did 2 of 3 exercises.
This one makes me crazy. I don't find the error/s.


I have a project of 3 exercises and one of these is to do some inputs and display a chart. The chart needs to show the amount of stars for each $100 of sales shown in the store sales enter by the user.

Example:
Enter today's sales for store 1: 1000 [enter]
....................... store 2: 1200 [enter]
same for each store until 5th.

Then, follow this, needs to show a chart like this:
SALES BAR CHART
(Each * = $100)
Store 1: **********
Store 2: ************
and so on.

I have problem with taking each sale of each store dividing by 100 to show the correct amount of stars.

I need an array, but I can't do it successfully. It taking sales of the last store.

Here is my code c++:

#include <iostream>
using namespace std;

int main()
{
int sales, stars, store, x, counter;

for (x = 1; x <= 5; x++)
{
cout << "Enter today's sale for store " << x << ": ";
cin >> sales;
}
cout << "\nSales Bar Chart\n";
cout << "(Each * = $100)\n";

for (store = 1; store <=5; store++)
{
stars = sales/100;
cout << "Store " << store << ": ";

for (int counter = 1; counter <= stars; counter++)
{
cout << "*";
}//end for counter of stars

cout << endl;

}//end for stores on chart

cin.get();
}//end main

THANKS A LOT FOR HELPING ME,... IT IS FOR SUNDAY!!!! 8-(
Last edited on
Here, I've written the first half for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main() {

	const unsigned short num_stores = 5;
	const unsigned short dollars_per_star = 100;

	int store[num_stores] = {0};
	//This array will hold the sales for each store

	for (unsigned short i = 0; i < num_stores; ++i) {

		std::cout << "Enter today's sales for store #" << i + 1 << ":\t";
		std::cin >> store[i];

	}

	/*Rest of code goes here*/

	std::cin.get();
	return 0; 
}


That's how one might handle the input for an array. Now it's easy for you to figure out how one might print the stars based on the values from the array.
Last edited on
I have a problem... I can't use an array, because we don't cover it yet.
I tried to do with that because of some info I saw at internet, but I didn't understand at all. Then, I am traveling all internet, book, references, since two days and I am very nervous because I need to finish this today. And have 2 more, but easier,... hope so.
I need to use something like one of these examples:

1- The output is as the professor wants, except that only took the store 5 to divided by 100 to print the *'s.

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

int main()
{
    int sales, stars, store, x, counter;
    
    for (x = 1; x <= 5; x++)
    { 
        cout << "Enter today's sale for store " << x << ": ";
        cin >> sales;
    }    
        cout << "\nSales Bar Chart\n";
        cout << "(Each * = $100)\n";
        
        for (store = 1; store <=5; store++)
        {
            stars = sales/100;
            cout << "Store " << store << ": ";
            
            for (int counter = 1; counter <= stars; counter++)
            {
                cout << "*";
            }//end for counter of stars
            
                cout << endl;      
        
        }//end for stores on chart
        
cin.get();         
}//end main 



2- it is not completed yet, but I am trying doing separately.
But it is not stopping. Have something wrong, because is infinite loop, don't stop, and I can't open my terminals now, because is running and don't stop, and I need to do 2 more exercises.

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

int main()
{
    int stars, store, counter, sales;
    int sales1, sales2, sales3, sales4, sales5;
    
    {
        cout << "Enter today's sale for store 1: ";
        cin >> sales1;
    }
    {
        cout << "Enter today's sale for store 2: ";
        cin >> sales2;
    }   
        cout << endl;
        cout << "\nSales Bar Chart\n";
        cout << "(Each * = $100)\n";
        
        for (store = 1; store <= 5; store++)
        { 
            cout << "Store" << store << ": ";
        
            while (counter <= (sales1/100))
            {
            cout << "*";
            }//end for sales1
            cout << endl;
            
            while (counter <= (sales2/100))
            {
            cout << "*";
            }//end for sales2
            cout << endl;
            
        }//end for chart
        
        cout << endl;      
            
cin.get();         
}//end main 


HERE OTHER I DID AND IT OUTPUT:

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

int main()
{
    int store, sales, counter;
    
    
    for (store = 1; store <= 5; store++)
    {   
        cout << "Enter today's sale for store " << store << ": ";
        cin >> sales;
       
    }    
        cout << "\nSales Bar Chart\n";
        cout << "(Each * = $100)\n";
        
        for (store = 1; store <=5; store++)
        {   
            cout << "Store " << store << ": ";
           
            while (counter <= (sales/100))
            {
                cout << "*";
                counter++;
            }//end for counter of stars
            
                cout << endl;      
        
        }//end for stores on chart
        
cin.get();         
}//end main 


OUTPUT:

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

Sales Bar Chart
(Each * = $100)
Store 1: ********************
Store 2:
Store 3:
Store 4:
Store 5:

EVERY HELP WILL BE GREAT, I AM EXHAUSTED WITH THIS!! I HAVE ONLY 2 MONTHS IN PROGRAMMING. JUST A BEGINNER!!
Last edited on
PLEASE SOME URGENT HELP TO MY CODE!!
MY Output is not what I need, and I don't know what else to do for solve it.

THANKS 8-/
Please use code tags when you're posting code on these forums.

How to use code tags:
http://www.cplusplus.com/articles/jEywvCM9/

Using code tags makes it more likely that someone will help you, because nobody wants to look at un-formatted code.

I'm confused. In your initial post you said you need to make use of an array, but now you're saying that's wrong?

Also, I realize that English may not be your first language, but there's no way I can understand what you're trying to say with this:

This running all as the professor wants, except only took the store 5 to divided by 100 to print the *.
I don't know about the tags, but I will check. Thanks.
No, it is not my first language. Sorry about my English errors. I want to learn more to be fully bilingual. I am not mad about corrections.
Thanks for try to help.

Yes,... first of all, I read somethings at the internet about the array as a solution to my problem, but then, I knew I can't use it.
I tried to do the code but I couldn't. Specially the output, it is not exactly what the professor wants.

I mean that the output is not what the professor wants.
The code program takes only the last sales input of the store 5 and does what I want of divided by 100 to print the *.

Last edited on
well, I did it... I am using the code tags.
Thanks
I see. Thank you for editing your previous posts with the code tags.
Now I understand what you're saying.

The problem you're experiencing has to do with the fact that you do not reset the counter variable. I would suggest you add:

counter = 0;

On line 21 of your second snippet.
Did that solve it?

*edit* now that I look at it, you never initialized counter to begin with. That's dangerous.
Last edited on
WHICH ONE?

I put counter = 0; in the last one, but not function, neither in the second one, that aren't stopping with ***. How can I stop it?
Last edited on
O.K. I missed another important, glaring thing somehow. I blame the summer heat.

By the time you get to printing the stars, the sales variable will only contain the value of the last store. This is so because you're giving it a new value every time you ask the user for input, effectively discarding all the sales data for all previous stores.

This means, you need to use an array to store the sales data. It seems you're at an impasse.

I also noticed that you're star-printing while loop prints one too many stars.
You can fix that by changing:

while (counter <= (sales / 100)) {

into:

while (counter < (sales / 100)) {


I've cleaned up the code you provided a little bit. It's still broken, in the way that there's no array to store the sales data, which results in all stores getting the same number of stars.

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
#include <iostream>

int main() {
	int store = 0;
	int sales = 0;
	int counter = 0;

	for (store = 1; store <= 5; ++store) {
		std::cout << "Store #" << store << " sales:\t";
		std::cin >> sales;
	}

	std::cout << std::endl;

	for (store = 1; store <= 5; ++store) {
		std::cout << "Store #" << store << ":";

		counter = 0;//reset the counter

		while (counter < (sales / 100)) {
			std::cout << "*";
			counter++;
		}
		std::cout << std::endl;
	}

	std::cin.get();
	std::cin.ignore();
	return 0;
}
Last edited on
well, I did one that make this output, the second one.
but...
do you think is nothing to do for solve this output than only an array?

if so, how can I add it? I read a lot, but I didn't get it.

why I need to add: int store = 0; int sales = 0; int counter = 0;
and why < instead of <= in while (counter < (sales/100)) if I need to reach all the stars including the results of the same sales/100?
and, counter = 0 if I declared above?


Last edited on
why I need to add: int store = 0; int sales = 0; int counter = 0;


You don't need to initialize the variables. However, it's good practice to do so, and if you don't do it, people will call you an amateur.
Initializing variables on the same line on which they're declared is a good thing to do, because it will help avoid undefined behavior headaches in the future.

why < instead of <= in while (counter < (sales/100))


Because that way you print the correct number of stars? I did a test with this earlier, and <= gave me one too many stars. < seemed to solve it.

and, counter = 0 if I declared above?


Well, of course the while loop is flawed to begin with, because the program is flawed to begin with, because you're not using an array to store the sales data. Let's ignore that technicality for now.
If you don't reset the counter, then the value of the counter will only increase, which could potentially mean if the sales for a particular store weren't high enough, the stars for that store wouldn't get printed.

But, more importantly, it's bad because if the previous counter value isn't discarded, you'll get an incorrect representation.

For example, let's say there's only three stores instead of five - for simplicity - Let's say the following is the sales data for each of the three stores:

Sales:

Store#1: 1000
Store#2: 500
Store#3: 2000

For store#1, the while loop would evaluate to be:

while(0 < 10) {

Then the counter would increment each iteration, and the loop would terminate once counter reaches ten.

Then, the next store wants to get it's stars printed. The counter hasn't been reset, and therefore, counter is still ten. The while loop would evaluate to:

while(10 < 5) {

Therefore, store#2's stars will never print.

However, the stars for store#3 will print. The counter is still ten, and for store#3, the while loop would evaluate to:

while(10 < 20) {

So, the result is that:

1.) Store#1's stars printed correctly.
2.) Store#2's stars didn't get printed at all
3.) Store#3 only printed 10 instead of 20 stars.

do you think is nothing to do for solve this output than only an array?


Using a for loop to get user input implies that you'll be using an array. If your teacher is asking you to use a for loop for user input, but not to use an array, she/he is stupid.

You could, technically, use five different variables to store the sales data, but that's most certainly not this assignment is supposed to teach. Are you sure your teacher is against arrays? What are the exact requirements for this assignment?
Last edited on
Yeah, I know!!!
BUT, LOOK BELOW... I DID IT NOW... without an array. So long, but,... as you said. I had been trying to do separately. Well, I did it... I need to fix some esthetic details and comments, and that's all.

Thanks for your collaboration. Blessings guy!!

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
69
70
71
72
73
#include <iostream>
using namespace std;

int main()

{
	int sales1=0, sales2=0, sales3=0, sales4=0, sales5=0;  //today's sales for five stores 
	int	star1=0, star2=0, star3=0, star4=0, star5=0;       //variable to stars
	int	counter;

	cout << "Enter the sales of five stores." << endl;

	cout << "\n Enter sales for store 1 : ";               //Input sales
	cin >> sales1;

	cout << "\n Enter sales for store 2 : ";
	cin >> sales2;

	cout << "\n Enter sales for store 3 : ";
	cin >> sales3;

	cout << "\n Enter sales for store 4 : ";
	cin >> sales4;

	cout << "\n Enter sales for store 5 : ";
	cin >> sales5;


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


	star1 = sales1/100;                      //Dividing sales by $100
	star2 = sales2/100; 
	star3 = sales3/100; 
	star4 = sales4/100; 
	star5 = sales5/100; 

	    {
	    cout << "Store 1: ";
            
        for (int counter = 1; counter <= star1; counter++)
            cout << "*";
	    }cout<< endl;         //end stars store1
        {
	    cout << "Store 2: ";
            
        for (int counter = 1; counter <= star2; counter++)
            cout << "*";
	    }cout<< endl;         //end stars store2
        {
	    cout << "Store 3: ";
            
        for (int counter = 1; counter <= star3; counter++)
            cout << "*";
	    }cout<< endl;         //end stars store3
	    {
	    cout << "Store 4: ";
            
        for (int counter = 1; counter <= star4; counter++)
            cout << "*";
	    }cout<< endl;         //end stars store4
	    {
	    cout << "Store 5: ";
            
        for (int counter = 1; counter <= star5; counter++)
            cout << "*";
	    }cout<< endl;         //end stars store5

	cout<<endl;

return 0;
}


Enter the sales of five stores.

Enter sales for store 1 : 1000

Enter sales for store 2 : 1200

Enter sales for store 3 : 1800

Enter sales for store 4 : 800

Enter sales for store 5 : 1900

SALES BAR GRAPH
(Each * = $100)
Store 1: **********
Store 2: ************
Store 3: ******************
Store 4: ********
Store 5: *******************
Well, I'm glad it works.
Topic archived. No new replies allowed.