转换此函数以读取整个文件 我在C++中做了一个智力竞赛节目。它正在工作,但文件中的每个问题都有单独的指令块。我需要转换指令块(在for之后)以解决所有问题
文件看起来像这样转换此函数以读取整个文件 我在C++中做了一个智力竞赛节目。它正在工作,但文件中的每个问题都有单独的指令块。我需要转换指令块(在for之后)以解决所有问题,c++,string,file,C++,String,File,文件看起来像这样 1.WHEN COMPUTER WAS FIRST INVENTIONED? a.1822 b.1823 c.1834 d.1922 2.WHO kILLED PRESEDENT BENOGIR VUTTO? a.nawaz shrif b.pervase c.non of them d.political leder 这只是我程序中的一个函数 void Question::quiz(int &Total) { string line[200]; s
1.WHEN COMPUTER WAS FIRST INVENTIONED?
a.1822
b.1823
c.1834
d.1922
2.WHO kILLED PRESEDENT BENOGIR VUTTO?
a.nawaz shrif
b.pervase
c.non of them
d.political leder
这只是我程序中的一个函数
void Question::quiz(int &Total)
{
string line[200];
string answer;
string easy[15]={"a","c","a","a","b","c","d","c","a","b","b","c","c","c","a"};
ifstream fin("questions.txt");
if(!fin)
{
cout<<"Cannot open file\n";
exit(1);
}
cout<<"The first question is\n";
for(int contor=0;contor<5;contor++)
{
getline(fin,line[contor]);
cout<<line[contor]<<'\n';
}
cout<<"Select your answer: ";
cin >> answer;
if(answer==easy[0])
{
Total+=1;
}
cin.get();
}
无效问题::测验(整数和总数)
{
弦线[200];
字符串回答;
字符串easy[15]={“a”、“c”、“a”、“a”、“b”、“c”、“d”、“c”、“a”、“b”、“b”、“c”、“c”、“c”、“a”};
ifstream fin(“questions.txt”);
如果(!fin)
{
cout您可以使用while循环来读取文件,直到行尾。因为每个块正好包含五行,所以您可以为每行进行输入,直到行大小大于0。因为输入中也会有空行,您需要忽略它们
void Question::quiz(int &Total)
{
string line[200];
string answer;
string easy[15]= {"a","c","a","a","b","c","d","c","a","b","b","c","c","c","a"};
ifstream fin("questions.txt");
if(!fin)
{
cout<<"Cannot open file\n";
exit(1);
}
int cnt=0;
while(getline(fin,line[0]))
{
cout<<line[0]<<endl;
while(line[0].size()==0)
{
getline(fin,line[0]);
cout<<line[0]<<endl;
}
for(int contor=1; contor<5; contor++)
{
do
{
getline(fin,line[contor]);
}
while(line[contor].size()==0);
cout<<line[contor]<<'\n';
}
cout<<"Select your answer: ";
cin >> answer;
if(answer==easy[cnt++])total++;
line[0]="";
}
cout<<total<<endl;
}
无效问题::测验(整数和总数)
{
弦线[200];
字符串回答;
字符串easy[15]={“a”、“c”、“a”、“a”、“b”、“c”、“d”、“c”、“a”、“b”、“b”、“c”、“c”、“c”、“a”};
ifstream fin(“questions.txt”);
如果(!fin)
{
cout这是一个利用对象和向量来实现的方法:
struct quiz_question
{
auto print_question() -> void
{
char prefix[] = { 'a', 'b', 'c', 'd' };
std::cout << "Question " << question_number << ": " << question << '\n';
for (auto x = 0; x < possible_answers.size(); x++)
{
std::cout << prefix[x] << ". " << possible_answers[x] << '\n';
}
}
auto check_answer(char user_answer) -> bool { return user_answer == answer; }
std::size_t question_number;
std::string question;
std::array< std::string, 4 > possible_answers;
char answer;
};
int main()
{
std::vector< quiz_question > questions;
std::string number_of_questions_str;
std::size_t number_of_questions;
std::ifstream ifs("questions.txt");
if (!ifs)
{
std::cout << "Cannot open questions.txt!\n";
exit(1);
}
// Start loading in questions.
// Read first line, describes how many questions there are.
std::getline(ifs, number_of_questions_str);
ifs.ignore(10000, '\n'); // Ignore a line
try
{
number_of_questions = std::stoul(number_of_questions_str);
}
catch (std::invalid_argument &ec)
{
std::cout << "Unable to parse questions.txt!\n";
exit(1);
}
// Load each question in.
for (auto x = 0; x < number_of_questions; x++)
{
quiz_question current_question;
current_question.question_number = x + 1;
// Read the question line
std::getline(ifs, current_question.question);
// Read the possible answers
for (auto &possible_answer : current_question.possible_answers)
{
std::getline(ifs, possible_answer);
}
// Read the actual answer
current_question.answer = ifs.get();
ifs.ignore(10000, '\n'); // Ignore the rest of that line
questions.push_back(std::move(current_question));
ifs.ignore(10000, '\n'); // Ignore a line
}
// Now all the questions have been loaded. Lets start the quiz!
char answer;
std::size_t score { 0 };
std::cout << "Starting the quiz!\n";
for (auto &question : questions)
{
question.print_question(); // Print question and possible answers
std::cin >> answer;
if (question.check_answer(answer))
{
std::cout << "Correct!\n\n";
score++;
}
else
{
std::cout << "Incorrect!\n\n";
}
std::cin.clear(); // Clear flags
std::cin.ignore(10000, '\n'); // Skip to next line
}
}
第一行是你的问题总数
空行
问句
回答A
答复B
答复C
答复D
正确答案
空行
重复数字3-9
希望这有帮助好的,你需要转换指令块,但你的具体问题是什么?@wally现在我需要为每个问题设置一个块(我有30个),我需要一个重复的结构来处理整个文件(所有问题);请不要在注释中扩展你的问题。问题本身必须可读。
2
WHEN COMPUTER WAS FIRST INVENTIONED?
1822
1823
1834
1922
a
WHO KILLED PRESEDENT BENOGIR VUTTO?
nawaz shrif
pervase
non of them
political leder
c