Write a program named Guessing Game that generates a random number between 1 and 100. (In other words, min is 1 and max is 100). The random number can be store it in a form level variable. Ask the user to guess a random number using a textbox control to enter the number. The user clicks a button "Guess". In the event, Evaluate the input number and if their guess was too high or too low, display a message indicating whether the guess is too high, too low or correct. If they did not guess the correct number the message should tell them to guess again "Your guess is Too High (or Too Low)". You can keep track of the number of guesses so that you can tell the user how many guesses it took to guess the correct number. After each guess, reset the focus to the textbox for the next guess. You would probably want to clear the text box also. When the correct number is guessed allow the user to reset the game so that they can guess a new random number. This could be done with another button called "Reset".

Respuesta :

Answer:

#include <iostream>

#include <time.h>

using namespace std;

int main()

{

// Sets the random() seed to a relatively random number

srand(time(0));

// Variables are defined

int RandomNumber = rand() % 100 + 1, UserSelection, Tries = 5;

// Gets a number from the user

cout << " Guess a number between 1 and 100, you have five tries to find the correct number.\n :";

cin >> UserSelection;

// Prevents the user from selecting a number out of bounds

while (UserSelection > 100 || UserSelection < 1)

{

 cout << " I said between 1 and 100. select a new number BETWEEN 1 AND 100!\n :";

 cin >> UserSelection;

}

// Stuck in while till they guess right, or run out of tries

while (UserSelection != RandomNumber)

{

 // kicks user from the loop when they run out of tries

 Tries -= 1;

 if (Tries == 0)

 {

  break;

 }

 // Tells the user they got the wrong number and how many tries they have left

 cout << " The Number was not correct, you have " << Tries << " more Guess(es) left.";

 // Tells the user if they are above the number

 if (UserSelection > RandomNumber)

 {

  cout << " Try guessing a little lower\n :";

 }

 // Tells the user if they are bellow the number

 else if (UserSelection < RandomNumber)

 {

  cout << " Try guessing a little higher\n :";

 }

 // User input if the number is wrong they stay in the loop, if they are right they fail the condition for the loop and get dialogue according to how many tries they have left

 cin >> UserSelection;

 // Prevents the user from selecting a number out of bounds

 // If the number is greater than 100 or less than 1 they are prompted to select a new number

 while (UserSelection > 100 || UserSelection < 1)

 {

  cout << " I said between 1 and 100. select a new number BETWEEN 1 AND 100!\n :";

  cin >> UserSelection;

 }

}

// The amount of tries the user has left determines what dialogue they get

// First try win

if (Tries == 5)

{

 cout << " That's not luck, that's skill. you got the number right on the first try.\n ";

 system("pause");

}

// Second try win

else if (Tries == 4)

{

 cout << " That's pretty good luck, you guessed it right on the second try.\n ";

 system("pause");

}

// Third try win

else if (Tries == 3)

{

 cout << " Could be better, but it could also be way worse. You got it right on the third try.\n ";

 system("pause");

}

// Fourth try win

else if (Tries == 2)

{

 cout << " Could be worse, but it could also be a lot better. You got it right on your fourth try.\n ";

 system("pause");

}

// Fifth try win

else if (Tries == 1)

{

 cout << " I hope you don't gamble much. You got it right on your last guess.\n ";

 system("pause");

}

// Losing dialogue

else

{

 cout << " You guessed wrong all five times, the right number was " << RandomNumber << "\n ";

 system("pause");

}

return 0;

}

Explanation:

Written in c++, read the comments in the code detailing whats going on.