Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Omnesolis

Grr...Easy problem I can't fix

This topic is 6662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK. For my first game, I''m making a simple, "Guess the number between 1 and 10" game. Everything seems to be working fine, except for one thing. The ''rand()'' function isn''t working. I was told that I had to make it rand(NULL), but that gave me errors for ''rand''. So, I took out the NULL, and everything worked, but the computer would ONLY selects 6. Here''s my source code: //Guess A Number Script # include # include # include int guess; void main() { int random; random = rand()%9 +1; int running = 1; while(running) { cout << "Player, Please guess the number, 1 through 10\n"; cin >> guess; if (guess < random) { cout << "Too Low!\n"; } if (guess > random) { cout << "Too High!\n"; } if (guess == random) { cout << "Correct! The number was " << random << "!\n"; running = 0; } } }

Share this post


Link to post
Share on other sites
Advertisement
You must do this at the beginning:

srand(time);

and I think you must
#include "time.h"

That way it picks different numbers each time...

Edited by - Nazrix on June 20, 2000 2:29:43 PM

Share this post


Link to post
Share on other sites
also, it should be (rand()%9)+1 (which doesnt cause your problem, but the way you have it now returns a number between 0 and 9 due to operator precedence)


--
Float like a butterfly, bite like a crocodile.

Share this post


Link to post
Share on other sites
Nazrix, that didn''t work..it gave me more errors, in fact. Maybe I did something wrong...Oh, and I did include time.h, it just didn''t show up in the post. Here''s what I have now:

int guess;


srand(time);

void main()
{
int random;
random = (rand()%9)+1;

int running = 1;
while(running)
{

cout << "Player, Please guess the number, 1 through 10\n";
cin >> guess;

if (guess < random)
{
cout << "Too Low!\n";
}
if (guess > random)
{
cout << "Too High!\n";
}
if (guess == random)
{
cout << "Correct! The number was " << random << "!\n";
running = 0;
}

}
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Omnesolis
int guess;

srand(time);

void main()
{
int random;
random = (rand()%9)+1;

int running = 1;
while(running)
.
.
.
}

int guess;

void main()
{
int random;

srand( (unsigned)time( NULL ) );
random = (rand()%9)+1;
int running = 1;
while(running) etc....

Share this post


Link to post
Share on other sites
Yeah, someone mentioned to me that I should make a word guessing game that picks words randomly from a txt file, but I''m pretty clueless as to how to get a program to a) read from a text file, and b) how to get the program to remember what letters were selected.

Share this post


Link to post
Share on other sites
Why not make your own random number function?
    
float random( void )
{
static unsigned short num = 0x4242, mul = 0x6969;
num *= mul;
return float( num ) / 0xffff;
}

OneEyeLessThanNone

my fish turns opaque when its fed

Share this post


Link to post
Share on other sites
Here is something to help get you kickstarted.
    


#include <stdio.h>
//This will open the file

pFile = fopen(FILE_NAME, "r");

while (ch != EOF)
{
//This will read in one character at a time and place each character in ch

ch = getc(pFile );


}
[/source]

Since this is a academic exercide to learn more than just getting the job done I''d recommend the following:
-Learn about how to make pointers to character stings.
-Learn about how to make array''s of pointers.
-Learn how to realloc string''s and use strcpy,strcat to copy in the data.
By then you should be able to loop through the file and place each word in to the array. Then you
dig up the random code you just made and randomly select one of the array entries. The rest would be simple.

Oh, one last thing in your file put something between each word to use as a marker or use one word per line. Then to detect when you have the end of a word use something like this:

[source]
if (ch == EOF // ch == ''\r'' // ch == ''\n'')

This will detect if a line end is the current character OR if the last character is the end of the file in which case we treat it like a line end anyway.


HTH

gimp

(I was where you were 3 months ago)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!