fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. srand(time(0));
  9.  
  10. while (true) {
  11. int a = rand() % 10 + 1;
  12. int b = rand() % 10 + 1;
  13.  
  14. char operation;
  15.  
  16. if (rand() % 2 == 0) {
  17. operation = '+';
  18. } else {
  19. operation = '-';
  20. }
  21.  
  22. // Make sure subtraction does not give a negative answer
  23. if (operation == '-' && a < b) {
  24. swap(a, b);
  25. }
  26.  
  27. int correctAnswer;
  28.  
  29. if (operation == '+') {
  30. correctAnswer = a + b;
  31. } else {
  32. correctAnswer = a - b;
  33. }
  34.  
  35. int answer;
  36.  
  37. cout << a << " " << operation << " " << b << " = ";
  38. cin >> answer;
  39.  
  40. if (answer == correctAnswer) {
  41. cout << "Correct! Great job! 😊" << endl;
  42. cout << endl;
  43. } else {
  44. cout << "Wrong answer! Game Over. ❌" << endl;
  45. cout << "The correct answer was: " << correctAnswer << endl;
  46. break;
  47. }
  48. }
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
1 + 4 = Wrong answer! Game Over. ❌
The correct answer was: 5