C++ How to get 2D array/vectors stored

Well I am trying to develop a simple text game, and I am trying to figure out how to get the text file into 2D vectors or arrays.

[Textfile.txt]
Username
Password
Class Type
Skill_1 exp_1
Skill_2 exp_2
Skill_3 exp_3
Item 1
Item 2
Item 3

Like my skills and experience are in the same line, however when I store in an array/vector it would become:

myArray[3] = {"Skill_1 exp_1", "Skill_2 exp_2", "Skill_3 exp_3"};

However I want it to be stored in 2D array/vector:
myArray[2][3] = {{"Skill_1", "Skill_2", "Skill_3"}, {"exp_1", "exp_2", "exp_3"}};

Off-topic, I am using inheritance but a lot of articles are recommending users to use composition to avoid the "Diamond Problem".
You haven't shown us the code for your I/O, so I can only guess that you're using getline to read the entire line at one time.

You probably want something like this:
1
2
3
4
5
6
7
8
 
  ifstream  ifs ("textfile.txt");
  string myArray[2][3];

  for (int i=0; i<3; i++)
  { ifs >> myArray[0][i];  // Read skill
     ifs >> myArray[1][i];  // read exp
  }


I am using inheritance but a lot of articles are recommending users to use composition to avoid the "Diamond Problem".

Again, without seeing your code, it's impossible to say if your inheritance structure is appropriate. Use the "is a" and "has a" rules and you should be okay. If the "is a" rule applies, then inheritance is appropriate. If the "has a" rule applies, then composition is indicated.


Last edited on
Here is my respiratory which includes all my source code, it is a mess at the moment, since I have been doing a lot of debugging and testing new stuffs.

https://github.com/hahajoker181/RandomGame-2014
Last edited on
Topic archived. No new replies allowed.