0

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.

2
  • pass user_entry by reference, in other words, use a pointer. Commented Oct 23, 2012 at 22:33
  • "and", not "or" ;) And what about "i" - the inner loop repeats many times before it even gets around to checking your "where()" condition. Commented Oct 23, 2012 at 22:38

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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);

}

4 Comments

i had actually had that earlier, and I got same results. I have made the changes, because obviously your logic is correct, but I'm still not able to break the loop after the user enters the correct number.
I forgot to edit the while condition. Always try to translate your condition into plain whatever language you talk. The condition here is as long as flag is not zero AND j is inferior to 3 keep on. Now the loop will break if flag is zero (which means that the user found the right answer) and if j is not inferior to 3.
Or you can just set j = 3 (or 4 or anything greater) manually when you set flag = 0, to make both ORed conditions false.
Using a flag fixed it! Like I said originally, I'm just starting out, so obviously I don't know all the tricks and styles of doing things yet, and your help was greatly appreciated sir! thank you.
0

}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.

2 Comments

What if j < 3 is met, and the user guesses the right answer before j == 3?
Alex Reynolds, nothing bad would happen? Would it not just work as intended?
0

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);

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.