# 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.

## 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 on other sites
Moving to For Beginners.

##### Share on other sites
Quote:
 Original post by Robo-Linkso... why doesnt that work?

What does it do? What do you expect it to do?

##### 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 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 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 on other sites
Quote:
 Original post by jpetrie0) 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.

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 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
// Assumingbool black = true;// Thenvoid change(){  black = !black;}

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 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 on other sites
Quote:
 Original post by Robo-Linkdoesnt work >.<

because?

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

##### Share on other sites
Quote:
 Original post by Robo-Linkbasicly you could just say the rest of the code isint 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 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 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 on other sites
Quote:
 Original post by Robo-Linkwell. 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 on other sites
Have you tried using a debugger?

##### Share on other sites
Quote:
 Original post by pi_equals_3Is 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 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 on other sites
Are you sure you're programming in c++?

##### Share on other sites
yea, my bad. turns out it was being called 10 times instead of once >.<
anyway, it wasnt a problem with change(), but thanks for your help on making change a lot more simpler.

##### 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 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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628707
• Total Posts
2984313

• 23
• 11
• 9
• 13
• 14