Results 1 to 3 of 3
OK, I'm trying to do something fairly simple (in thought, but possibly not in practice) but a little weird. Let's say I have a list of character variables (1 letter ...
- 02-04-2009 #1
[C++] setting a variable in another variable
OK, I'm trying to do something fairly simple (in thought, but possibly not in practice) but a little weird. Let's say I have a list of character variables (1 letter each). like a1, a2, a3, b1, b2, etc. up to c3 (9 characters). I take input (in character form) like so:
where move is a 2 letter character variable.Code:cin >> move
I am trying to make it so that the user basically enters which of the first 9 variables they want to equal "X". So, what I want to happen is the following:
If the user enters "c1", I want to (effectively) do
if they enter ,say, a2, I want it to (effectively) doCode:c1 = 'x'
but I don't want to write them all (for simplicity of the code).Code:a2 = 'x'
basically, the actual code should be this:
but instead of setting "move" to x, I want to set the variable that "move" contains (for example, c1, or a2) to x, effectively making "move" turn into whatever the user inputs. Does this all make sense? If so, how might I do this?Code:move = 'x'
EDIT:
In very short, I am trying to put "x" into the variable that the user enters, be it a1, c2, etc. whatever ("move" is the variable "inputed")When I find myself burried in errors, Windows Help appears to me; speaking words of wisdom, Reboot!
- 02-04-2009 #2
Why don't you use an array of characters char myarray[9] and have the user enter which array element he/she wants to work with...i.e cin>>intval then myarray[intval] to access the array element...Hope this helps...Gerard4143
- 02-04-2009 #3
Ah.... yes, that would be most logical!
I've used a bunch of other languages before, but I'm relatively new to C++ (as you may have noticed
)
When I find myself burried in errors, Windows Help appears to me; speaking words of wisdom, Reboot!


Reply With Quote