0% found this document useful (0 votes)
14 views1 page

C++ Game Code

This C++ code implements a simple 'Guess the Number' game where the computer selects a random number and the player attempts to guess it. The player is informed if their guess is too high or too low, and the game continues until the correct number is guessed. The program also counts and displays the number of attempts taken by the player to guess the number correctly.

Uploaded by

mohammadshath912
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

C++ Game Code

This C++ code implements a simple 'Guess the Number' game where the computer selects a random number and the player attempts to guess it. The player is informed if their guess is too high or too low, and the game continues until the correct number is guessed. The program also counts and displays the number of attempts taken by the player to guess the number correctly.

Uploaded by

mohammadshath912
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

C++ GAME CODE

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
// Seed the random number generator
srand(time(0));

// Generate a random number between 0 and a very large number (e.g., INT_MAX)
long long secretNumber = rand(); // You can use rand() or a more random number
approach
long long playerGuess = -1; // Start with an invalid guess
int numberOfTries = 0;

cout << "Welcome to the Guess the Number Game!" << endl;
cout << "I have selected a random number." << endl;
cout << "Can you guess what it is?" << endl;

// Loop until the player guesses correctly


while (playerGuess != secretNumber) {
cout << "Enter your guess: ";
cin >> playerGuess;
numberOfTries++;

if (playerGuess > secretNumber) {


cout << "Too high! Try again." << endl;
} else if (playerGuess < secretNumber) {
cout << "Too low! Try again." << endl;
} else {
cout << "Congratulations! You've guessed the number in " <<
numberOfTries << " tries!" << endl;
}
}

return 0;
}

You might also like