Friday, 22 December 2017

C++ Program For Quiz Game


#include<iostream>

#include<string>

using namespace std;

char guess;

int total;



class Quiz

{

public:

       void setValues(string, string, string, string, string, char, int);

       void askQuestion();



private:

       string Question_Text;

       string answer_1;

       string answer_2;

       string answer_3;

       string answer_4;



       char correct_answer;

       int Question_Score;

};



void Quiz::setValues(string q, string a1, string a2, string a3, string a4, char ca, int pa)

{

       Question_Text = q;

       answer_1 = a1;

       answer_2 = a2;

       answer_3 = a3;

       answer_4 = a4;

       correct_answer = ca;

       Question_Score = pa;

}

void Quiz::askQuestion()

{

       cout << Question_Text << endl;

       cout << "a. " << answer_1 << endl;

       cout << "b. " << answer_2 << endl;

       cout << "c. " << answer_3 << endl;

       cout << "d. " << answer_4 << endl;

       cout << endl;

       cout << "What is your answer?" << endl;



       cin >> guess;

       if (guess == correct_answer)

       {

              cout << endl;

              cout << "Correct!" << endl;

              total = total + Question_Score;

              cout << endl;

              cout << "Press enter to continue." << endl;



              cin.get();



              cin.ignore();

       }

       else

       {

              cout << "Sorry, you're wrong..." << endl;

              cout << "The correct answer is " << correct_answer << "." << endl;

              cout << "Press enter to continue." << endl;



              cin.get();



              cin.ignore();

       }

}



void main()

{

       cout << "Press enter to start..." << endl;

       cin.get();

       string name;

       cout << "What's your name?" << endl;

       cin >> name;

       cout << endl;



       string respond;

       cout << "Are you ready to start the quiz, " << name << "? Yes/No." << endl;

       cin >> respond;

       cout << endl;



       if (respond == "Yes")

       {

              cout << "Good luck!" << endl;

              cout << endl;

              cout << "Press enter to continue.";



              cin.get();

              cin.ignore();

              // }



              Quiz q1;

              Quiz q2;

              Quiz q3;

              Quiz q4;

              Quiz q5;



              q1.setValues("1. What command prints something to the screen?",

                     "cin",

                     "cout",

                     "char",

                     "print",

                     'b',

                     20);



              q2.setValues("2. Which of the following categories does C++ belong to?",

                     "Operating System",

                     "High-level programming language",

                     "low-level programming language",

                     "Compiler",

                     'b',

                     20);



              q3.setValues("3. Which command is correctly written?",

                     "cout >>",

                     "cin <<",

                     "cout <>",

                     "cin >>",

                     'd',

                     20);



              q4.setValues("4. What is this called, <iostream>?",

                     "directive",

                     "pre-processor directive",

                     "file",

                     "command",

                     'b',

                     20);



              q5.setValues("5. What punctuation ends most lines of code?",

                     " . ",

                     " ; ",

                     " : ",

                     " ' ",

                     'b',

                     20);



              q1.askQuestion();

              q2.askQuestion();

              q3.askQuestion();

              q4.askQuestion();

              q5.askQuestion();



              cout << "Your Total Score is " << total << " out of 100!";

              cout << endl;



              if (total > 50)

              {

                     cout << "You win...Happy:" << endl;

                     cin.ignore();

              }

              else

              {

                     cout << "You failed... Sorry, better luck next time.";

                     cout << endl;

                     system("pause");

              }

       }
}

0 comments:

Post a Comment

Tech UOG