The computer stores a random number between 1 and 15 and the player (user) attempts to guess it. The player has a total of three attempts. After each wrong guess, the computer tells the user if the number was too high or too low. If the third attempts
is also wrong, the number is output on screen. The player wins if he or she can guess the number within three attempts
{Please not I used 1-- 3 to see the differece, but still not so good!!}
/* Generating a random number */
#include <iostream>
#include <ctime>
using namespace std;
int main ()
{
int randNumber, guess;
/* initialize random seed: */
srand ( time(NULL) );
/* generate a random number number: */
randNumber = rand() % 1 + 3;
do
{
cout << ("guess the number (1 to 3): ");
cin >> guess;
if (randNumber <guess) cout << ("The secret number is lower");
else if (randNumber >guess) puts ("The secret number is higher");
}
while (randNumber != guess);
Well this helped little bit.but I want the guessing to be 3 times before the computers shows what the right number is
This is the latest code.
/* Generating a random number */
#include <iostream>
#include <ctime>
using namespace std;
int main ()
{
int randNumber, guess;
/* initialize random seed: */
srand ( time(NULL) );
/* generate a random number number: */
randNumber = rand() % 15 + 1;
do
{
cout << ("guess the number (1 to 15): ");
cin >> guess;
if (randNumber <guess) cout << ("The secret number is lower");
else if (randNumber >guess) puts ("The secret number is higher");
}
while (randNumber != guess);
This would take a couple of easy changes, first you should declare a counter int, to keep track of how many guesses you've made. Then have the Do While loop subtract from the counter, and check what it's on in some way, I'd write it something like:
1 2 3 4 5 6 7 8
do
{
//
//All your code in here
//
counterInt--;
}
while (randNumber != guess || counterInt != 0); //If counterInt was initialized to 3
I also suggest that instead of it always saying "Congratulations!" you could put an if/else statement based on a bool that tells you if any of the guesses were right.