Archived

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

Grr...Easy problem I can't fix

This topic is 6382 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
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
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
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

Share this post


Link to post
Share on other sites