Это правильный способ чтения файла ввода? - PullRequest
0 голосов
/ 04 февраля 2019

Это правильный способ прочитать строку из файла, разделить ее на 2 переменные (текст и ответ), а затем передать эти две переменные в вектор класса, который я создал?

Например, если у меня есть текстовый файл со списком вопросов и ответов, будет ли это так, как я читаю каждый вопрос и ответ в одной строке «12 Сколько дюймов в футе?»

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;
}

Класс вопросов

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;
}

Примеры вопросов и ответов:

206 How many bones are in the average adult human?
2000 How many yards are in a nautical mile?

1 Ответ

0 голосов
/ 04 февраля 2019

Я думаю, вместо

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

вы можете использовать

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...