Archived

This topic is now archived and is closed to further replies.

CompuWizJacob

Bit of code

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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 ==.

Share this post


Link to post
Share on other sites