Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <cstdio>
- #include <ctime>
- #include <iomanip>
- using namespace std;
- void quiz(int level, int selected_operation) {
- srand( time(NULL) );
- int a, b;
- int answer;
- int operation = selected_operation;
- while ( answer != -1 ) {
- if (selected_operation == 5)
- operation = rand() % 4 + 1;
- a = rand() % 100 + 1;
- b = rand() % 100 + 1;
- if (level == 1) {
- a /= 10;
- b /= 10;
- }
- cout << "Type your answer (-1 to quit): " << endl;
- cout << setw(4) << a << endl;
- int correct_answer;
- switch (operation) {
- case 1:
- cout << "+";
- correct_answer = a + b;
- break;
- case 2:
- cout << "-";
- correct_answer = a - b;
- break;
- case 3:
- cout << "x";
- correct_answer = a * b;
- break;
- case 4:
- cout << "/";
- correct_answer = a / b;
- break;
- }
- cout << setw(3) << b << endl;
- cout << "----" << endl;
- cin >> answer;
- while (answer != correct_answer && answer != -1) {
- cout << "Try again." << endl;
- cin >> answer;
- }
- if (answer == correct_answer)
- cout << "Correct answer!\n" << endl;
- }
- cout << endl;
- }
- int main()
- {
- int level, operation;
- do {
- cout << "Enter your graduation level:\n1. Beginner\n2. Advanced" << endl;
- cin >> level;
- switch (level) {
- case 1:
- case 2:
- cout << "Enter the exercises you would like to take:\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Random" << endl;
- cin >> operation;
- switch (operation) {
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- quiz(level, operation);
- break;
- default:
- if ( cin.get() != EOF )
- cout << "Invalid choice." << endl;
- }
- break;
- default:
- if ( cin.get() != EOF )
- cout << "Invalid choice." << endl;
- }
- } while ( cin.get() != EOF );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement