#### Archived

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

# Grr...Easy problem I can't fix

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

## 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 on other sites
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 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 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 on other sites
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 on other sites
Thanks sooo much..it works perfectly now! Now to upgrade it to a better game!

##### Share on other sites
A good second game is something like roulette or a word guessing game

##### 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 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 on other sites
Here is something to help get you kickstarted.
  #include //This will open the filepFile = 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 youdig 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 on other sites
Hmm...I think I know what you''re talking about. I''ll see what I can do with it, and I''m sure I''ll have some questions along the way.

##### Share on other sites
If you get really stuck I can send you my first project. A utility to scan text files and find some text names that relate to files them copy them to a directory.

It uses heaps of cool little tricks that a fairly important to learn. It''s best however to learn about everything however rather than just the easiest things...my theory is, there''s generally a reason for the hard way and it often relates to making games

Of course I''d write that utility differently today but without going through that learning process I wouldn''t have understood when to do it the easy way and when to do it the hard way.

String are a good example, some people will suggest that basic_string is the way to go to make life easy. In the end for me it made life harder and I wan''t learning about C++ but rather other people classes.

The most important thing I''ve learn''t was that memory was there for the editing.... If your feeling lucky have a play with memcpy, playing with this is when i had one of those breakthrough''s where it all started making sence....

gimp

• 30
• 11
• 17
• 11
• 13