using integers in string arrays

I was wondering if I could have an integer value in a string array.
For example:

1
2
3
4
5
6
7
8
9
const string WORDS[5][3] = 
    {
        {"wall", "Do you feel you're banging your head against something?", 10 },
        {"glasses", "These meight help you see the answer.", 50 },
        {"labored", "Going slowly, is it?", 50 },
        {"persistent", "Keep at it.", 100 },
        {"jumble", "It's what the game is all about.", 50 }
    };


Only if you store then as strings ("10"). Otherwise you could write a struct for that.
You can write a function that performs conversion. Then write string WORDS[] = {"hello world", int_to_string(10)};
edit: If you don't know how, see http://www.cplusplus.com/articles/numb_to_text/
Last edited on
Since the structure of all entries is the same, you can also do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct entry_t
{
    string word;
    string str_two;
    int integer;
};

const entry_t WORDS[]=
{
        {"wall", "Do you feel you're banging your head against something?", 10 },
        {"glasses", "These meight help you see the answer.", 50 },
        {"labored", "Going slowly, is it?", 50 },
        {"persistent", "Keep at it.", 100 },
        {"jumble", "It's what the game is all about.", 50 }

};

To access a specific element of this array write:
WORDS[0].integer; WORDS[0].str_two; and etc.

EDIT: D'oh! I did what filipe already said...
Last edited on
Topic archived. No new replies allowed.