Find the answer to your Linux question:
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 ...
  1. #1
    Linux Newbie Themer's Avatar
    Join Date
    Feb 2008
    Posts
    171

    [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:
    Code:
    cin >> move
    where move is a 2 letter character variable.

    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
    Code:
    c1 = 'x'
    if they enter ,say, a2, I want it to (effectively) do
    Code:
    a2 = 'x'
    but I don't want to write them all (for simplicity of the code).
    basically, the actual code should be this:
    Code:
    move = 'x'
    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?

    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!

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    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

  3. #3
    Linux Newbie Themer's Avatar
    Join Date
    Feb 2008
    Posts
    171
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...