Imre_talpa
Imre_talpa

Reputation: 121

Transform this function to read the entire file

I made a quiz program in c++. It's working but with separate blocks of instructions for every question in file. I need to transform the block of instructions(after for) to work for all questions.

The file look like this

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

This is only a function in my program.

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

Upvotes: 0

Views: 57

Answers (2)

mahbubcseju
mahbubcseju

Reputation: 2290

You can use a while loop for reading the file upto end of line. As every block contains exactly five lines, you can take input for each line until you get the line size greater than 0. As blank line will also be in the input and you need to ignore them.

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

Upvotes: 1

Alex Hodges
Alex Hodges

Reputation: 607

Here is one was of doing it, making use of objects and vectors:

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
    }
}

I've had to change your questions.txt format a little also, its described below:

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
  1. The first line is your total number of questions.
  2. Blank line.
  3. Question line
  4. Answer A
  5. Answer B
  6. Answer C
  7. Answer D
  8. Correct answer
  9. Blank line
  10. Repeat numbers 3-9

Hope this helps

Upvotes: 0

Related Questions