using a variable to refrence a variable inside an array

so im making a game of battleships as a refresher to get back into c++. i made a post the other day about array issues, and thats now fixed thanks to your guys help. now i have... not an issue, a road block- i know what i want to do, i dont know how to do it.

in the game, players need to enter a tile selection (i.e. E,9)for either ship placement or target selection. when that coordinate is entered, im going to store it in a variable via

cin.getline() .

now what i want to be able to do is then take the variable in which the coordinate is stored and use it to call up the variable attached to the coordinate.
ie:

1
2
3
4
5
6
7
8
9
 

        //this isnt my code im using, just an example- input is user enter coords, gui is what will be printf'd, ship present is for math purposes.
 
        if (input = a7)
           { 
             gui_image_a7 = O;
             ship_present_a7 = 1;
           }


the problem is, i dont want to have to write an if statment for each of 100 tiles on my gameboard. is there any function i can use to select a variable in an array from another variable(s) that contain the uer entered data? ie:

1
2
3
4
5
6
7
8
9
10
11
12
13

char vert; //vertical
int hori; //horizontal

cout << "please enter coords:" << endl;
cout << "vertical:";
cin >> vert;
cout << endl << "horizontal:";
cin << hori;

vert[hori] = "X" 
//what i would intend for the system to see by this line is E[9] = "X". i know that dosnt work, but thats what i need to figure out.


is there any function for this, or any way to do it- to use a variable to refrence a variable in an array?
A whole lot of ideas suggest themselves.

Since this about fun and just learning things, perhaps you'd like to do it with a std::map. Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
string vert; //vertical
string hori; //horizontal

map<string, char> coordinatesToValueMap;

cout << "please enter coords:" << endl;
cout << "vertical:";
cin >> vert;
cout << endl << "horizontal:";
cin << hori;

// Should check the input values to be sure they're valid before using them
coordinatesToValueMap[vert+hori] = "X";


Putting values into the map, with the key being strings (for example, "a7" or "e5"), and reading them back with the same key.
normally since you have 2 dimensional gameboard you should have a 2 dimensional array as well.
Since this about fun and just learning things, perhaps you'd like to do it with a std::map. Something like this


exelent, thank you. but what exactly is happening here? im unframiliar with the map command. its just if i dont understand it, i wont remember it.

normally since you have 2 dimensional gameboard you should have a 2 dimensional array as well.


your right, i've been thinking that from the start of this project, and more so now because i've ended up with a boat load single line arrays that each need to be individulay declared. its a pain and im already thinking of doing a bit of redesign. i didnt go with a 2d array from the start because im not realy confident with them- i know of them, i've used, i never realy got used to them, and now i've forgoten them completely except that they're a thing that exists, lol. i thought single line arrays would be sufficiant because at the start i didnt realy have an accurate concept of how much i'd be using those arrays in the design. its painfull, i've spent more time duplicating sections of code then writing new ones. im gonna look into 2d arrays and figure them out.

also coder, i never said thanks in my last thread, you solved my problem for me. i ment to go back and say thanks but i couldnt find the thread.

so thanks, lol
Topic archived. No new replies allowed.