Sign in to follow this  
Farbodkain

Help with Exercises Guess my number

Recommended Posts

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

Share this post


Link to post
Share on other sites
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):
[CODE]
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;
}
}
[/CODE] Edited by Moonkis

Share this post


Link to post
Share on other sites
[quote name='Moonkis' timestamp='1341236949' post='4954902']
[CODE]
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;
}
}
[/CODE]
[/quote]

You got the statements the opposite way. If the chosen number is higher, then you are outputting "..low.." instead of "..high..". and vise versa.

Share this post


Link to post
Share on other sites
thanks guys i do something like this.

[CODE]
//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;
}
[/CODE]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this