Sign in to follow this  

Bool giving random numbers

This topic is 2535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I got a problem with bool data type, everytime I cout it in my game loop or add it to watch during debugging:


player[0].boolean = true;
cout << player[0].boolean << endl;



I get random number from 1 to 200+. Anybody knows why? Outside the game loop it works fine [depressed]

P.S. I'm using c++ and directx

Share this post


Link to post
Share on other sites
Sorry for late reply, there's short version of my code:


structures.h file

struct Player
{
bool boolean;
};


main.cpp file

// globals
Player *player = new Player[1];

//before game loop
player[0].boolean = true;

// inside game loop
cout << player[0].boolean << endl;




And in output I'm getting random numbers, can anybody help with this ?

Share this post


Link to post
Share on other sites
#include <iostream>
using namespace std;

struct Player
{
bool boolean;
};

int main() {
Player *player = new Player[1];

//before game loop
player[0].boolean = true;

// inside game loop
cout << player[0].boolean << endl;
}




This consistently prints out 1. You need to include enough code for us to include and reproduce the problem if we're to have any hope of helping you with it. Instead of comments like "// before game loop", show us the game loop. For bonus points, make an actual compilable project (I have a 'test' project I like to reuse), and double check that it actually reproduces the error instead of inadvertently fixing it.

Share this post


Link to post
Share on other sites
Thanks for the reply and the advice [wow], I found the problem by commenting functions in my game loop and after commenting the right one, bool was working. I was using too high index in one of the while loops. [totally]

Share this post


Link to post
Share on other sites
Quote:
Original post by Mafioso
Sorry for late reply, there's short version of my code:

*** Source Snippet Removed ***

And in output I'm getting random numbers, can anybody help with this ?


That's neither a compiling, nor a syntactically correct code snippet. Debugging incorrect code snippets for you does not necessarily yield the answer to your problem with your real code.

Share this post


Link to post
Share on other sites

This topic is 2535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this