Thanks to visit codestin.com
Credit goes to www.scribd.com

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

P1

This document is a C++ program for a Number Guessing Game where the user has 5 attempts to guess a randomly generated number between 1 and 100. The program provides feedback on whether the guess is too low or too high, and it reveals the correct number if the user fails to guess it within the allowed attempts. It uses standard input/output and random number generation functions from the C++ standard library.

Uploaded by

malikwasiq932
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)
7 views1 page

P1

This document is a C++ program for a Number Guessing Game where the user has 5 attempts to guess a randomly generated number between 1 and 100. The program provides feedback on whether the guess is too low or too high, and it reveals the correct number if the user fails to guess it within the allowed attempts. It uses standard input/output and random number generation functions from the C++ standard library.

Uploaded by

malikwasiq932
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

#include <iostream>

#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
// Seed the random number generator with the current time
srand(static_cast<unsigned int>(time(0)));

// Generate a random number between 1 and 100


int randomNumber = rand() % 100 + 1;
int userGuess = 0;
int attempts = 0;
const int maxAttempts = 5;

cout << "Welcome to the Number Guessing Game!" << endl;


cout << "I have generated a random number between 1 and 100." << endl;
cout << "You have " << maxAttempts << " attempts to guess the number!" << endl;

// Loop until the user guesses the number or exceeds the max attempts
while (userGuess != randomNumber && attempts < maxAttempts) {
cout << "Enter your guess: ";
cin >> userGuess;
attempts++;

if (userGuess < randomNumber) {


cout << "Too low! Try again." << endl;
} else if (userGuess > randomNumber) {
cout << "Too high! Try again." << endl;
} else {
cout << "Congratulations! You guessed the number!" << endl;
return 0; // Exit the program if guessed correctly
}
}

// If the user has used all attempts, reveal the number


cout << "Sorry! You've used all your attempts. The number was " << randomNumber
<< "." << endl;

return 0;
}

You might also like