views:

749

answers:

4

I'm currently working through some exercises in a c++ book, which uses text based games as its teaching tool. The exercise I am stuck on involves getting the pc to select a word from a const array of words (strings), mixing the letters up and asking the player to guess the word. This was easy, but as a follow on the book asks to add the option to provide a hint to the player to help them guess, firstly as a parallel array (again, no problem) and then as a 2 dimensional array. This is where I'm stuck. My (shortened) array of words is as follows:

const string WORDS[NUM_WORDS] = {"wall", "glasses"};

I need to provide a hint for each of these words but not sure how to go about it. Posting this from phone so extensive googling is not an option!

My parallel array was as follows:

const string HINTS[NUM_WORDS] = "brick...", "worn on head"};

Just need to combine the two.

Thanks in advance, Barry

A: 

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 can use WORDS[i].first -- as the unknown word

and WORDS[i].second as its hint.

f0b0s
or maybe you just need to use yours arrays and same index:word = WORDS[i], hint = HINTS[i]i dont really get the question
f0b0s
you could also consider a vector of pairs of strings, which may make coding easier and reduce bugs.std::vector< std::pair< std::string, std::string > >Note the space between the > > at the end there.The initialisation using a single statement won't be possible though (afaik)
pxb
yeah, I know, but vectors don't have initialization with {}, only by copying c-arrays.
f0b0s
Thanks foobs. I have done it using 2 arrays but the next part of the question wants a 2 dimensional array.
barry
+1  A: 

you could make 2d array?

string aArray[][2] = {{"wall", "brick..."}, 
                               {"glasses", "corrective eye tool thingymajig"},
                               {"calculator", "number cruncher"}};

not sure if the syntax is right but i hope you get the concept.

it's an array inside another array.

EDITED: i removed the NUM_WORDS from the first square bracket. you can't declare multi dimentional array from variables.. sorry i forgot about that. i just tested it and it works.

huh, pair is more c++ stylish, arrays are c-style...
f0b0s
well he explicitly mentions that he wants it as 2d array
and he explicitly metions that he is newbie, so I think he should be warned that c++ != c
f0b0s
you need to close "}" at the end
begray
Thanks for that. I do get the concept and had tried it but like you don't know the syntax.
barry
edited for correction..
f0b0s, i think it's rather hard to grasp the distinctions between c++ and c, esp for newbies (including me). besides, he's doing exercises from a book...
Sojiee, the NUM_WORDS in the first [] works for me. Anyway, thanks for your answer, and everyone else - what a great resource this is!
barry
A: 

Here is a simple example That will give you a 2 dimensional array[2][2] from the other two arrays that you already have that 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 reason no matter what I do the box will not let me copy and paste the code in so that it formats correctly. sorry.

ChadNC
You have to indent code by 4 spaces (8 when the code is after a list). You can do this by highlighting the code, then clicking the "1010" icon.
GMan
A: 

Though the book tells you to use 2 arrays, the better way to do this would be an array of structures so that you can access the words and hints by name rather than by an integer index. An ad hoc structure is also better than a pair since you can name the members. Another useful trick in C++ is to have the compiler calculate the number of elements in the array using the sizeof operator. This reduces redundancy in the code which reduces the chances of bugs.

int main(){
  /////////////version 1 - the way I would do it
  struct element{
    string word;
    string hint;
  };

  const element array[] = {
    {"wall", "brick..."},
    {"glasses", "corrective eye tool thingymajig"},
    {"calculator", "number cruncher"}
  };    
  const size_t NUM_WORDS = sizeof(array)/sizeof(element);

  for(size_t i=0; i<NUM_WORDS; i++){
    cout << array[i].word << ": " << array[i].hint << endl;
  }

  /////////////version 2 - the way the book asks for it
  const string array2[][2] = {
    {"wall", "brick..."},
    {"glasses", "corrective eye tool thingymajig"},
    {"calculator", "number cruncher"}
  };    
  const size_t NUM_WORDS2 = sizeof(array2)/(sizeof(string)*2);

  for(size_t i=0; i<NUM_WORDS2; i++){
    cout << array2[i][0] << ": " << array2[0][1] << endl;
  }    
}
SigmaXiPi
Thanks for the answer. Why do we need to multiply sizeof() by 2 when getting the size of the array?
barry
sizeof(string)?!!! sizeof class != size of chars of the string.sizeof string will be larger, or lesser -- depends on realisation. but it's WRONG! this sizeof trick on c-arrays is good only for POD types.
f0b0s
the first variant is wrong too.just test it. sizeof(string("abcd")) won't be 4 or 5, it could be for example 32
f0b0s
barry - a string or any other object takes up X bytes of memory so sizeof(string) will return X. In version 2, I have 6 strings in array2 so sizeof(array2) will return 6*X. However, since I'm pretending the 2D array is a 1D array of pairs of strings, I have to divide the number of bytes used in the array by 2 to get the number of entries in the array. Thus NUM_WORDS = ((6*X)/(X))/2.
SigmaXiPi
f0b0s - I'm not concerned with the number of characters in a given string since NUM_WORDS is not dependent on the length of any given string in the dataset. I only care about the number of entries in the array so sizeof(array)/sizeof(element) gives me what I need.
SigmaXiPi