• Create Account

# Help with Exercises Guess my number

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Farbodkain  Members   -  Reputation: 113

Like
0Likes
Like

Posted 02 July 2012 - 07:30 AM

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;
}

Edited by Farbodkain, 02 July 2012 - 07:36 AM.

### #2Moonkis  Members   -  Reputation: 273

Like
1Likes
Like

Posted 02 July 2012 - 07:49 AM

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;
}
}


Edited by Moonkis, 02 July 2012 - 07:50 AM.

### #3anonymous22  Members   -  Reputation: 199

Like
0Likes
Like

Posted 02 July 2012 - 07:57 AM

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.

### #4Farbodkain  Members   -  Reputation: 113

Like
0Likes
Like

Posted 03 July 2012 - 12:02 AM

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;
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;
}


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS