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?