Sign in to follow this  

this... is a retarded error [help]

This topic is 3740 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

void change() { bool b = true; if(black == 0 && b) { black = 1; b = false; } if(black == 1 && b) { black = 0; b = false; } } so... why doesnt that work? any lang that isnt super retard would easely get this right, but c++ says "OHHHHH black IS ALWAYS 0 LOL" i think this is really retard, and have been working on fixing it for 2 hours. everything i do at this is right, but c++ is doing something retarded that i cant see. i already know that the function is triggering, and that the bug is in this function, so would someone please help?

Share this post


Link to post
Share on other sites
0) Only a poor workman blames his tools. In the future, take a walk, a deep breath, and calm down before you come and post. You're letting your frustration control you, causing you to place the blame on the compiler (I assume this what you mean when you personify C++), which doesn't make you look good.

1) You haven't provided enough information. Post the actual error the compiler is giving you, in full, and the actual source code that you're using -- not a snippet you think is relevant, because (as you don't know what the problem is), you're in no position to judge what is an is not relevant.

The problem the code that you've presented is that "black" is an unknown identifier, which will cause a compiler error. Presumably your real code includes a definition and initialization of "black" somewhere, and you need to show that.

Share this post


Link to post
Share on other sites
what i want it to do
if(black == 0)
black = 1;

else if(black == 1)
black = 0;

if you run this it changes black from 0 to 1 or 1 to 0
no matter what i put it runs both though.

and i've even tried making it return int and return 0 after it changes it once.

also @Mod
wasnt it already in For Beginners? o.O;

Share this post


Link to post
Share on other sites
I think it's supposed to "flip" the value of black, of course there is an easier way.


void change()
{
if( black )
{
black = 0;
}
else
{
black = 1;
}
}


or even just replace the whole function with:

black = black ? 0 : 1;

Edit:

Quote:

if(black == 0)
black = 1;

else if(black == 1)
black = 0;


Why didn't you just do that? The OP made it seem like you didn't know the else statment, what the hell is the point of the variable "b" then?

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
0) Only a poor workman blames his tools. In the future, take a walk, a deep breath, and calm down before you come and post. You're letting your frustration control you, causing you to place the blame on the compiler (I assume this what you mean when you personify C++), which doesn't make you look good.

1) You haven't provided enough information. Post the actual error the compiler is giving you, in full, and the actual source code that you're using -- not a snippet you think is relevant, because (as you don't know what the problem is), you're in no position to judge what is an is not relevant.

The problem the code that you've presented is that "black" is an unknown identifier, which will cause a compiler error. Presumably your real code includes a definition and initialization of "black" somewhere, and you need to show that.


0)your right on this.

2) this is a closed source project, so i cant do that. and its not a compile error, its a logical one.

3) yea, black is an Int set to 0.

basicly you could just say the rest of the code is
int main()
{
int black = 0;
change();
cout << black;
change();
cout << black;
return 0;
}

Share this post


Link to post
Share on other sites
If, as it sounds, this is a boolean variable to gate whose turn it is, or which color the player is (in chess or othello, for example), and even more concise solution is

// Assuming
bool black = true;

// Then
void change()
{
black = !black;
}


But the OP still hasn't addressed the important questions. More context will be helpful. Closed source or not, you've already posted some of, so post more of it. We can't help you without more context -- you're clearly pretty new and or confused about this. The less information you give, the less we can help you. For example, your most recent post details an example that still won't compile (black is not visible in the scope of change) and if it did, wouldn't work the way you probably want (black is a local variable in main).

If you really want to be pointless secretive about your code, go write a mock program that exhibits the same unexpected behavior and post that.

And no, it was posted in Game Programming originally.

Share this post


Link to post
Share on other sites
Quote:
Original post by Scet
I think it's supposed to "flip" the value of black, of course there is an easier way.


void change()
{
if( black )
{
black = 0;
}
else
{
black = 1;
}
}


or even just replace the whole function with:

black = black ? 0 : 1;

