Sign in to follow this  
chris_bell

Noob Here, need help with "Guess My Number"

Recommended Posts

I have just recently purchased Michael Dawson's book, "Beginning C++ Game Programming" but have come to a road block upon completing chapter two. (I have zero previous knowledge of any programming) In the end of chapter 2, one of Dawson's exercises is as follows: "3. Write a new version of the Guess My Number program in which the player and the computer switch roles. That is, the player picks a number and the computer must guess what it is." On his CD, Dawson supplies the code for the OPPOSITE program (where the user guesses the computer's number). I'm trying to rearrange steps and place the user in the computer's shoes, though I'm stumped...Here's what I _THINK_ I know... -------------------- 1. I need to have the user input an integer number to be guessed... "cin >> theNumber;" 2. I need to have the computer choose random numbers using the "rand()" 3. I need to use a 'do' accompanied by a series of 'if's...the loop must wait for the user to hit enter (or some key), then the computer will choose a random number from say...1-100. If the number is too low, it gives the computer a message saying so, same with if its too High. 4. With each new number guessed, i need to increase the number of tries by 1. ++tries; (where 'tries' is the integer value of the number of tries performed) 5. Need to end loop when computer finds number....also display a "Congrats!" or something -------------------- My biggest problem is understanding how to have the computer "guess" or choose a random number...though I'd like to view the entire program's code Any basic examples of how to accomplish this program are appreciated. This being my first post, I hope not to barge in on this new community if I'm asking too much so soon.... (this post probably could have been alot shorter too lol)

Share this post


Link to post
Share on other sites
Well, if you want to have the computer guess a random number between 1-100, you would use the rand function like so:

int com_guess = rand() % 100;

To clarify, by default, rand() chooses a random number between 0 and RAND_MAX. RAND_MAX is around 30,000 I think. Anyway, there is a way to make the random number generator pick between 0 and a number you define. If you put a % symbol, followed by the max number you want the random number generator to go up to, it will work.

I hope that answers your question.

Share this post


Link to post
Share on other sites
Well...first you have to write a function that picks a random number between to numbers.

int Random(int high, int low);
there are a few ways to do this, but the easiest is as follows

int Random(int high, int low)
{
//this selects a radnom number between high and low
return rand() % (high - low + 1) + low;
}

this will allow you to choose random numbers

to allow for a more random number you should seed rand() which will create unique random numbers every time the program is run as oppose to the same set.

you can do this by writing this at the top of main(as a seperate functino call or not)

srand((unsigned int)time(0));


after you do this you can follow the following general idea.

have the cpu chose a number...if it is too high or too low have it select a new number accordingly.

for example
user types 30

cpu selects(1-100) 50
too high
cpu slects(1-50) 20
too low
cpu selects(20-50) 30
Bells and whistles


Hope this helps.

Regards, Me

Share this post


Link to post
Share on other sites
OK...thanks for all the help so far...this is where I am at the moment.
Lines marked by "*****" is where I know a problem exists...

// Guess My Number 2.0
// Have computer guess your number

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

using namespace std;

int main()
{
int theNumber;
int comGuess = rand() % 100 + 1;
int tries;

cout << "\tWelcome to Guess My Number 2.0!\n\n";

cout << "Please enter a number to be guessed: ";
cin >> theNumber;

do
{
cout << "\n\nPress Enter to see the Computer's guess...";
*****How do I get the program to wait for enter (or a key) to be pressed?*****

return comGuess;
cout << "The Computer's Guess: " << comGuess << ".\n";
++tries;

if (comGuess > theNumber)
cout << "Too high!\n\n";

if (comGuess < theNumber)
cout << "Too low!\n\n";

} while (comGuess == theNumber);

cout << "\nThat's it! The Computer got it in " << tries << " guesses!\n";

}

-----------------------------------

I doubt that's everything as I haven't had a successful run yet.
I'm pretty sure I will still have problem's getting the computer to choose a random number. Wasn't clear how to seed rand() successfully so I could be attempting some noob way to generate and return random numbers.

Any additions to making my learning program more complete are welcome :D

Share this post


Link to post
Share on other sites
Quote:
Original post by Stompy9999
Well, if you want to have the computer guess a random number between 1-100, you would use the rand function like so:

int com_guess = rand() % 100;

To clarify, by default, rand() chooses a random number between 0 and RAND_MAX. RAND_MAX is around 30,000 I think. Anyway, there is a way to make the random number generator pick between 0 and a number you define. If you put a % symbol, followed by the max number you want the random number generator to go up to, it will work.

I hope that answers your question.


That would give you a number from 0 to 99, not 1 to 100. As chris_bell has done, you would need:

int com_guess = rand() % 100 + 1;

Share this post


Link to post
Share on other sites
Can anyone figure out what's wrong with this program? Closes immediately after user enters "theNumber".
I tried using system("pause") where i have "cout << "\n\nPress Enter to see the Computer's guess...";" but the program would just get to that new pause point and close afterward (not much further than the previous closing @ entering 'theNumber').
-----------------------------------------

// Guess My Number 2.0
// Have computer guess your number

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

using namespace std;

int main()
{
int theNumber;
int comGuess = rand() % 100 + 1;
int tries;

cout << "\tWelcome to Guess My Number 2.0!\n\n";

cout << "Please enter a number to be guessed: ";
cin >> theNumber;

do
{
cout << "\n\nPress Enter to see the Computer's guess...";
cin.get();
return comGuess;
cout << "The Computer's Guess: " << comGuess << ".\n";
++tries;

if (comGuess > theNumber)
cout << "Too high!\n\n";

if (comGuess < theNumber)
cout << "Too low!\n\n";

} while (comGuess == theNumber);

cout << "\nThat's it! The Computer got it in " << tries << " guesses!\n";

}

Share this post


Link to post
Share on other sites
Your problem is the line "return comGuess" that statement causes the program to exit with comGuess as the programs return value. This is likely not what you want.

Most likely what you want is to move the line "int comGuess = ..." into the do loop in place of your "return comGuess" line.

Share this post


Link to post
Share on other sites
thanks for that, i wasn't really sure what to do about obtaining a value each time for comGuess.
now for some reason, though, i'm getting an error on the 'while' line...though i see nothing wrong with it.

} while (comGuess == theNumber);

