I'm working on a very basic C homework. I need to make a game where computer generates a random number and user tries to guess it. User has 5 attempts and I tried to give that by a for loop. But on every loop the attemp decrement by two instead of one. I can't find out what I'm missing here. My code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
int counter;
int prediction;
int number;
srand(time(NULL));
number = 1 + (rand() % 100);
for ( counter=1; counter<=5; counter++ ) {
printf("\nGuess my number from 1 to 100: \n");
scanf("%d", &prediction);
if (prediction < number) {
printf("My number is greater than your guess. \n");
printf("You have %d attempts left \n", (5-counter) );
counter++;
}
if (prediction > number) {
printf("My number is smaller than your prediction. \n");
printf("You have %d attempts left \n", (5-counter) );
counter++;
}
if (prediction == number) {
printf("Congratulations! You guessed my number correctly! \n");
break;
}
}
return 0;
}