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?