How do I assign parts of a text file to indexs of an array?

时间:2018-03-25 20:10:45

标签: c++ arrays file random

I have a question text file and answer text file, both have 100 lines. I'm very new to c++. I'm trying to assign an array index to each line so I generate random lines of the text file to display. I've tried researching this but was unable to figure it out. Any help would be appreciated. Thank you.

`srand(time(0));
int questionNumber = rand() % 100;

string line;
ifstream qArray ("Q_Array.txt");
if (aArray.is_open())
{
   while ( getline(qArray,questionNumber))
   {
      cout << questionNumber;
   }
   qArray.close();
}

Here is my attempt so far.`

1 个答案:

答案 0 :(得分:0)

I’m not sure for what purpose you need randomization here. You just need to create array of strings which in c++ is a two dimensional array of “chars”. As you know that you have two files with 100 strings in each, so you need to create two arrays with size 100x255 (for example, 254 symbols in line plus new line symbol). After creation you should assign each string to corresponding row in array.