【问题标题】:Transform this function to read the entire file转换此函数以读取整个文件
【发布时间】:2019-11-04 10:56:48
【问题描述】:

我用 C++ 编写了一个测验程序。它正在工作,但文件中的每个问题都有单独的指令块。我需要转换指令块(在 for 之后)以解决所有问题。

文件如下所示

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();     
}

【问题讨论】:

  • 好的,您需要转换指令块,但是您的具体问题是什么?
  • @wally 现在每个问题都需要一个块(我有 30 个),我需要一个重复的结构来处理整个文件(所有问题);
  • 请不要在 cmets 中扩展您的问题。问题本身必须是可读的。

标签: c++ string file


【解决方案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;
}

【讨论】:

    【解决方案2】:

    这是一个使用对象和向量的方法:

    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 格式,如下所述:

    问题.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
    
    1. 第一行是您的问题总数。
    2. 空白行。
    3. 问题行
    4. 回答A
    5. 答案 B
    6. 答案 C
    7. 回答 D
    8. 正确答案
    9. 空白行
    10. 重复数字 3-9

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多