Advertisement
7889

Untitled

Jun 9th, 2023
57
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. // C++ program for the above approach
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int Guess;
  7. int Total;
  8.  
  9. // Question Class
  10. class Question {
  11. private:
  12.     string Question_Text;
  13.     string Answer_1;
  14.     string Answer_2;
  15.     string Answer_3;
  16.     string Answer_4;
  17.     int Correct_Answer;
  18.     int Question_Score;
  19.  
  20. public:
  21.     // Setter Function
  22.     void setValues(string, string,
  23.                 string, string,
  24.                 string, int, int);
  25.  
  26.     // Function to ask questions
  27.     void askQuestion();
  28. };
  29.  
  30. // Driver code
  31. int main()
  32. {
  33.     cout << "\n\n\t\t\t\tTHE DAILY QUIZ"
  34.         << endl;
  35.     cout << "\nPress Enter to start "
  36.         << "the quiz... " << endl;
  37.  
  38.     // Input
  39.     cin.get();
  40.  
  41.     string Name;
  42.     int Age;
  43.  
  44.     // Input the details
  45.     cout << "What is your name?"
  46.         << endl;
  47.     cin >> Name;
  48.     cout << endl;
  49.  
  50.     cout << "How old are you?"
  51.         << endl;
  52.     cin >> Age;
  53.     cout << endl;
  54.  
  55.     string Respond;
  56.     cout << "Are you ready to take"
  57.         << " the quiz " << Name
  58.         << "? yes/no" << endl;
  59.     cin >> Respond;
  60.  
  61.     if (Respond == "yes") {
  62.         cout << endl;
  63.         cout << "Good Luck!" << endl;
  64.     }
  65.     else {
  66.         cout << "Okay Good Bye!" << endl;
  67.         return 0;
  68.     }
  69.  
  70.     // Objects of Question Class
  71.     Question q1;
  72.     Question q2;
  73.     Question q3;
  74.     Question q4;
  75.     Question q5;
  76.     Question q6;
  77.     Question q7;
  78.     Question q8;
  79.     Question q9;
  80.     Question q10;
  81.  
  82.     // 3 is the position of
  83.     // correct answer
  84.     q1.setValues("Question : guess the answer  ", "4",
  85.                 "9", "3",
  86.                 "15", 4, 10);
  87.     q2.setValues("Question :  guess the answer  : ", "8",
  88.                 "9", "1",
  89.                 "6", 1, 10);
  90.     q3.setValues("Question :  guess the answer ", "99",
  91.                 "4", "3",
  92.                 "255", 4, 10);
  93.     q4.setValues("Question :  guess the answer ", "2",
  94.                 "55", "22",
  95.                 "1", 2, 10);
  96.     q5.setValues("Question :  guess the answer ", "3",
  97.                 "9", "11",
  98.                 "6", 3, 10);
  99.     q6.setValues("Question :  guess the answer ", "9",
  100.                 "2", "3",
  101.                 "94", 9, 10);
  102.     q7.setValues("Question :  guess the answer  ", "15",
  103.                 "900", "3",
  104.                 "4", 15, 10);
  105.     q8.setValues("Question :  guess the answer ", "7",
  106.                 "1900", "13",
  107.                 "Answer 4", 1900, 10);
  108.     q9.setValues("Question :  guess the answer ", "9",
  109.                 "700", "Answer 210",
  110.                 "800", 800, 10);
  111.     q10.setValues("Question :  guess the answer ", "8",
  112.                 "907", "302",
  113.                 "500", 907, 10);
  114.  
  115.     q1.askQuestion();
  116.     q2.askQuestion();
  117.     q3.askQuestion();
  118.     q4.askQuestion();
  119.     q5.askQuestion();
  120.     q6.askQuestion();
  121.     q7.askQuestion();
  122.     q8.askQuestion();
  123.     q9.askQuestion();
  124.     q10.askQuestion();
  125.  
  126.     // Display the total score
  127.     cout << "Total Score = " << Total
  128.         << "out of 100" << endl;
  129.  
  130.     // Display the results
  131.  
  132.     // If the player pass the quiz
  133.     if (Total >= 70) {
  134.         cout << "Congrats you passed the"
  135.             << " quiz!" << endl;
  136.     }
  137.  
  138.     // Otherwise
  139.     else {
  140.         cout << "Alas! You failed the quiz."
  141.             << endl;
  142.         cout << "Better luck next time."
  143.             << endl;
  144.     }
  145.     return 0;
  146. }
  147.  
  148. // Function to set the values of
  149. // the questions
  150. void Question::setValues(
  151.     string q, string a1,
  152.     string a2, string a3,
  153.     string a4, int ca, int pa)
  154. {
  155.     Question_Text = q;
  156.     Answer_1 = a1;
  157.     Answer_2 = a2;
  158.     Answer_3 = a3;
  159.     Answer_4 = a4;
  160.     Correct_Answer = ca;
  161.     Question_Score = pa;
  162. }
  163.  
  164. // Function to ask questions
  165. void Question::askQuestion()
  166. {
  167.     cout << endl;
  168.  
  169.     // Print the questions
  170.     cout << Question_Text << endl;
  171.     cout << "1. " << Answer_1 << endl;
  172.     cout << "2. " << Answer_2 << endl;
  173.     cout << "3. " << Answer_3 << endl;
  174.     cout << "4. " << Answer_4 << endl;
  175.     cout << endl;
  176.  
  177.     // Display the answer
  178.     cout << "What is your answer?(in number)"
  179.         << endl;
  180.     cin >> Guess;
  181.  
  182.     // If the answer is correct
  183.     if (Guess == Correct_Answer) {
  184.         cout << endl;
  185.         cout << "Correct !" << endl;
  186.  
  187.         // Update the correct score
  188.         Total = Total + Question_Score;
  189.         cout << "Score = " << Question_Score
  190.             << " out of "
  191.             << Question_Score
  192.             << "!" << endl;
  193.         cout << endl;
  194.     }
  195.  
  196.     // Otherwise
  197.     else {
  198.         cout << endl;
  199.         cout << "Correct answer !" << endl;
  200.         cout << "Score = 9"
  201.             << " out of "
  202.             << Question_Score
  203.             << "!" << endl;
  204.         cout << "Correct answer = "
  205.             << Correct_Answer
  206.             << "." << endl;
  207.         cout << endl;
  208.     }
  209. }
Advertisement
Comments
  • 7889
    2 years
    # text 0.08 KB | 0 0
    1. Hi this is a game run it in https://www.programiz.com/cpp-programming/online-compiler/
Add Comment
Please, Sign In to add comment
Advertisement