fork download
  1. #include"dtdio.h"
  2. #include "conio.h
  3. #include "math.h
  4.  
  5. using namespace std;
  6.  
  7. const int MAX_ATTEMPTS = 8;
  8.  
  9. void playGame() {
  10. srand(time(0));
  11. int target = rand() % 100;
  12. int guess, attempts = 0;
  13.  
  14. cout << "Number Guessing Game (0 - 99)\n";
  15.  
  16. while (attempts < MAX_ATTEMPTS) {
  17. attempts++;
  18. cout << "Enter Number (0 - 99) #" << attempts << " : ";
  19. cin >> guess;
  20.  
  21. if (guess < 0 || guess > 99) {
  22. cout << "Please enter a number between 0 and 99.\n";
  23. attempts--;
  24. continue;
  25. }
  26.  
  27. if (guess == target) {
  28. cout << "KengJungJung\n";
  29. break;
  30. } else if (guess < target) {
  31. cout << "Up\n";
  32. } else {
  33. cout << "Down\n";
  34. }
  35.  
  36. cout << "Attempts left: " << MAX_ATTEMPTS - attempts << endl;
  37. }
  38.  
  39. if (guess != target) {
  40. cout << "Game Over\n";
  41. cout << "The answer is " << target << "\n";
  42. }
  43. }
  44.  
  45. int main() {
  46. char playAgain;
  47.  
  48. do {
  49. playGame();
  50.  
  51. cout << "Do you want to play again? (y/n): ";
  52. cin >> playAgain;
  53.  
  54. } while (playAgain == 'y' || playAgain == 'Y');
  55.  
  56. cout << "Thank you for playing! Goodbye!\n";
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0.03s 25948KB
stdin
Standard input is empty
stdout
#include"dtdio.h"
#include "conio.h
#include "math.h

using namespace std;

const int MAX_ATTEMPTS = 8;

void playGame() {
    srand(time(0));
    int target = rand() % 100;
    int guess, attempts = 0;

    cout << "Number Guessing Game (0 - 99)\n";

    while (attempts < MAX_ATTEMPTS) {
        attempts++;
        cout << "Enter Number (0 - 99) #" << attempts << " : ";
        cin >> guess;

        if (guess < 0 || guess > 99) {
            cout << "Please enter a number between 0 and 99.\n";
            attempts--; 
            continue;
        }

        if (guess == target) {
            cout << "KengJungJung\n";
            break;
        } else if (guess < target) {
            cout << "Up\n";
        } else {
            cout << "Down\n";
        }

        cout << "Attempts left: " << MAX_ATTEMPTS - attempts << endl;
    }

    if (guess != target) {
        cout << "Game Over\n";
        cout << "The answer is " << target << "\n";
    }
}

int main() {
    char playAgain;

    do {
        playGame();

        cout << "Do you want to play again? (y/n): ";
        cin >> playAgain;

    } while (playAgain == 'y' || playAgain == 'Y');

    cout << "Thank you for playing! Goodbye!\n";
    return 0;
}