简体   繁体   中英

While loop won't break as intended in C

I'm trying to learn how to program in C and have stumbled into a problem that seems like it should have been a simple fix, but it's giving me more issues then I anticipated. I'm trying to created a number guessing game, where you get three chances to guess the number, but my issue is that the Do While loop wont break when the right answer is guessed. Here is the function:

void Win_Search(int lucky[],const int MAX, int user_entry, int i)
{
    int j=0;

    do {
        j++;
        printf("Please enter a number between 0 and 100\n");
        scanf("%d",&user_entry);

        for(i = 0; i < MAX; i++)
        {
            if(user_entry==lucky[i])
            {
                printf("winner\n");
            }
        }
    } while(user_entry==lucky[i]||j<3);

}

Basically it's supposed to loop through the array lucky[i] and check to see if the user_entry equals any of the 20 numbers in the array. As of right now it loops through, recognizes if a winning number has been selected from the array, but doesn't break from the array.

when I change it to

}while(user_entry!=lucky[i]||j<3);  

it completely ignores the counter and just loops forever.

I don't want to use break because everything I've read about it talks about it's poor programming practice. Is there another way to break, or have simply just made a mistake thats causing this issue.

Thanks in advance.

Consider for a second where your index variable "i" comes from. What happens to it after you've found a correct user entry? Where does the control flow go?

I would suggest having a look at the "break" keyword.

You wrote while (user_entry == lucky[i]..) which translates to as long as user_entry is equal to lucky[i] keep on looping . Which is clearly not what you intend to do.

Transform your condition to } while (user_entry != lucky[i] && j < 3); and you should be fine. This will translate in plain english to as long as user_entry is different of lucky[i] AND j is inferior to 3, keep looping .

But using this, you test on the value of lucky[i] even when i means nothing ( when i is equal to max, you don't want to test it, and this goes in the domain of undefined behavior ).

But if you realy dont want to use break keyword, one solution is to use a flag. Set it to 1 before you start to loop, and change it to 0 when the good answer is found. Your code will become

void Win_Search(int lucky[],const int MAX, int user_entry, int i)
{
    int j=0;
    char flag = 1;

    do {
        j++;
        printf("Please enter a number between 0 and 100\n");
        scanf("%d",&user_entry);

        for(i = 0; i < MAX; i++)
        {
            if(user_entry==lucky[i])
            {
                printf("winner\n");
                flag = 0;
            }
        }
    } while(flag&&j<3);

}

}while(user_entry!=lucky[i]||j<3);

That is bad logic - loop while the user's entry isn't the lucky number OR j is below three? Surely you actually want this:

}while(user_entry!=lucky[i]&&j<3);

This is only the solution to your second issue of it ignoring the counter - the main problem is solved in the other answers.

The only independent condition is that the user has more guesses left. try this while"

while(j <= 3);

The less than should be obvious, but the equals belongs there because you increment your j before the loop so it will be

j = 1 => first guess

j = 2 => second guess

j = 3 => third guess

After that the user should have no more guesses

You should find this doesn't work, that is because we want to exit the loop if the user guesses correctly. To do this, you can use a int as a bool (0-false, 1-yes).

void Win_Search(int lucky[],const int MAX, int user_entry, int i)
{
    int j=0;
    int exitCase = 0;
    do {
        j++;
        printf("Please enter a number between 0 and 100\n");
        scanf("%d",&user_entry);

        for(i = 0; i < MAX; i++)
        {
            if(user_entry==lucky[i])
            {
                exitCase = 1;
                printf("winner\n");
            }
        }
    } while(exitCase == 0 || j <= 3);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM