Howdy all. I'm only in college for programming and am only in the my 3rd month of C++, and I'm working on a program now, just for fun and practice, and need some help. I'm making a MineSweeper game that will run in Command Prompt by making a grid of Arrays that will act as a grid to place the bombs and numbers in. However, I'm setting it up so you can choose how many rows and columns you want... ex. column1[0-4], column2[0-4] , etc. etc.

However, when I'm trying to make the loop to construct to the arrays, I'm not quite sure if I can concatenate the word column and a variable to form the array names desired.

Sorry for the stupid question, but like I said, I'm new! Thank you all in advance!

Wouldn't using a preprocessor definition work?

For example--

#include <cstdlib>
#include <iostream>

#define COLUMN(arg) arg

using namespace std;

int main(int argc, char *argv[])

    typedef int column[5]; //anything marked with column before its variable name will be
                                     //an int array of size 5
    column COLUMN(a); //an int array of size 5, variable name a. Using preprocessor definition
                                  //so a is still associated with the word column
    column COLUMN(b); //an int array of size 5, variable name b... 
    column COLUMN(c); //an int array of size 5, variable name c...
    COLUMN(a)[0] = 1; //using these as examples for proof that the array definitions were legitiment
    COLUMN(b)[0] = 2;
    COLUMN(c)[0] = 3;

    cout << COLUMN(a)[0] << endl; //further debugging to test the typedef and preprocessor macros
    cout << COLUMN(b)[0] << endl;
    cout << COLUMN(c)[0] << endl;

    cin.get( );
    return 0;

--but you might have to write a ridiculous amount of variables which would make this seem redundant.

Then again if it's easier for you to reference the variable in a familiar manner, like this one, then I suppose it's alright.

This article has been dead for over six months. Start a new discussion instead.