0

I'm having a vector subscript error when trying to compile. My code is here below. The program is designed to read a text file, and retrieve the questions and answers into a vector, then choose 5 random questions out of the 10 in the text file, then display the first on the screen. Each question has 6 lines in the text file: 1 for the question, 4 for the possible answers, and 1 for the correct answer.

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <ctime>

//FUNCTION PROTOTYPES
void SetQuestions(std::vector<std::vector<std::string>> &questionsChosen);

int main()
{
    //seed random number generator
    srand(static_cast<unsigned int>(time(0))) ;

    //RETRIEVE QUESTIONS FROM FUNCTION
    std::vector<std::vector<std::string>> questionsChosen;
    SetQuestions(questionsChosen);

    //Test if stored correctly
    std::cout << questionsChosen[0][0];


    return 0;
}

void SetQuestions(std::vector<std::vector<std::string>> &questionsChosen)
{
    //RETRIEVES ALL Q&A AND PUTS INTO 2 DIMENSIONAL VECTOR - questionsAll

    std::vector<std::vector<std::string>> questionsAll;
    std::ifstream fromFile("QA.txt");
    for (; fromFile.eof();)
    {
        std::vector<std::string> question;
        for (int i = 0; i < 6; i++)
        {
            std::string line;
            std::getline(fromFile, line);
            question.push_back(line);
        }
        questionsAll.push_back(question);
    }

    fromFile.close();

    //GENERATE RANDOM NUMBERS TO CHOOSE 5 RANDOM QUESTIONS

    int c1 = rand() % 9;

    int c2 = rand() % 9;
    //prevent duplicates
    while (c2 == c1)
    {
        c2 = rand() % 9;
    }

    int c3 = rand() % 9;
    while ((c3 == c1) || (c3 == c2))
    {
        c3 = rand() % 9;
    }

    int c4 = rand() % 9;
    while ((c4 == c1) || (c4 == c2) || (c4 == c3))
    {
        c4 = rand() % 9;
    }

    int c5 = rand() % 9;
    while ((c5 == c1) || (c5 == c2) || (c5 == c3) || (c5 == c4))
    {
        c5 = rand() % 9;
    }

    //COPY CHOSEN QUESTIONS INTO NEW ARRAY


    questionsChosen.push_back(questionsAll[c1]);
    questionsChosen.push_back(questionsAll[c2]);
    questionsChosen.push_back(questionsAll[c3]);
    questionsChosen.push_back(questionsAll[c4]);
    questionsChosen.push_back(questionsAll[c5]);

}

iehrlich
  • 3,524
  • 4
  • 30
  • 42
  • `for (; fromFile.eof();)` See [this question](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) please! At least did you mean `for (; !fromFile.eof();)`?? – πάντα ῥεῖ Nov 23 '14 at 21:39
  • Please post the exact error message and highlight the line on which it occurs. – Neil Kirk Nov 23 '14 at 21:40
  • For 2-D vectors, you need a space between ">>" i.e., std::vector > – learningToCode Nov 23 '14 at 21:43
  • `int i = 0; string line; while(getline(stream, line)) { if (i == 0) { questionsAll.resize(questionsAll.size() + 1); } questionsAll.back().push_back(line); ++i; if (i == qnum) { i = 0; }} if (i != 0) { error }` – Neil Kirk Nov 23 '14 at 21:43
  • Thanks guys. It was just that missing exclamation mark before "fromFile.eof()". Now everything is working as expected! Glad it was something small like that! :) – Robert Helms Nov 23 '14 at 21:52
  • @RobertHelms _"Now everything is working as expected!"_ Read that link I gave you though. – πάντα ῥεῖ Nov 24 '14 at 17:24
  • I don't see the problem with the usage myself, as the condition is only met when the end of the file is reached anyway. Because the end of the file is reached, the loop will not reiterate after that point. – Robert Helms Nov 25 '14 at 01:21

0 Answers0