0

In my case, the code I want looks something like this:

anRow0[] = {0,0,0,0,0,0,0} anRow1[] = {0,0,0,0,0,0,0} anRow2[] = {0,0,0,0,0,0,0} int place(int nVar1, int nVar2) //Where nVar1 is the row and nVar2 = current turn { int nVert = 0; while (anRow\i want to add nVar1 here\[nVert+1] && nVert < 6) { nVert += 1; } anRow[nVert] = nVar2; return true; } 

I could make several "if (nVar1 == 0) //check for anRow1[] etc. but that seems inefficient.
Is there a way to add a number like this? Also, please ignore the rest of code, I know you could for example replace the while with a for, but that's besides the point.

Appreciate any help

7
  • 2
    Why not just using 2d array? p.s. c++ does not support much metaprogramming Commented Sep 20, 2013 at 6:58
  • anRow\i want to add nVar1 here\[nVert+1] ? Commented Sep 20, 2013 at 6:58
  • How do you format a 2d array in c++? I I've seen array[x,y] but that doesnt seem to work. the anRow\i want to add nVar here\means i just want to "paste" the characters of nVar to the name of anRow. But a 2d array is better, just that Im not sure how to write one. Commented Sep 20, 2013 at 7:01
  • @user2798044 When you saw array[x,y] you were looking at a different language, in C++ it's array[x][y] Commented Sep 20, 2013 at 7:02
  • @user2798044 I'm wondering why you asked about adding chars to a variable name when you knew that a 2D array is better. Why not just ask about a 2D array? This is a classic case of the XY problem, meta.stackexchange.com/questions/66377/what-is-the-xy-problem Commented Sep 20, 2013 at 7:05

5 Answers 5

2

Seems like you want a two dimensional array, something like this

int an[3][6] = {{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}}; int place(int nVar1, int nVar2) { int nVert = 0; while (nVert < 6 && an[nVar1][nVert+1]) { nVert += 1; } an[nVar1][nVert] = nVar2; return true; } 

Although that code is undoubtedly bugged (would be better with nVert < 5). Still fixing the bugs is another question.

Sign up to request clarification or add additional context in comments.

Comments

1

This is not possible, since C is a compiled language and the variable names are translated to memory addresses. At run-time, the code does not even know the name of the variables in your source code.

If you want to choose a value by runtime-data, you will always have to use arrays. There are multiple options here, the best for C++ is to use some of the STL-containers (std::array, std::vector, std::list, ...), but you may also use C-style arrays.

Comments

1

You could put the arrays in another array, like this:

std::vector<int*> allRows = { anRow0, anRow1, anRow2 }; 

Then use a variable to index the allRows vector, like

allRows[i][nVert] = nVar2; 

Or better, use an std::array of std::array:

std::array<std::array<int, 7>, 3> allRows = {{ {{ 0, 0, 0, 0, 0, 0, 0 }}, {{ 0, 0, 0, 0, 0, 0, 0 }}, {{ 0, 0, 0, 0, 0, 0, 0 }} }}; 

1 Comment

Isn't mixing containters and arrays like this a bad practice? Of course it solves the problem at hand, but still...
1

You could use a two dimensional array instead of 3 1-dimensional arrays. You can read something about them here: http://www.cplusplus.com/doc/tutorial/arrays/

Here's a possible modification:

int an[3][7] = {0}; int place(int nVar1, int nVar2) //Where nVar1 is the row and nVar2 = current turn { int nVert = 0; while (anRow[nVar2][nVert+1] && nVert < 6) { nVert += 1; } anRow[nVert] = nVar2; return true; } 

By the way, why are you are returning "true" although the return value is an int. It's allowed, but I would recommend not doing it. Return 1 instead, or change the return value to boolean.

Comments

0

No you cannot manipulate the names of variables, functions, or really anthing syntactically - if you really need this C++ is the wrong language. There may be some very limited set of functionality available by using preprocessor macros, but that is really not what you want.

Instead use containers to store your variables:

std::vector<std::array<int, 7>> rows; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.