Help with Exercises Guess my number

Started by
2 comments, last by Farbodkain 11 years, 9 months ago
Hi everyone,im sorry if i created topic for something like this,but i can't solved the last exercise chapter 2 book Beginning C++ Through Game.i hope someone here can help me
here it is:
they want to switch Player by CPU.i mean player choose number and CPU take guess untile reach the answer.but i don't find way to do that.i start C++ new and i Can't do something like that.i really appreciated if someone help me

here is Original one

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
srand(static_cast<unsigned int>(time(0))); //seed random number generator

int secretNumber = rand() % 100 + 1; // random number between 1 and 100
int tries = 0;
int guess;

cout << " Welcome to Guess My Number\n\n";

do
{
cout << "Enter a guess: ";
cin >> guess;
++tries;

if (guess > secretNumber)
{
cout << "Too high!\n\n";
}
else if (guess < secretNumber)
{
cout << "Too low!\n\n";
}
else
{
cout << "\nThat's it! You got it in " << tries << " guesses!\n";
}

} while (guess != secretNumber);

return 0;
}
Advertisement
Think of it as this, the computer guesses a random number ( using the rand() % 100 + 1 ) then you check if it's higher, lower or correct.
This can be done with some sort of loop.

For example a while loop:
Pseudo-code(ish):

int guess = rand() % 100 + 1 // Get the initial guess from the computer
while(true)
{
if(guess < choosenNumber) {
cout << "That is to low!" << endl;
guess = rand() % 100 + choosenNumber;
}
else if(guess > choosenNumber) {
cout << "That is to high!" << endl;
guess = rand() % choosenNumber + 1;
}
else
{
cout << "The correct number was guessed" << endl;
return;
}
}


int guess = rand() % 100 + 1 // Get the initial guess from the computer
while(true)
{
if(guess < choosenNumber) {
cout << "That is to low!" << endl;
guess = rand() % 100 + choosenNumber;
}
else if(guess > choosenNumber) {
cout << "That is to high!" << endl;
guess = rand() % choosenNumber + 1;
}
else
{
cout << "The correct number was guessed" << endl;
return;
}
}



You got the statements the opposite way. If the chosen number is higher, then you are outputting "..low.." instead of "..high..". and vise versa.
thanks guys i do something like this.


//Copmuter Guess My Number
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0)));
int yourNumber;
int triess=0;
int maxNumber=100;
int minNumber=1;
int cpuNumber;
cout<<"Please Enter Your Number: "<<endl;
cin>>yourNumber;
do
{
cpuNumber=rand()%(maxNumber-minNumber)+minNumber;
++triess;
cout<<"\nComputer Guess is: ";
if (yourNumber>cpuNumber)
{
cout<<cpuNumber<<endl;
minNumber=cpuNumber+1;
}
else if (yourNumber<cpuNumber)
{
cout<<cpuNumber<<endl;
maxNumber=cpuNumber-1;
}
else if (yourNumber == cpuNumber)
{
cout<<"right number is "<<cpuNumber<<endl;
cout<<"\nAfter "<<triess<<" Tries You success to reach my number"<<endl;
}
} while (yourNumber != cpuNumber );
return 0;
}

This topic is closed to new replies.

Advertisement