seems fine to me...

Share this post


Link to post
Share on other sites
Quote:
Original post by chris_bell
thanks for that, i wasn't really sure what to do about obtaining a value each time for comGuess.
now for some reason, though, i'm getting an error on the 'while' line...though i see nothing wrong with it.

} while (comGuess == theNumber);

seems fine to me...


while statements do not have an end ';'

so your line should look like "} while (comGuess == theNumber)" but you probably want to make that a "!=" otherwise it will only continue if the computer gets it right rather than continuing until the computer gets it right.

Share this post


Link to post
Share on other sites
just got the last message while posting. Thanks for the help :D.
Noobs gotta start somewhere lol

EDIT: Hmm..... wow. STILL getting an error.

Line currently looks like so:

} while (comGuess != theNumber)

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave Hunt
Quote:
Original post by jperalta
[while statements do not have an end ';'


while statements don't, but do-while statements do. The closing ';' is required.


I actually put the same thing down, and then checked it in my C++ book. To my suprise, Do-While statements actually have a semi-colon.

Share this post


Link to post
Share on other sites
i don't know if this is what is causing your error or not, but you really should have a return 0; at the very end of the int main() function.

vc++ 6 is just giving me a warning about it, but other compilers might not allow it.

edit: also, while i can compile it, you have some logic errors as well.

Share this post


Link to post
Share on other sites
now don't use this unless you understand how it works, but here is a fully functional version.


// Guess My Number 3.0
// Have computer guess your number

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

using namespace std;

int main()
{
int theNumber;
int comGuess;
int tries = 0;
int lowest = 1; //the lowest number the computer should guess
int highest = 100; //the highest number the computer should guess

cout << "\tWelcome to Guess My Number 2.0!\n\n";

cout << "Please enter a number to be guessed: ";
cin >> theNumber;

do{
comGuess = (highest + lowest)/2;
cout << "\n\nPress Enter to see the Computer's guess...";
cin.get();
//return comGuess;
cout << "The Computer's Guess: " << comGuess << ".\n";
++tries;

if (comGuess > theNumber){
cout << "Too high!\n\n";
highest = comGuess;
}

if (comGuess < theNumber){
cout << "Too low!\n\n";
lowest = comGuess;
}

} while (comGuess != theNumber);

cout << "\nThat's it! The Computer got it in " << tries << " guesses!\n";

return 0;
}




remember, learning is the most important part, regardless of what level of learning you're at.

Share this post


Link to post
Share on other sites
sorry for the late response, but i had to run out the house. Thanks for all the replies, i'll study that program and learn from it CJWR. I'm in it for the learning, that's why i didn't just jump to chapter 3 when I couldn't figure this out from the start.

Again, thanks. For a problem with such a simple program it's nice to know you guys are still passionate about helping. Looks like I'll be hanging around here for a while and learning.

Share this post


Link to post
Share on other sites
Quote:
Original post by chris_bell
1. I need to have the user input an integer number to be guessed

I don't think that's necessary. Wouldn't it make more sense to let the computer guess and each try you say if the number is higher or lower? If two humans played the game together it would seem odd to me if the first step is telling the other one the number he has to guess...

Share this post


Link to post
Share on other sites
Quote:
Original post by Fred304
Quote:
Original post by chris_bell
1. I need to have the user input an integer number to be guessed

I don't think that's necessary. Wouldn't it make more sense to let the computer guess and each try you say if the number is higher or lower? If two humans played the game together it would seem odd to me if the first step is telling the other one the number he has to guess...


Quoted for not actually missing the point.

The game goes like this:


- Output welcome message, and ask user to *think of* a number.
- Decide on an initial guess.
- Repeat:
- Increment count of guesses.
- Output the current guess and ask the user to indicate if it is high, low or correct.
- If guess was correct, we're done and can break out of this loop.
- Otherwise, modify the current guess (and loop again).
- Output the count of guesses and terminate.

Share this post


Link to post
Share on other sites
Quote:
Original post by CJWR
i don't know if this is what is causing your error or not, but you really should have a return 0; at the very end of the int main() function.

vc++ 6 is just giving me a warning about it, but other compilers might not allow it.


VC++ 6 is non conforming in this respect the standard has an exception for main stating that you don't need to return a value from it, if you omit the return statement 0 is implicitly assumed and your program is still conforming.

main is the only exception though so all other functions declared to return values must do so.

Share this post


Link to post
Share on other sites
Quote:
Original post by chris_bell
Can anyone figure out what's wrong with this program? Closes immediately after user enters "theNumber".
I tried using system("pause") where i have "cout << "\n\nPress Enter to see the Computer's guess...";" but the program would just get to that new pause point and close afterward (not much further than the previous closing @ entering 'theNumber').
-----------------------------------------

// Guess My Number 2.0
// Have computer guess your number

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

using namespace std;

int main()
{
int theNumber;
int tries;

cout << "\tWelcome to Guess My Number 2.0!\n\n";

cout << "Please enter a number to be guessed: ";
cin >> theNumber;

do
{
int comGuess = rand() % 100 + 1; //Problem 2
cout << "\n\nPress Enter to see the Computer's guess...";
getch();
cout << "The Computer's Guess: " << comGuess << ".\n";
++tries;

if (comGuess > theNumber) {
cout << "Too high!\n\n"; } //Not a problem, but its bad style

if (comGuess < theNumber) {
cout << "Too low!\n\n"; } //Same

} while (comGuess != theNumber); //Problem 1

cout << "\nThat's it! The Computer got it in " << tries << " guesses!\n";
getch();

}


Also, note that after the computer gets it it quits.

Also note: I'm not that skilled at C++ yet i picked up all the errors in ~10 seconds.

From,
Nice coder

Share this post


Link to post
Share on other sites
Quote:
Original post by Nice Coder
Also, note that after the computer gets it it quits.

Also note: I'm not that skilled at C++ yet i picked up all the errors in ~10 seconds.

From,
Nice coder


is there actually any c++ in that? i only see c statements. but i don't know c lol.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this