Guest User

Untitled

a guest
Dec 10th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.31 KB | None | 0 0
  1. // Math test, first shot
  2.  
  3. //Start by including libraries
  4. #include <iostream>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int addquiz(int a)
  11. {
  12.  
  13.     //Create multiplier to use in first while loop
  14.     //First while loop will change the value of multiplier, which will be used in later rand() command
  15.     int multiplier = 10;
  16.     int difficulty = 0;
  17.     int timesdone = 0;
  18.     int value1;
  19.     int value2;
  20.     int guess;
  21.     int numbercorrect = 0;
  22.  
  23.     //While loop to change difficulty
  24.     while (difficulty != a)
  25.     {
  26.         difficulty++;
  27.         if (difficulty !=a) multiplier*=10;
  28.     }
  29.  
  30.     //Second while loop, this one is just used to do 10 quizzes
  31.     while (timesdone < 10)
  32.     {
  33.         timesdone++;
  34.  
  35.         //Setting values for quiz
  36.         value1 = rand() %multiplier+1;
  37.         value2 = rand() %multiplier+1;
  38.  
  39.         cout << "What is " << value1 << " + " << value2 << "?" << endl;
  40.         cin >> guess;
  41.  
  42.         if (guess == value1+value2)
  43.         {
  44.             numbercorrect++;
  45.             cout << endl;
  46.             cout << endl;
  47.             cout << "That is correct!" << endl;
  48.         }
  49.         else
  50.         {
  51.             cout << endl;
  52.             cout << endl;
  53.             cout << "That is incorrect." << endl;
  54.         }
  55.         if (timesdone < 10)
  56.         {
  57.             cout << "Next question." << endl;
  58.             cout << endl;
  59.         }
  60.     }
  61.     return numbercorrect;
  62. }
  63.  
  64. int subquiz(int b)
  65. {
  66.    
  67.     //Create multiplier to use in first while loop
  68.     //First while loop will change the value of multiplier, which will be used in later rand() command
  69.     int difficulty = 0;
  70.     int multiplier = 10;
  71.     int timesdone = 0;
  72.     int value1;
  73.     int value2;
  74.     int value3; //Value 3 is necessary only for subtraction and division
  75.     int guess;
  76.     int numbercorrect = 0;
  77.  
  78.     //While loop to change difficulty
  79.     while (difficulty != b)
  80.     {
  81.         difficulty++;
  82.         if (difficulty !=b) multiplier*=10;
  83.     }
  84.  
  85.     //Second while loop, this one is just used to do 10 quizzes
  86.     while (timesdone < 10)
  87.     {
  88.         timesdone++;
  89.  
  90.         //Setting values for quiz
  91.         value1 = rand() %multiplier+1;
  92.         value2 = rand() %multiplier+1;
  93.         value3 = value1 + value2;
  94.  
  95.         cout << "What is " << value3 << " - " << value2 << "?" << endl;
  96.         cin >> guess;
  97.  
  98.         if (guess == value3-value2)
  99.         {
  100.             numbercorrect++;
  101.             cout << endl;
  102.             cout << endl;
  103.             cout << "That is correct!" << endl;
  104.         }
  105.         else
  106.         {
  107.             cout << endl;
  108.             cout << endl;
  109.             cout << "That is incorrect." << endl;
  110.         }
  111.         if (timesdone < 10)
  112.         {
  113.             cout << "Next question." << endl;
  114.             cout << endl;
  115.         }
  116.     }
  117.     return numbercorrect;
  118. }
  119.  
  120. int multquiz(int c)
  121. {
  122.    
  123.     //Create multiplier to use in first while loop
  124.     //First while loop will change the value of multiplier, which will be used in later rand() command
  125.     int multiplier = 1;
  126.     int difficulty = 0;
  127.     int timesdone = 0;
  128.     int value1;
  129.     int value2;
  130.     int guess;
  131.     int numbercorrect = 0;
  132.  
  133.     //While loop to change difficulty
  134.     while (difficulty != c)
  135.     {
  136.         if (difficulty !=c) multiplier*=10;
  137.         difficulty++;
  138.     }
  139.  
  140.     //Second while loop, this one is just used to do 10 quizzes
  141.     while (timesdone < 10)
  142.     {
  143.         timesdone++;
  144.  
  145.         //Setting values for quiz
  146.         value1 = rand() %multiplier+1;
  147.         value2 = rand() %multiplier+1;
  148.  
  149.         cout << "What is " << value1 << " x " << value2 << "?" << endl;
  150.         cin >> guess;
  151.  
  152.         if (guess == value1*value2)
  153.         {
  154.             numbercorrect++;
  155.             cout << endl;
  156.             cout << endl;
  157.             cout << "That is correct!" << endl;
  158.         }
  159.         else
  160.         {
  161.             cout << endl;
  162.             cout << endl;
  163.             cout << "That is incorrect." << endl;
  164.         }
  165.         if (timesdone < 10)
  166.         {
  167.             cout << "Next question." << endl;
  168.             cout << endl;
  169.         }
  170.     }
  171.     return numbercorrect;
  172. }
  173.  
  174. int divquiz(int d)
  175. {
  176.    
  177.     //Create multiplier to use in first while loop
  178.     //First while loop will change the value of multiplier, which will be used in later rand() command
  179.     int multiplier = 1;
  180.     int difficulty = 0;
  181.     int timesdone = 0;
  182.     int value1;
  183.     int value2;
  184.     int value3;
  185.     int guess;
  186.     int numbercorrect = 0;
  187.  
  188.     //While loop to change difficulty
  189.     while (difficulty != d)
  190.     {
  191.         difficulty++;
  192.         if (difficulty !=d) multiplier*=10;
  193.     }
  194.  
  195.     //Second while loop, this one is just used to do 10 quizzes
  196.     while (timesdone < 10)
  197.     {
  198.         timesdone++;
  199.  
  200.         //Setting values for quiz
  201.         value1 = rand() %multiplier+1;
  202.         value2 = rand() %multiplier+1;
  203.         value3 = value1 * value2;
  204.  
  205.         cout << "What is " << value3 << " + " << value2 << "?" << endl;
  206.         cin >> guess;
  207.  
  208.         if (guess == value3/value2)
  209.         {
  210.             numbercorrect++;
  211.             cout << endl;
  212.             cout << endl;
  213.             cout << "That is correct!" << endl;
  214.         }
  215.         else
  216.         {
  217.             cout << endl;
  218.             cout << endl;
  219.             cout << "That is incorrect." << endl;
  220.         }
  221.         if (timesdone < 10)
  222.         {
  223.             cout << "Next question." << endl;
  224.             cout << endl;
  225.         }
  226.     }
  227.     return numbercorrect;
  228. }
  229.  
  230. int main()
  231. {
  232.     //Declare variables
  233.     int quizchoice = 0;
  234.     int quizdifficulty = 0;
  235.     bool properchoice=false;
  236.     int timescorrect;
  237.    
  238.  
  239.     //Seed random number command
  240.     srand(time(NULL));
  241.  
  242.     //Intro and ask for quiz choice
  243.     cout << "This is the math test. Created by Cockgoblin." << endl;
  244.     cout << "Which test would you like to do?" << endl;
  245.     cout << "1. Addition" << endl;
  246.     cout << "2. Subtraction" << endl;
  247.     cout << "3. Multiplication " << endl;
  248.     cout << "4. Division" << endl;
  249.    
  250.     while (properchoice==false)
  251.     {
  252.         cin >> quizchoice;
  253.    
  254.         //Check to see if answer is valid
  255.         if (quizchoice >= 1 && quizchoice <= 4) properchoice = true;
  256.  
  257.         if (properchoice==false)
  258.         {
  259.             cout << "Sorry, that was an invalid command." << endl;
  260.             cout << endl;
  261.         }
  262.     }
  263.  
  264.     //Reset properchoice boolean for quizdifficulty check
  265.     properchoice = false;
  266.  
  267.     //Ask for quiz difficulty
  268.     cout << endl;
  269.     cout << endl;
  270.     cout << "What difficulty would you like?" << endl;
  271.     cout << "1. Elementary" << endl;
  272.     cout << "2. Junior High School" << endl;
  273.     cout << "3. High School" << endl;
  274.     while (properchoice==false)
  275.     {
  276.         cin >> quizdifficulty;
  277.    
  278.         //Check to see if answer is valid
  279.         if (quizdifficulty >= 1 && quizdifficulty <= 3) properchoice = true;
  280.  
  281.         if (properchoice==false)
  282.         {
  283.             cout << "Sorry, that was an invalid command." << endl;
  284.             cout << endl;
  285.         }
  286.     }
  287.  
  288.     //Call for specific quiz function
  289.     //I know I can use an if/else for 3 and 4, don't care
  290.     if (quizchoice == 1) timescorrect = addquiz(quizdifficulty);
  291.     if (quizchoice == 2) timescorrect = subquiz(quizdifficulty);
  292.     if (quizchoice == 3) timescorrect = multquiz(quizdifficulty);
  293.     if (quizchoice == 4) timescorrect = divquiz(quizdifficulty);
  294.  
  295.     cout << "You got " << timescorrect << " out of 10 questions right. :)" << endl;
  296.  
  297.     system("PAUSE");
  298.     return 0;
  299. }
Add Comment
Please, Sign In to add comment