0

Hello I'm working on a school project and I'm trying to read from a file with these contents:

the idx of question, the corresponding concept 
1 Arrays Hold Multiple Values
1 Pointer Variables
2 Arrays as Function Arguments
3 Comparing Pointers
4 Pointer Variables
5 Pointer Variables
6 Initializing Pointers
7 Pointers as Function Parameters
8 Arrays as Function Arguments
8 Pointer Variables
9 Arrays Hold Multiple Values
10 Pointer Variables

The goal is to store the numbers on the beginning of the string into a vector container to hold its values along with the rest of the string as the corresponding concepts.

Note: Each number represents a question in a quiz and it may include more than one concept, which is separated in a different line in the file. So mapping the two contents into a string with the same number 1 for example is totally fine.

I tried 2d vector and 1d vector but my problem is I don't know how I can split the string and detect the integers in the beginning also with two or three digits integers the problem will be more complex.

Please any help will be much appreciated!!!!

Here what I tried:

void MapQC::setMapQC()
{
    string line;
    string idxString;
    fstream inFile;

    int numOfLines = 0;
    int idx;

    inFile.open("database/map-question-concept.txt", ios::in);

    if (inFile.is_open())
    {
        while (!inFile.eof())
        {
            getline(inFile, line);

            vector<string> row;

            for (int i = 0; i <= numOfLines; i++)
            {
                if (getline(inFile, line, ' '))
                {
                  // cout << line << endl;
                  row.push_back(line);
                }
            }
            mapQCVec.push_back(row);
            numOfLines ++;
            cout <<"row at: " << row[1] << endl;
        }

    }

    inFile.close();
}
  • 1
    I recommend using `std::stringstream` after you have read the text line from the file. – Thomas Matthews Feb 18 '22 at 17:45
  • Future bug: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Feb 18 '22 at 17:49

1 Answers1

0

This is not the cleanest solution, but it should get you started. The following separates the numerals at the beginning of each line from other content:

// for each line string
string idxStr;
string lineWithoutIdx;
for (const char& c : line) 
{
    if (isdigit(c)) { idxStr.push_back(c); }
    else { lineWithoutIdx.push_back(c); }
}
const int idx = atoi(idxStr.c_str()); // numbers-string to int
// use containers of your choice, e.g. std::vector
lineIndices.push_back(idx);
lineContents.push_back(lineWithoutIdx);

Each line will have its own entry in each container, then you can filter/merge duplicates, or access a specific string in lineContents by a number in lineIndices. Note that this does NOT check if numbers are within the text, but assumes they are at the beginning of each line, as per your example.

theRPGmaster
  • 107
  • 1
  • 6