Bit of code

Started by
6 comments, last by CompuWizJacob 21 years, 6 months ago
Here''s a simplified version of some code i''ve been working on: #include <iostream.h> int xpositions[9]; void play() { int move; cout << "Where would you like to move?: "; cin >> move; } void processmove(int move) { xpositions[1] = 1; if(move == xpositions[1]) { cout << "It works!\n"; } } int main() { play(); int q; //i''ll use this to stop the program cin >> q; return 0; } If my code isn''t obvious enough, when program flow goes to play(), the user inputs a number. The number is then sent to processmove(int move), where it is checked against one of the elements of the array xpositions[9]. My problem is, when run the program and type in 1 for move, the if statement is not triggered (it should be because the number i''m comparing it against is 1). I have a feeling that the error is with the array, but I can''t find it.
Advertisement
I''m not sure how much you''ve simplified the code, but ... you do realise that you''ll need to actually call the functions, right?
well, judging from only the code you posted, move is a local variable inside of the play() function, and doesn''t ever see the light of day outside of that scope.

if there isn''t anything else going on, make move a global and see if that fixes your problem.

do that by taking it out of the function:

int move;
void play()
{
cout << "Where would you like to move?: ";
cin >> move;
}
dedrickwww.whiteknucklegames.com
Woa, sorry, got a little ahead of myself. I call play() in main, and I meant to call process move(int move) at the end of play(). So now play() looks like this:

play()
{
int move;
cout << "Where would you like to move?: ";
cin >> move;
processmove(move); // this is where I meant to write the call
}


Note that I still have a problem, because this was an error in transferring from my compiler to the forum. I didn''t have that mistake in my compiler version.

here''s my code, pretty much pulled directly from your original post:

#include <iostream.h>

int xpositions[9];

void processmove(int move)
{
xpositions[1] = 1;

if(move == xpositions[1])
{
cout << "It works!\n";
}
}
void play()
{
int move;
cout << "Where would you like to move?: ";
cin >> move;
processmove(move);
}


int main()
{
play();

int q; //i''ll use this to stop the program
cin >> q;
return 0;
}


here''s the output when i type in ''1'' then hit enter:

Where would you like to move?: 1
It works!

dedrickwww.whiteknucklegames.com
Sorry, dedrick, I posted what this post was beforehand before I read your latest post. Yeah, the code you put works. I'm trying to look for differences between that and what I have in the actual program.

[edited by - CompuWizJacob on October 16, 2002 11:35:09 PM]
Thanks dedrick, problem solved. It turns out that I was using a == where I was supposed to be using a =. Stupid mistake, i''m not afraid to admit, and even though I found the problem (because it was only in my code) I think you re-writing the code made me look hard at it. Thanks for your time!
We all mix the = and == incorrectly every once in a while. Even after 10+ years of C programming, I put a if statement
if (CurrentState.GameState = GAME_WON)

the other day and I was getting a bug, and it took me a few minutes to see I needed a ==.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement