while() conditions

Hi everyone,
Today I spotted an error in my code and, unfortunately my IDE doesn't have any debug mode (actually it just doesn't work) so I'm referring to YOU guys to find out where is my error.

Before the code, I just wanted to tell you that I think the error comes from the while conditions loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    cout << "Please enter your name: ";
    Player player;
    player.setName();

    Player cpu(Randname());
    cout << cpu.getName() << " just entered the table.\n" << endl;

    while(!cpu.lost() || !player.lost() || !player.hasBlackjack() || !cpu.hasBlackjack() ||(!player.stands() && !cpu.stands()));//AI OR PLAYER IS ALIVE or player and cpu don't stand
    {
        if(!player.lost())
        {
            //PLAYER TURN
            PlayerTurn(player);
        }

        if(!cpu.lost())
        {
            //CPU TURN
            AITurn(cpu);
        }

    cout << "DEBUG end of turn." << endl;
    }


AAaand I can never see the "DEBUG end of turn" line :(
Am I even entering the loop?
Thanks for every answer I get
closed account (3qX21hU5)
If you never see the "DEBUG end of turn" you are not entering the while loop. Though we won't be able to tell you what the error is and why it is occurring without seeing all those functions you have in the while condition.

I would suggest start studying up on how a debugger works and if you don't have a working one get one. They can be the most important tool a programmer has in his toolbox so it is basically a must to know how they work and how to use them.

Here is a very good one for VS2010 that you can use. http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn

This will also teach you the concept and the knowledge can be transferred to almost any debugger.
Last edited on
+1 @ Zereo. Nothing beats a debugger for when you have to debug.
Incoming huge amount of code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int Player::cardsvalueAdd()
{
    int value=0;
    for(unsigned i=0;i<ownedcards.size();i++)
    {
        value += ownedcards[i].Value();
    }
    return value;
}

bool Player::lost()
{
    if(cardsvalueAdd() > 21)
        return true;

    else
        return false;
}

bool Player::stands()
{
    return Card::stand;
}

bool Player::hasBlackjack()
{
    if(this->cardsvalueAdd()==21)
    {
        return true;
    }
    else
        return false;
}


Hope you can get something out of that

Edit: in fact, not that huge..
Last edited on
This really just needs to be debugged. If you can't debug yourself (since you say your debugger is broken?), then perhaps can you post the full source so that we can compile/debug on our machine? If it's too big to post on the board, you can always zip it up and throw it on dropbox or something.
> unfortunately my IDE doesn't have any debug mode
burn it.


first snip, semicolon `;' on line 8
Last edited on
Paul Combaldieu wrote:
26
27
28
29
30
31
32
33
{
    if(this->cardsvalueAdd()==21)
    {
        return true;
    }
    else
        return false;
}
Why would you put braces around one and not the other? That's like wrapping a box of milk chocolate but not wrapping an identical box of dark chocolate, and giving both to your loved one...you love your compiler, don't you?

Also, have you tried outputting at the beginning of the loop to see if it even gets in the loop or not? Putting output at the end after a whole load of statements is not helpful.
Because you wrapped a box of milk chocolate, bananas and a car. While you were eating they robbed you.
All that's left is milk, nicely wrapped. You are too depressed to undress it.

By the way, it is not the compiler you should worry about.
Do we really need an if statement at all?

1
2
3
// also add const specifiers to cardsvalueAdd() etc 
bool Player::hasBlackjack() const 
{ return cardsvalueAdd() == 21 ; }
first snip, semicolon `;' on line 8


Ah... yep. That's gotta be it. That'll deadlock your program.

Good catch, ne555
Yeah nice catch! I'm not on my computer right now so I can't check it out, but it's probably it. This loop used to be a do{}while; that's why there is a semicolon, my bad.
Anyway thanks to all of you guys for answering me and proposing me upgrades for my code that really helped me out ;)
> Yeah nice catch!

Compile with warnings enabled at the highest level and a reasonable compiler would catch it for you.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
    int i = 7 ;

    // g++ with -Wextra: *** warning: suggest braces around empty body in an 'if' statement
    // msvc with -W4: **** warning ';' : empty controlled statement found; is this the intent?
    if( i == 7 ) ;
        std::cout << "hello world\n" ;
}
g++ does not give a warning if it were a `while' instead of an `if'
or if there is an `else'
> g++ does not give a warning if it were a `while' instead of an `if'

Yes. Thanks.

Perhaps because while( *dest++ = *srce++ ) ; is almost canonical.


> if there is an `else'

Then it only warns about an empty body in the else statement, which is reasonable. Because unless the if having an empty body was intented, we would have got an error: else without a previous if.
I have seen in ugly code many times:
1
2
3
4
if(condition)
    ;
else
    statement;
It must be a style that is required to be followed, but it explains why the warning would go away.
Topic archived. No new replies allowed.