Advertisement
sheredega

Task #12

Dec 10th, 2022
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string>
  4. using namespace std;
  5.  
  6. void playGame(int max, int min, int num) {
  7.     cout << "Try to guess the secret number: ";
  8.     int tryNum;
  9.     cin >> tryNum;
  10.     if (tryNum < num) {
  11.         cout << "The secret number is bigger!\n";
  12.         playGame(max, min, num);
  13.     }
  14.     else if (tryNum > num) {
  15.         cout << "The secret number is smaller!\n";
  16.         playGame(max, min, num);
  17.     }
  18.     else {
  19.         cout << "Congratulations! You win this game! The secret number was " << num << endl;
  20.         cout << "Do u want to play this game again? (yes/no): ";
  21.         string str;
  22.         cin >> str;
  23.         if (str == "yes") {
  24.             cout << "Okay, we chose new secret number!\n";
  25.             num = min + rand() % (max - min + 1);
  26.             playGame(max, min, num);
  27.         }
  28.         else {
  29.             cout << "Okay! Goodbye!\n";
  30.         }
  31.     }
  32. }
  33.  
  34. int main() {
  35.     srand(time(NULL));
  36.     int max;
  37.     int min;
  38.     cout << "Enter the minimum number of range: ";
  39.     cin >> min;
  40.     cout << "Enter the maximum number of range: ";
  41.     cin >> max;
  42.     int num = min + rand() % (max - min + 1);
  43.     playGame(max, min, num);
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement