0% found this document useful (0 votes)
52 views

Game in C++ Using Random Function

The document describes a random number guessing game program written in C++. The program uses random numbers between 1-100, tracks the number of guesses, and allows the user to play again if they choose. It provides feedback on if guesses are too high, too low, or correct. The program runs until the user exhausts their 20 attempts or correctly guesses the random number.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Game in C++ Using Random Function

The document describes a random number guessing game program written in C++. The program uses random numbers between 1-100, tracks the number of guesses, and allows the user to play again if they choose. It provides feedback on if guesses are too high, too low, or correct. The program runs until the user exhausts their 20 attempts or correctly guesses the random number.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <iomanip>
#include <ctime>
using namespace std;
char chr;
int main()
{
srand(time(NULL)); //the function that
generates random numbers
int number=rand()%100+1; //the range of the
random numbers
int guess; //The guess is stor
ed here
int tries=0; //The number of tri
es stored here
char answer; //The answer to
the question is stored here
answer='y';
while(answer=='y'||answer=='Y')
{
while (tries<=20 && answer=='y'|| answer=='Y')
{
cout<<"Enter a number between 1 and 100 "<<endl; //The user is aske
d for a guess
cin>>guess; //The guess is sto
red here
tries++; //Adding a number f
or every try
if(guess==0||guess>100) //If statement that
produces an error message if user enters a number out of the peramiters
{
cout<<"This is not an option try again"<<endl; //Error message
}
if(tries<20)
cout<<"Tries left: "<<(20-tries)<<endl; //Output to let th
e user know how many guess they have left
if(number<guess); //if the guess is
to high
cout<<"Too high try again"<<endl; //This message pri
nts if guess it to high
if(number>guess) //if the guess is
to low
cout<<"Too low try again"<<endl; //This message pri
nts if the guess is to low
if(number==guess) //If the user gue
sses the number
{
cout<<"Congratualtions!! "<<endl; //Message printe
d out if the user guesses correctly
cout<<"You got the right number in "<<tries<<" tries"<<endl; //Lets the us
er know how many guess they used
answer = 'n';
}
if(tries >= 20) //If the user
uses all their guesses
{
cout << "You've run out of tries!"<<endl; //The message
that prints when a user is out of guesses
answer='n';
}
if(answer=='n')
{
cout<<"Would you like to play again? Enter Y/N"<<endl; //asking if p
lay would like to play again
cin>>answer; //Store users
answer
if (answer=='N'|| answer=='n') //if the user
says no
cout<<"Thanks for playing!"<<endl; //This messag
e prints out if they choose not to play again
else
number=rand()%100+1; //This start
s the game over if they choose to play again
}
}
}
cin>>chr;
return 0;
}

You might also like