Grr...Easy problem I can't fix

Started by
10 comments, last by Omnesolis 23 years, 10 months ago
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; } } }
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
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
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.

--Float like a butterfly, bite like a crocodile.
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;
}

}
}
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....

Thanks sooo much..it works perfectly now! Now to upgrade it to a better game!
A good second game is something like roulette or a word guessing game
Those who dance are considered insane by those who cannot hear the music.
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.
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
Here is something to help get you kickstarted.
    #include <stdio.h>//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)
Chris Brodie

This topic is closed to new replies.

Advertisement