【发布时间】:2019-06-14 17:56:09
【问题描述】:
我正在尝试使用 c++ 制作一个测验游戏,为此我想将我的所有问题 (MCQ) 及其答案逐行存储在一个文本文件中。
格式如下 “这是什么?a)x b)y c)z d)p”'a'
现在我想从文件中读取并将其存储在我的问答游戏中。这是一个字符串变量中的问题和 char 变量中的答案。
然后我想检查用户是否输入了正确的答案。
#include <iostream>
#include <fstream>
using namespace std;
int NoOfQuestions = 2;
int counter = 0;
int main(){
ifstream file("c++.txt");
string question;
char a;
while(counter<NoOfQuestions){
getline(file,question);
cout<<question<<endl;
counter++;
}
}
【问题讨论】:
-
std::string::substr?您可以将字符串存储在std::stringstream 中,并使用带有分隔符的std::geline
标签: c++ file-handling