Skip to main content
gamedev.net
🔒 Locked

Need help with code?

Started by NateTheGreat22 7 replies 1.1k views
Original Post
NateTheGreat22
NateTheGreat22
Hey guys,

I'm trying to create a little text game that has the computer guess the number a user inputs. The problem is that the computer keeps guessing the same number an infinite amount of times.

Here's the code :

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

using namespace std;

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

int CompGuess = rand() % 100 + 1; //random # bet. 1 and 100
int tries = 0;
int secretnumber;

cout << "\tWelcome to Guess My number V 1.1" << endl;

cout << "\nPlease input a number: ";
cin >> secretnumber;

cout << "\nNow lets see how long the AI takes to guess your number....hehehehe!!!" << endl;

do
{
cout << "Hmmm is your number " << CompGuess << " ???";
++tries;

if (CompGuess != secretnumber)
{
cout << "Try again!";
}

else
{
cout << "\nThat's it! You got it in " << tries << " guesses!\n";
}
}while (CompGuess != secretnumber);
}
deadstar
deadstar
You need to 'seed' the random number generator before using it. Find some documentation for the srand() function to get you going.

EDIT: That'll teach me to skim read, I see you're already seeding! Have you tried stepping each line using your debugger, and checking each value is what you expect?

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2   A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk
swiftcoder
swiftcoder
Quote:
Original post by NateTheGreat22
The problem is that the computer keeps guessing the same number an infinite amount of times.
Look carefully at where the computer makes its random guess (hint: it only does so once, instead of every time through the loop).
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
NateTheGreat22
NateTheGreat22
Thanks guys I got it 2 work but I still have a question

When the computer guesses a number it tends to guess a number a certain amount of times before it goes to its next guess. Is there a way to stop it from doing that??

Here's the code and try it on your compiler to have a first-person point of view of
what I mean. (P.S. - How do you format code on a forum properly?)



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

using namespace std;

int main()
{
srand(static_cast<unsigned int>(time(0))); //seed random number generator
int CompGuess = rand() % 10 + 1; //random # bet. 1 and 10

int tries = 0;
int secretnumber;

cout << "\tWelcome to Guess My number V 1.1" << endl;

cout << "\nPlease input a number from (1 - 10): ";
cin >> secretnumber;

cout << "\nNow lets see how long the AI takes to guess your number....hehehehe!!!" << endl;

while (CompGuess != secretnumber)
{

srand(static_cast<unsigned int>(time(0)));
int CompGuess = rand() % 10 + 1;

cout << "\nHmmm is your number " << CompGuess << " ???\n";
++tries;

if (CompGuess != secretnumber)
{
cout << "\nTry again!";
}
else
{
cout << "\nTook it long enough....\n";
cout << "The AI guessed your number in " << tries << " try / tries....WOW!";
break;
}


}
}
nobodynews
nobodynews
Seed the random number generator once at the beginning of the program, not every loop.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
NateTheGreat22
NateTheGreat22
.........wow....smh real embarrassing :$, thanks bro
boogyman19946
boogyman19946
Quote:
Original post by NateTheGreat22
(P.S. - How do you format code on a forum properly?)


wrap your code with [ source ] or [ code ] tags. No spaces :D

Yo dawg, don't even trip.
Metallon
Metallon
You put your code text between these tags: [ source ] and [ /source ] (without spaces). If you want to specifically use a certain syntax highlighting then you use instead [ source lang="" ] and [ /source ], with the desired language between the "". For example, source lang ="cpp" is for C++. Like so:

//Code tags (this example is specifically for C++).#include <iostream>int addition(int a, int b, int c){   int r;   r = a + b + c;   return (r);}/* Remember that you have to indent yourself(I do this by having three spaces equal one indent. */int main(){   int sum, x, y, z;   std::cin >> x >> y >> z;   sum = addition(x, y, z);   std::cout << sum << std::endl;   return 0;}


You can see a list of the different tags here: http://www.gamedev.net/community/forums/faq.asp#tags

Anyway, from looking at your source code it seems like you seed it only ones. I take it you edited it after nobodynews commented on it? Anyway, I would suggest you copy swiftcoder's reply and paste it in your source code as a comment, and use it as a reference. Then read your code line by line with no rush and make sure you understand what's happening.


EDIT: Ninja'd.

[Edited by - Metallon on January 2, 2011 1:57:58 AM]
NateTheGreat22
NateTheGreat22
@ Metallon

I actually did use swiftcoder's post as a ref. and I know have everything running smoothly

Thanks

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.