0

Is this the right way to read a line from a file, split it into 2 variables(a text and an answer), and then feed those two variables into a vector of a class I created?

example being if i had a text file with a list of questions and answers, would this be how I read each question and answer being on one line together "12 How many inches in a foot?"

Question_Bank::Question_Bank(){};

Question_Bank::Question_Bank(std::string fileName){
    std::cout<<"Please enter the name of the file containing your questions: ";
    cin>>fileName;
    questionsFile.open(fileName);
    while (questionsFile.fail())
            {
            cout<<"Unable to open the file."<<endl;
            std::cout<<"Please enter the name of the file containing your questions: ";
            cin>>fileName;
            questionsFile.open(fileName);
            }
}

void LoadQuestions(){
    std::string blank;
    std::string text;
    std::string answer;
    std::string line;

    while (std::getline (questionsFile, line ) {
    std::stringstream ss(line);
    ss>>answer>>blank;
    getline(ss, text);
    questions.Question(text, answer).push_back();
    if (questionsFile.eof()){
            break;
            }
    }
questionsFile.close();
}

Question GetNextQuestion(){
    return questions;
}

The Question class

Question::Question(){};

Question::Question(std::string text, std::string answer){
    this->text = text;
    this->answer = answer;
}

std::string Question::GetText(){
    return text;
}

bool Question::AnswerContainsDigit(char digit){
    if (isdigit digit){
    return true
    }
    else return false;
}

std::string Question::GetAnswerWithPlaceholder(std::vector<char> answH){
string tempAnswer = "___"; //3 underscores
    for (int x=0; x < answH.size(); x++){
            for (int y = 0; y < tempAnswer.size(); y++){
                    if (answer.at(y)== answH.at(x){
                            tempAnswer.at(y) = answH.at(x)
                    }
            }
    }
return tempAnswer;
}

bool Question::AllDigitsGuessed(std::string userGuess){
    if (userGuess == answer)
    return true;
    else return false;
}

Examples of the questions and answers are as follows:

206 How many bones are in the average adult human?
2000 How many yards are in a nautical mile?
  • Avoid using `eof` checks. – unalignedmemoryaccess Feb 04 '19 at 07:44
  • 2
    Welcome aboard. On this site, we 1) post code, 2) say what it is supposed to do 3) say what it going wrong. I think that your question is probably better suited to https://codereview.stackexchange.com/ – Mawg says reinstate Monica Feb 04 '19 at 07:45
  • `ss>>answer>>blank;` is wrong in your example `blank` will end up as `"How"`. You need to find a different way to remove any leading blanks. `questions.Question(text, answer).push_back();` looks wrong, you haven't given enough information but I would guess `questions.push_back(Question(text, answer));` And drop the end of file test. – john Feb 04 '19 at 07:48
  • Does it work? If not what exactly is going wrong? What does the file contain? Can you post a few lines? Are all the answers numbers? Are any of the answers more than one word? – Galik Feb 04 '19 at 07:49
  • There isn't really anything wrong with the first approach. You could handle multiple spaces between the number and question with `ss.ignore(numeric_limits::max(), ' ');` instead of reading `blank`. – David C. Rankin Feb 04 '19 at 07:56
  • Do all the questions have answers that are a single number? – Galik Feb 04 '19 at 07:56
  • Possible duplicate of [C++ writing an object to a file then later reading it in?](https://stackoverflow.com/questions/7742361/c-writing-an-object-to-a-file-then-later-reading-it-in) – moooeeeep Feb 04 '19 at 08:02
  • ok, i updated my original post with quite a bit more info. Thank you all for your help! –  Feb 04 '19 at 08:04

1 Answers1

0

I guess instead of

questions.Question(text, answer).push_back();

you can use

questions.push_back(Question(text, answer));

Mayur
  • 2,583
  • 16
  • 28
  • So each of those two statements are valid? is either one "preferred" or more acceptable? –  Feb 04 '19 at 07:59
  • @JohnnyMitchell How is questions declared? Most likly only one of the options will work. –  Feb 04 '19 at 08:46