1

I am a beginner and i know this might be stupide (and yes i have the "correct" code) but can someone please explain to me why my code doesnot work as it should (get the 3 tries)

int main()
{
    int guess;
    int winner = 5;
    int numbertries = 0;
    int maxtries = 3;

    while (guess != winner && numbertries > 3)
        ;
    {
        printf("guess a number: ");
        scanf("%d", &guess);
        numbertries++;

        if (guess == winner)
        {
            printf("you win");
        }

        else
        {
            printf("you lose");
        }
    }
    return 0;
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • 3
    When you reach the `while` the variable `guess` has not been assigned a value (it has *garbage*). – pmg Jul 19 '21 at 09:43
  • 4
    `while(guess != winner && numbertries > 3 );` <= Start buy getting rid of that semi-colon. And I'm not going to sugar coat it: your indentation and brace style is dreadful. I'm updating your post to show what it looks like with better formatting, and how the problem I mentioned is *immediately* obvious. – WhozCraig Jul 19 '21 at 09:43
  • 1
    In addition to the uninitialised variable `numbertries > 3` should be `numbertries < 3` – kaylum Jul 19 '21 at 09:43
  • `while();` is an empty loop (notice the semicolon `;`) ... either infinite loop or never loop (in your case it's a never loop, the remaining code (inside the braces) is not *connected* to the `while` and runs once only) – pmg Jul 19 '21 at 09:44
  • guess=0, numbertries<3, remove ; after while... – Lalit Verma Jul 19 '21 at 09:45
  • 3
    Also, using `scanf()` on user input without checking the return value. Aside from `guess` being uninitialized on the *first* iteration of the loop, it may be un-(re-)initialized on any *subsequent* loop as well. (Short rule: **Always** check the return value of `*scanf()`.) – DevSolar Jul 19 '21 at 09:48
  • 1
    For starters, enable all compiler warnings and follow up on them. That already would have caught 50% of the mistakes spotted by commenters. – Ruud Helderman Jul 19 '21 at 09:51

2 Answers2

1

First of all, you should be using a do{}while();. This way, when the while tests, the guess variable has a value. Second, change it to numbertries < 3 from numbertries > 3, so it can actually count your guesses. Third, you should use the variable maxtries instead of 3, once you created it. When done, your code should look something like this:

int main()
{
    int guess;
    int winner = 5;
    int numbertries = 0;
    int maxtries = 3;

    do
    {
        printf("guess a number: ");
        scanf("%d", &guess);
        numbertries++;

        if (guess == winner)
        {
            printf("you win");
        }

        else
        {
            printf("you lose");
        }
    }while(guess != winner && numbertries < maxtries);
    return 0;
}
-1

The main problem is that you didn't started the while loop, and also the condition was wrong. I've commented some errors in the program, and also some recommendations. Then I've added the same code with the errors fixed.

Original code with comments

int main()
{
    int guess;
    int winner = 5;
    int numbertries = 0;
    int maxtries = 3;


    //The first error that I see is that you used a semicolon after the while declaration.
    //This is only used in the do-while loops, but in this case, you shouldn't use it because
    //this will skip all the while loop.

    //The second error is that you'r trying to repeat it when numbertries is higher than 3, but
    //in the begginning, numbertries is 0. The condition should be 'numbertries < 3', so you can
    //start the loop. Also I recommend you to use the variable 'maxtries' instead of the number itself.
    while(guess != winner && numbertries > 3 );
    {
        printf("guess a number: ");
        //Here I recommend you to clear the input buffer, using the command fflush(stdin);, included in the <stdio.h> library
        scanf("%d", &guess);
        numbertries++; 
        
        if(guess == winner)
            //At the end of the messages, you should add a new line character ('\n')
        {    printf("you win");   }
           
           else {printf("you lose");}

    }
        return 0;
}

New code with fixed errors

#include <stdio.h>

int main()
{
    int guess;
    int winner = 5;
    int numbertries = 0;
    int maxtries = 3;

    while(guess != winner && numbertries < maxtries )
    {
        printf("guess a number: ");
        fflush(stdin);
        scanf("%d", &guess);
        numbertries++; 
        
        if(guess == winner) { printf("you win\n"); }  
        else { printf("you lose\n"); }
    }
        return 0;
}

Wish I could help! Good luck!

davidjs11
  • 1
  • 2
  • thank you, i didnt know about the ; which is only for do-while – Anthony Oknaian Jul 19 '21 at 09:59
  • 1
    The presented solution also has undefined behavior, as the expression `guess != winner` is reading from an uninitialized variable. As pointed out in the other answer, a `do`...`while` should be used instead. – Andreas Wenzel Jul 19 '21 at 11:48
  • need to initialize guess or with '0' or with 'winner - 1' (to be sure that it's initialized and not equat to winner) – Igor_M Jul 19 '21 at 11:50
  • The line `fflush(stdin)` also [has undefined behavior](https://stackoverflow.com/q/2979209/12149471). – Andreas Wenzel Jul 19 '21 at 13:56
  • @AndreasWenzel thanks for the advice of fflush(stdin), I didn't know about the problems, and the tutorials I've seen actually use it... – davidjs11 Jul 20 '21 at 01:50
  • Note that you can [edit] your answer in order to fix the errors in your answer. – Andreas Wenzel Jul 20 '21 at 12:34