这是读取文件输入的正确方法吗?
Is this the right way to read a file input?
这是从文件中读取一行,将其拆分为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?
我猜而不是
questions.Question(text, answer).push_back();
您可以使用
questions.push_back(Question(text, answer));
相关文章:
- 一种在C++中读取TXT配置文件的简单方法
- 读取大文件(>2GB)(文本文件包含以太网数据)并通过不同参数随机访问数据的最佳方法是什么?
- C++11 原子<>:只能使用提供的方法读取/写入?
- 将多个效果与 libSox 链接并读取输出数据的正确方法
- 读取二进制文件的惯用C++17标准方法是什么
- 以C++读取文件并处理可能的错误的便携式方法
- 从包含 IP 标头片段的二进制文件中读取结构的最佳方法是什么
- 从 txt 文件中读取数据的最简单方法
- 组织从内存读取的数据的最佳方法(用于调试)c++
- 将大文件读取到字符数组的正确方法
- 在方法中引用我正在读取的流
- exc_bad_access(代码= 1,地址= 0x68)在第二个读取方法中
- 是否可以覆盖 std::istream 读取方法?
- C++ 如何使用相同的类和方法来读取/写入可变大小的数据
- 类方法读取同一变量C++的不同值
- 从 mfc 应用程序点击网络服务,并希望从网络服务登录方法读取用户代码返回
- 使用fstream方法读取二进制数据
- 最好的方法读取行和col的txt文件
- 用Settimer方法读取高速数据的问题
- 在openCV和c++中以给定的命名方法读取多个图像