Advertisement
michelksu

Untitled

Jul 8th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <ctime>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. void quiz(int level, int selected_operation) {
  10.     srand( time(NULL) );
  11.     int a, b;
  12.     int answer;
  13.     int operation = selected_operation;
  14.  
  15.     while ( answer != -1 ) {
  16.         if (selected_operation == 5)
  17.             operation = rand() % 4 + 1;
  18.         a = rand() % 100 + 1;
  19.         b = rand() % 100 + 1;
  20.         if (level == 1) {
  21.             a /= 10;
  22.             b /= 10;
  23.         }
  24.         cout << "Type your answer (-1 to quit): " << endl;
  25.         cout << setw(4) << a << endl;
  26.         int correct_answer;
  27.         switch (operation) {
  28.         case 1:
  29.             cout << "+";
  30.             correct_answer = a + b;
  31.             break;
  32.         case 2:
  33.             cout << "-";
  34.             correct_answer = a - b;
  35.             break;
  36.         case 3:
  37.             cout << "x";
  38.             correct_answer = a * b;
  39.             break;
  40.         case 4:
  41.             cout << "/";
  42.             correct_answer = a / b;
  43.             break;
  44.         }
  45.         cout << setw(3) << b << endl;
  46.         cout << "----" << endl;
  47.         cin >> answer;
  48.         while (answer != correct_answer && answer != -1) {
  49.             cout << "Try again." << endl;
  50.             cin >> answer;
  51.         }
  52.         if (answer == correct_answer)
  53.             cout << "Correct answer!\n" << endl;
  54.     }
  55.     cout << endl;
  56. }
  57.  
  58. int main()
  59. {
  60.     int level, operation;
  61.     do {
  62.         cout << "Enter your graduation level:\n1. Beginner\n2. Advanced" << endl;
  63.         cin >> level;
  64.         switch (level) {
  65.         case 1:
  66.         case 2:
  67.             cout << "Enter the exercises you would like to take:\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Random" << endl;
  68.             cin >> operation;
  69.             switch (operation) {
  70.             case 1:
  71.             case 2:
  72.             case 3:
  73.             case 4:
  74.             case 5:
  75.                 quiz(level, operation);
  76.                 break;
  77.             default:
  78.                 if ( cin.get() != EOF )
  79.                     cout << "Invalid choice." << endl;
  80.             }
  81.             break;
  82.         default:
  83.             if ( cin.get() != EOF )
  84.                 cout << "Invalid choice." << endl;
  85.         }
  86.     } while ( cin.get() != EOF );
  87.  
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement