Write your question here.
Hi I have just started coding and was wondering if you could help me out.
It keeps telling me the answer. I also need to make it ask if you want to play again. I would apperaicate some help because I have been working on this for awhile.
also I it has to be between 5-15
and I also need to give the user the opportunity to play the game again or quit.
Thanks
#include <iostream>
#include <cstdlib> // Needed for random numbers
#include <ctime>
usingnamespace std;
int main()
{
int guess, tries=0, randomNum;
// Will hold the randomly generated integer
srand(time(0));// Seed the random generator
randomNum = 5 + rand() % 11; //random number between 5-15
do
{
cout<< "Guess a number between 5 and 15 "<<endl;
cout<<randomNum<<endl;
cin>>guess;
tries++;
if(randomNum < guess)
cout<<"Too high, Try Again."<<endl;
elseif(guess<randomNum)
cout<<"Too low, Try again."<<endl;
else
cout << "\nCongratulations. you guessed the number and it took you \n " << tries << " time to find out!\n";
}while (guess != randomNum);
cin.ignore();
cin.get();
}
Random number is displayed because you do so on line 17.
And I also need to give the user the opportunity to play the game again or quit.
To do this you have to wrap the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
randomNum = 5 + rand() % 11; //random number between 5-15
do
{
cout<< "Guess a number between 5 and 15 "<<endl;
cin>>guess;
tries++;
if(randomNum < guess)
cout<<"Too high, Try Again."<<endl;
elseif(guess<randomNum)
cout<<"Too low, Try again."<<endl;
else
cout << "\nCongratulations. you guessed the number and it took you \n " << tries << " time to find out!\n";
}while (guess != randomNum);
#include <iostream>
#include <cstdlib> // Needed for random numbers
#include <ctime> // For the time function
#include <string> //for a string
usingnamespace std;
int main()
{
int guess, tries=0, randomNum;
// Will hold the randomly generated integer
char again;
srand(time(0));// Seed the random generator
randomNum = 5 + rand() % 11; //random number between 5-15
do
{
randomNum = 5 + rand() % 11;
cout<< "Guess a number between 5 and 15 "<<endl;
cout<<randomNum<<endl;
cin>>guess;
tries++;
if(randomNum < guess)
cout<<"Too high, Try Again."<<endl;
elseif(guess<randomNum)
cout<<"Too low, Try again."<<endl;
else
cout << "\nCongratulations. you guessed the number and it took you \n " << tries << " time to find out!\n";
cout<<"Do you want to play again? (Y/N)";
cin>>again;
}while(again == 'Y' || again == 'y');
while (guess != randomNum);
cin.ignore();
cin.get();
}
I just figured it out. I Feel so dumb for putting it there.
#include <iostream>
#include <cstdlib> // Needed for random numbers
#include <ctime> // For the time function
#include <string> //for a string
using namespace std;
int main()
{
int guess, tries=0, randomNum;
// Will hold the randomly generated integer
srand(time(0));// Seed the random generator
randomNum = 5 + rand() % 11;//random number between 5-15
do
{
cout<< "Guess a number between 5 and 15 "<<endl;
cin>>guess;
tries++;
if(randomNum < guess)
cout<<"Too high, Try Again."<<endl;
else if(guess<randomNum)
cout<<"Too low, Try again."<<endl;
else
cout << "\nCongratulations. you guessed the number and it took you \n " << tries << " time to find out!\n";
//...
while(!done)
{
//except you have to take these 2 lines out of your while so only one random number
//is generated for each guessing game
randomNum = 5 + rand() % 11;
cout<< "Guess a number between 5 and 15 "<<endl;
time = 0; //also null the times each time you repeat the game
/* your do while goes here */
cout << "Another try? (Y/N)? : ";
char ch;
cin >> ch;
if(ch == 'N' || ch == 'n') done = true;
}
cin.ignore();
cin.get();
so that you can repeat your own loop as many times as you want. With current code you generate new random number every time you either guess it or don't.