Why does this always evaluate true?

1
2
3
4
5
6
7
8
9
10
11
int checkWin()
{
    for(int i = 0; i < 18; i++)
    {
        if(betNum[i] == x)
        {
            return 1;
        }
    }
    return 0;
}

Why does this always evaluate true?
Thanks in advance.
betNum[0] - betNum[17] are the numbers betting on by the player, x is the winning number.
Last edited on
Because at least one entry in betNum is equal to x
Well, that's the thing:

This is for a roulette game.
I have all the betNums set to 40 at default, and last time I bet on the number 1, and the winning number was 13. It still evaluated true.
That's all I can tell you from the code you posted. betNum[i] must be equal to x. That's the only way that function can return true. Without seeing more code it's impossible to diagnose further.

Perhaps you are not setting 'x' or 'betNum' correctly.
Why don't you cout betNum[i] and x so you can see for yourself?

X must be a global variable because if it isn't you should get a compiler error. Also, know that C++ allows for the bool variable so you can use true and false instead of 0 and 1.
Thanks for your replies.
I realized I should use bool short after I posted this here, so I did. Thank you, though. x is a global variable, so is betNum.
I couted betNum and x inside the for-loop like this:
1
2
3
4
5
6
7
8
for(int i = 0; i < 18; i++)
    {
        cout<<betNum[i]<<" "<<x<<endl;
        if(betNum[i] == x)
        {
            return 1;
        }
    }

I bet on number 1, and the winning number was 35. Here's what I got:

1 35
40 35
40 35
40 35
40 35
40 35
40 35
40 35
40 35
40 35
40 35
40 35
40 35
40 35
40 35
40 35
40 35
40 35

But still it returns true.
try to cout inside if statement.
But still it returns true.


It probably doesn't. I think you are misdiagnosing the problem. It's far more likely you are misusing the value this function returns.

But as dandy is suggesting, you need to find out what the program is actually doing, rather than guessing. Adding cout statements everywhere is one way to do this, but I find that an easier way is to just use the debugger. Now is a perfect opportunity to learn how to use it.

Here's a crash course on the basics:

There are two key features you want to use: "Breakpoints" and "Watch"

-) Set a breakpoint on code you're interested in (default key to set/remove a bp on the current line of code is F9 in MSVS). Here, you are interested in the checkWin function, so I would set a breakpoint at the start of that function.

-) Run the program and get the bug to trigger. When the program executes checkWin (or whatever line of code you put your breakpoint on), the debugger will "snap", bringing your IDE to the front and effectively "freezing" your program.

-) Open up a "Watch" window (in VS, go in the "Debug" menu -> Windows -> Watch). The watch window lets you type in the name of a variable and see its current contents. You can even modify variable contents in realtime if you want to do some tweaking.

-) Walk through your code and make sure variables are what you expect, and the program is running code you expect. You can use the below options for walking through code:

---) "Step Over" (default key=F10 in VS). Unfreezes your program and runs it for one line of code, then freezes it again. If that line of code is a function call, it will skip over the body of the function.

example:

1
2
int var = func();  // <- if we are frozen here, and we StepOver
var += 3;   // <- the debugger will freeze here 


---) "Step Into" (default key=F11 in VS). Same as Step Over, but if the line calls a function, it will go into it:
1
2
int var = func();  // <- if we are frozen here, and we StepInto
  // ... the debugger will freeze on the first line of the 'func' function 



---) "Step Out" (default key=Shift+F11 in VS). Runs until the current function exits, then freezes again.


-) once you're done debugging you can run (default key=F5 in VS) to "unfreeze" your program.



So yeah... do that. Step through the code, find out what it's actually doing, and see where your program is going wrong.

You can also use breakpoints to quickly see if code is actually executing. IE: if you put a breakpoint on that return 1 line, you'll quickly know if that function actually is returning 1 or not (if the breakpoint never trips, you know it isn't)
Last edited on
A more revealing cout would be
1
2
3
4
5
6
7
8
for(int i = 0; i < 18; i++)
    {
        if(betNum[i] == x)
        {
             cout<<betNum[i]<<" "<<x<<endl;
            return 1;
        }
    }
Last edited on
... or just set a breakpoint on the return 1 line.

Learn to use a debugger, people!
There are two key features you want to use: "Breakpoints" and "Watch"


Seriously, your post should be stickied to the top of this forum.

Knowing how to use the debugger would have previously saved me quite a bit of time as you can see what's going wrong with ANY variable with the "watch" window you talked about.
1. I already tried a cout inside the if-statement, but it didn't output anything, so it has never gotten into the if-statement.

2. @Disch, thank you very much for the elaborate explanation. I never really used the debugger before. Thank you for teaching me something new.

I put a breakpoint at the return 1 line, and at the return 0 line. It didn't get to the return 1 line, but it did get to the return 0 line. Still, when I cout checkWin, it outputs 1.
I also get this build message:
<path>|1041|warning: the address of 'bool checkWin()' will always evaluate as 'true'|
Ah. You aren't calling checkWin.. you are outputting the function itself (ie: a pointer to it).

1
2
3
bool foo = checkWin;  // what you're doing... bad

bool foo = checkWin();  // what you need to do 
Ah, okay... That's it. I understand now. Thank you!
Topic archived. No new replies allowed.