Jump to content
  • Advertisement
Sign in to follow this  
Mafioso

Bool giving random numbers

This topic is 2819 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
Advertisement
Make sure the "player" array is properly created. If it is dynamic and isn't being allocated properly it might point to the junk you're seeing.

Share this post


Link to post
Share on other sites
I'm going to guess that the struct of which 'boolean' is part of contains an array, and that this array is being overrun.
Please post the definition of this struct.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!