[ACCEPTED]-initializing a const multidimensional array in c++-multidimensional-array
you could make 2d array?
string aArray[][2] = {{"wall", "brick..."},
{"glasses", "corrective eye tool thingymajig"},
{"calculator", "number cruncher"}};
not sure if the 6 syntax is right but i hope you get the concept.
it's 5 an array inside another array.
EDITED: i 4 removed the NUM_WORDS from the first square 3 bracket. you can't declare multi dimentional 2 array from variables.. sorry i forgot about 1 that. i just tested it and it works.
Use a struct array:
typedef struct
{
string Word;
string Hint;
}
WORDDEF;
const WORDDEF WordList[] =
{
{"wall", "brick..."},
{"glasses", "worn on head"},
...
};
Then you refer to them 1 as:
printf ("For entry %d, Word is %s, Hint is %s\n", 1, WordList[1].Word, WordList[1].Hint);
huh... maybe you should use pair struct?
std::pair<std::string, std::string> WORDS[10] = { make_pair("wall", "brick"),
make_pair("glasses, worn") }
now, you 2 can use WORDS[i].first -- as the unknown 1 word
and WORDS[i].second as its hint.
Here is a simple example That will give 6 you a 2 dimensional array[2][2] from the 5 other two arrays that you already have that 4 you can use as a starting point.
#include "stdafx.h"
#include "iostream"
#include "string"
#define NUM_WORDS 2
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const string WORDS[NUM_WORDS] = {"wall", "glasses"};
const string HINTS[NUM_WORDS] = {"brick", "worn on head"};
string hw[NUM_WORDS][NUM_WORDS];
for(int i=0; i < NUM_WORDS; i++)
for(int j=0; j < NUM_WORDS; j++)
{
if(i > 0)
hw[i][j] = HINTS[j];
else if(j > 0)
hw[i][j] = WORDS[i+1];
else
hw[i][j] = WORDS[i];
}
for(int i=0; i < NUM_WORDS; i++)
for(int j=0; j < NUM_WORDS; j++)
{
cout << "hw[" << i << "][" << j << "]= " << hw[i][j] << endl;
}
char c;
cin.get(c);
return 0;
}
For some 3 reason no matter what I do the box will 2 not let me copy and paste the code in so 1 that it formats correctly. sorry.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.