我用C ++编写了一个测验程序。它正在工作,但是针对文件中的每个问题都有单独的说明块。我需要转换说明框(用于后)才能处理所有问题。
文件看起来像这样
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();
}
答案 0 :(得分:1)
您可以使用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;
}
答案 1 :(得分:0)
这是利用对象和向量进行的一项工作:
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
}
}
我还必须对您的questions.txt格式进行一些更改,如下所述:
questions.txt
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
希望这会有所帮助