141k views
3 votes
In this warm up project, you are asked to write a C++ program proj1.cpp that lets the user or the computer play a guessing game. The computer randomly picks a number in between 1 and 100, and then for each guess the user or computer makes, the computer informs the user whether the guess was too high or too low. The program should start a new guessing game when the correct number is selected to end this run (see the sample run below).

Check this example and see how to use functions srand(), time() and rand() to generate the random number by computer.

Example of generating and using random numbers:

#include
#include // srand and rand functions
#include // time function
using namespace std;

int main()
{
const int C = 10;

int x;
srand(unsigned(time(NULL))); // set different seeds to make sure each run of this program will generate different random numbers
x = ( rand() % C);
cout << x << endl;
x = ( rand() % C ); // generate a random integer between 0 and C-1
cout << x << endl;
return 0;
}

User Ffao
by
4.7k points

1 Answer

2 votes

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.\\ :";

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!\\ :";

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\\ :";

}

// Tells the user if they are bellow the number

else if (UserSelection < RandomNumber)

{

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

}

// 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!\\ :";

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.\\ ";

system("pause");

}

// Second try win

else if (Tries == 4)

{

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

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.\\ ";

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.\\ ";

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.\\ ";

system("pause");

}

// Losing dialogue

else

{

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

system("pause");

}

return 0;

}

Step-by-step explanation

C++, console based guessing game.

Int Tries at the top of main is linked to how many tries the user has

Read the comments in the code they roughly explain whats going on.

User Nidhin Rejoice
by
5.6k points