Thread: How to Input questions Into This C++ Quiz Maker?

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    3

    Question How to Input questions Into This C++ Quiz Maker?

    I'm working on a quiz project in C++ at the moment, and I'm confused as to how to make some of it work. I have a class (quiz) which sets up the rules for how the quiz works. It looks like this:

    Code:
    class quiz
    {
    private:
        //how many questions
        int num_q;
        char answer[50];
        string question[50];
    
        int pom;
        string plus;
        string minus;
    
    public:
        quiz()
        {
            pom =0;
        }
        void set_pom_answers(string pluss, string minuss)
        {
            plus = pluss;
            minus = minuss;
        }
        void set_num_q(int num_qq)
        {
            num_q = num_qq;
        }
        //Question_Number = question number, ca = correct answer, and q = question
        void set_q_a(intQuestion_Number, string q,char ca)
        {
            question[Question_Number-1]= q;
            answer[Question_Number-1]= ca;
        }
        void run_quiz()
        {
            char ans;
            for(int i =0; i < num_q; i++)
            {
                cout << question[i]<<": ";
                cin >> ans;
                if(ans == answer[i])
                {
                    cout <<"Correct! good for you!"<< endl;
                    pom = pom +1;
                }
                else
                {
                    cout <<"Wrong. :("<< endl;
                    pom = pom;
    
                }
                system("pause");
                system("cls");
            }
            if(pom >=(num_q /2))
            {
                cout << plus << endl;
                cout <<"your score was "<< pom << endl;
            }
            elseif(pom <(num_q /2))
            {
                cout << minus << endl;
                cout <<"your score was "<< pom << endl;
            }
            else
            {
                cout <<"input not recognised"<< endl;
            }
            system("pause");
        }
    
    };
    
    


    To run a quiz using this, you use a format like this in Main():

    Code:
            quiz Politics;
            Politics.set_num_q(10);
            Politics.set_pom_answers("You're good at Politics.","Revise more!");
            Politics.set_q_a(1,"Michael Fallon is the current foreign secretary: [t]rue or [f]alse",'f');
            Politics.set_q_a(2,"Tony Blair was PM from 1996-2007: [t]rue or [f]alse",'f');
            Politics.set_q_a(3,"Michael Gove is the current education secretary : [t]rue or [f]alse",'f');
            Politics.set_q_a(4,"Teresa May is the current home secretary: [t]rue or [f]alse",'t');
            Politics.set_q_a(5,"Owen Jones is a blairite: [t]rue or [f]alse",'f');
            Politics.set_q_a(6,"Nick Robinson is the BBC's political editor: [t]rue or [f]alse",'f');
            Politics.set_q_a(7,"David Cameron is a eurosceptic: [t]rue or [f]alse",'t');
            Politics.set_q_a(8,"Matthew Handcock is a Tory MP: [t]rue or [f]alse",'t');
            Politics.set_q_a(9,"Jonathon Woodcock is a Labour MP: [t]rue or [f]alse",'t');
            Politics.set_q_a(10,"David McDonell is the current shadow chancellor: [t]rue or [f]alse",'f');
    
    


    The above works absolutely fine when I run it using Politics.run_quiz().

    The issue is that I want to create a menu that allows me to make or take the quizzes, and I am confused as to how I code the maker part. In the end I want to save this information to a file, and when the user selects 'take' in the menu, they will load this file and the quiz will be run from it. So far, I have this just as an example to illustrate what I want:

    Code:
    int main()
        {
    
            intChoice;
    
            do{
                cout <<"Would you like to:\n\n"<<"1)Make a quiz?\n"<<"2) Take a quiz?\n"<<"3) Quit?\n\n";
                cin >>Choice;
            }while(Choice>3);       
    
            if(Choice==1){
                string Quiz_Name;
                cout <<"What is the name of this quiz?\n";
                cin >>Quiz_Name;
    
                intNum_of_Qs;
                cout <<"How many Questions should there be?\n";
                cin >>Num_of_Qs;
    
                string Answer_Plus;
                cout <<"What message should be displayed if the score is above 50%?\n";
                cin >>Answer_Plus;
    
                string Answer_Minus;
                cout <<"What message should be displayed if the score is less that 50%?\n";
                cin >>Answer_Minus;
    
                /*Here I would like to add all the elements together, along with the questions and answers. for example:
    
                quiz Quiz_Name;
                Quiz_Name.set_num_q(Num_of_Qs);
                Quiz_Name.set_pom_answers(Answer_Plus, Answer_Minus);
    
                for (int j = 1; j <= Num_of_Qs; j++) {
                Quiz_Name.set_q_a(j, [The user inputs a question], [the user inputs an answer])
                }
    
                or something along those lines? How would I make something like this work?
    
                */
    
    
            }
    
            if(Choice==2){
                Politics.run_quiz();
            }
    
            if(Choice==3){
                return0;
            }
    
        }
    
    


    I have no idea whether this is the best way to go about it or not? Could someone please help me work out how to make a quiz using user inputs?

    Thanks!

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    File IO is almost the same as console IO for the user.
    Code:
    std::ifstream in("questions.txt");
    if (!in) //some problem with file, maybe doesn't exist.
    Now use 'in' the same way you use cin.
    Just discard the output prompts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File input/output questions
    By forpetesake568 in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2016, 10:30 AM
  2. Instant Keyboard Input Questions
    By mr_raging_money in forum C Programming
    Replies: 3
    Last Post: 03-15-2012, 11:06 AM
  3. C quiz questions
    By ayanbizz in forum C Programming
    Replies: 8
    Last Post: 01-13-2012, 12:47 PM
  4. Replies: 12
    Last Post: 10-18-2011, 09:24 PM
  5. User input y/n questions.
    By phufbq in forum C Programming
    Replies: 8
    Last Post: 10-27-2008, 09:47 AM

Tags for this Thread