1. ## for loop/counter error

Hey everyone.

Writing some code and i'm having a little problem with my for loop. the program is only supposed to output 3 lines of code (all different random prices). However, the first price is always \$0.00. I'm sure this is the result of a careless mistake, but i'm not seeing it.

Code:
```        for (counter=0; counter < num_games; counter++){
printf("The cost of game %d is \$%.2lf\n", counter +1, cost_new);
cost_new = 25 + rand()%26;}
if(cost_new != 50) {
cost_new += rand()%100 * 0.01;}
break;}```
is more code needed?

as always, any help is appreciated.

2. Does cost_new have a value before you enter the loop? Because in the code you posted you print cost_new before you assign any value to it which could explain why it's 0 in the first round.

3. no, that makes sense.. i'll just throw all the code down. The assignment is to have the program assign a random cost of \$5-\$20 for used games and \$25-50 for new games.

Code:
```int main() {
int new_games, used_games, num_games, selection, counter;
double cost_new, cost_used;
srand(time(0));

printf("Welcome to UCF Games!\n");
printf("What would you like to do?\n");
printf("\t 3. Quit.\n");

scanf("%d", &selection);

while (selection == 1){
switch (selection) {
case 1:
printf("How many New Games would you like to buy today?\n");
scanf("%d", &num_games);
}

for (counter=0; counter < num_games; counter++){
printf("The cost of game %d is \$%.2lf\n", counter +1, cost_new);
cost_new = 25 + rand()%26;}
if(cost_new != 50) {
cost_new += rand()%100 * 0.01;}
break;}```

4. Just move your printf() statement to a point in your loop body AFTER calculating cost_new.

The break statement in the for loop will cause it to stop after the first iteration.

The thread is mistitled too. The problem is not a for loop/counter error. It is a "broken programmer logic" error.

5. So just switch place of line #22 and #23.

6. fair enough. thanks for the help