-2

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;
}
  • 3
    You are incrementing `counter` once again in the `if`s. – JASLP doesn't support the IES Aug 02 '21 at 12:39
  • 2
    I suggest that you run code line by line in a debugger while monitoring the values of all variables, especially the `counter` variable, in order to determine at which point your program stops behaving as intended. If you did not already try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) – Andreas Wenzel Aug 02 '21 at 12:40
  • At the time of this writing, your question received two downvotes. Although I am not one of the people who downvoted your question, I would like provide a possible explanation for these downvotes: As far as I can tell, the way you asked your question was good. Your question was clear and you provided all code necessary to immediately answer the question. Therefore, as far as I can tell, the reasons for the downvotes probably are (1) [No debugging attempt](https://idownvotedbecau.se/nodebugging/), or: (2) The downvoters found your question too basic to be interesting for other people. – Andreas Wenzel Aug 02 '21 at 12:52
  • If the reason for the downvotes is reason #2, then there is nothing you can do about it. Every programmer must once go through the phase of being a beginner. Personally, I would never downvote a question just because it is a beginner question, if the question is otherwise good. However, I do recommend that you always try to run your code line by line in a debugger before asking a question, so that you question doesn't get downvoted for reason #1. – Andreas Wenzel Aug 02 '21 at 13:01

1 Answers1

2

You're incrementing the counter twice: once in two of the if blocks and once in the for loop line itself. Remove the extra increments in the loop body:

    if (prediction < number) {
        printf("My number is greater than your guess. \n");
        printf("You have %d attempts left \n", (5-counter) );
    }
    
    if (prediction > number) {
        printf("My number is smaller than your prediction. \n");
        printf("You have %d attempts left \n", (5-counter) );
    }
dbush
  • 205,898
  • 23
  • 218
  • 273
  • @Andreas_Wenzel thank you for your suggestions. I did try some alternatives of the code but I felt like adding those here would make a mess. Also thank you for the link, I will check it out. – ikindicayi Aug 02 '21 at 13:24