Edit:

Quote:

if(black == 0)
black = 1;

else if(black == 1)
black = 0;


Why didn't you just do that? The OP made it seem like you didn't know the else statment, what the hell is the point of the variable "b" then?


cause

Quote:

if(black == 0)
black = 1;

else if(black == 1)
black = 0;


doesnt work >.<

nor does
black = black ? 0 : 1;

-.-|||

Share this post


Link to post
Share on other sites
Quote:
Original post by Robo-Link
doesnt work >.<


because?

Quote:
Original post by jpetrie
1) You haven't provided enough information. Post the actual error the compiler is giving you, in full

Share this post


Link to post
Share on other sites
Quote:
Original post by Robo-Link
basicly you could just say the rest of the code is
int main()
{
int black = 0;
change();
cout << black;
change();
cout << black;
return 0;
}


Immediately, I can see why C++ is always saying black is always zero. I'm actually surprised your compiler hasn't give you a warning in your change function.

If you read up on C++ basics, all of the following will work:
The if test someone posted
black = black ? 0 : 1;
black = black ^ 1;

Share this post


Link to post
Share on other sites
well. since my mock program works fine i'll post some more things that might affect this.

class Player
{
public:
Player()
{
black = 0;
}

int black;
}

and, yes, that mock i posted wouldnt work cause black isnt a global, i know about c++ basics, and a good bit of advanced stuff too, its just this error is geting me.

anyway, would having my code like that do anything?
i assumed that you need the constructed to be the first thing, so i couldnt declare black till after, but i didnt get a compile error, so i figured its okay. and its only public for debuging perpouses.

Share this post


Link to post
Share on other sites
I don't have any new advice to contribute, but I figured I'd go ahead and agree with everyone else in this thread in hopes that it will convince you we're right.

The piece of code you posted, as well as all of the shorter alternatives posted, do in fact work. The problem must lie somewhere else in your code.

Instead of looking at the portion of the code that you suspect is causing this logical error, could we see the portion of the code where you first noticed the error? Like the function that calls change and initializes black?

Share this post


Link to post
Share on other sites
Quote:
Original post by Robo-Link
well. since my mock program works fine i'll post some more things that might affect this.

class Player
{
public:
Player()
{
black = 0;
}

int black;
}

and, yes, that mock i posted wouldnt work cause black isnt a global, i know about c++ basics, and a good bit of advanced stuff too, its just this error is geting me.

anyway, would having my code like that do anything?
i assumed that you need the constructed to be the first thing, so i couldnt declare black till after, but i didnt get a compile error, so i figured its okay. and its only public for debuging perpouses.


Is the change function also a member of the Player class? And are you absolutely certain that you are calling the change function for the same instance of the Player class from which you are reading the value of black?

Share this post


Link to post
Share on other sites
Quote:
Original post by pi_equals_3

Is the change function also a member of the Player class? And are you absolutely certain that you are calling the change function for the same instance of the Player class from which you are reading the value of black?

yes and yes. also just having
player.black = player.black ? 1:0
doesnt even work -.-|||

also, the trigger works fine, cause i tested it with another function, and it called that one. the other function also belonged to player class. and yes, i have used a debugger.

Share this post


Link to post
Share on other sites

#include <iostream>
#include <string>

using namespace std;

class Player
{
public:
Player()
{
black = 0;
}
void change(){black = black ? 0 : 1;}
int black;
};

int main()
{
Player A;
cout << A.black << endl;
A.change();
cout << A.black << endl;
A.change();
cout << A.black << endl;
return 0;
}




Output:

0
1
0

Seems to work fine for me. I suggest you look at jpetrie's post some more.

Share this post


Link to post
Share on other sites
We've all been there.

In my experience, the best method of tackling any *trivial* programming problem is to explain it to someone other than yourself.

As you talk yourself through your problem, it -will- click. Otherwise, they'll see it and help you out.

Share this post


Link to post
Share on other sites

This topic is 3740 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