Just a note:
the * does not really go with the datatype, but it does define its type. For instance:
int *p, q; is the same as
int *p;
int q;
to make 2 pointers it would be:
int *p, *q;
As for your other code:
Random nitpicks:
#include cstdlib and ctime
.h files are C, not C++ and thusly none standard
int main()!!!!!!!
void main() is just plain wrong.
void functions don't need a return, the compiler does this automatically for us.
This is a REAL REAL nitpick, but stylistically, functions should be named after what they do. GetSeed makes us think it returns a value (due to get). You should really name things more along the lines of what they do. Something like 'randomize' or 'seedrand' might be more informative. But stuff like that will come after you have the language down a bit more.
To get more to the point of your problem, it is right here:
int GetRandom(int &low, int &high) //set a random integer in range
{
double random;
random = rand() / ((double)(RAND_MAX) + 1);
return (int)((high * random) + low);
}
I'm not sure exactly what this is supposed to do...
rand() returns a number between 0 and RAND_MAX, inclusive. Follow our elementry math skills, random will be somewhere between 0 and RAND_MAX/(RAND_MAX + 1) (which is slightly below 0). So then you take this number, multiply it by 'high', and then add 'low' to it. Think of this in terms of percents.
So we get some number between 0 and high, excluding high, and then add low to it. So we get values between low and high + low (more or less). Now, I introduce you to Mr. Subtraction. Try turning this into a nice little equaltion:
random multiplied by the difference between high and low, then add low.
thanks for the suggestions. here are some responses:
i know that int *p, q; is the same as int *p; int q;
i guess i'm not seeing it all just yet, because i don't fully understand why the prototype is "function(int *);" i'm doing it now cuz it works.
i'm trying to get away from the .h files. but my compiler didn't have them without the .h. do u think i can manually rename the files? do i have to install additional libraries? for time's sake, i just added the .h so the compiler stopped complaining. (btw, i'm using Visual Studio 6.0)
i know u said 'int main()' before. right now, the professor wants it this way. we're not returning a code to the OS, so it really doesn't matter right now. (does it?)
i know void functions don't need return. i don't think i was consistent, but i wanted the empty returns to be clear that no value was returned. i was told it's optional.
it's a funny thing u said about the function names. just today, i learned about the 'get' method for i/o. now i see a confusion with 'getseed'. before today, i just read it as 'go get a seed value' and 'go get a random number'. they'll get changed now.
i actually referenced the wrong page in my book for the random function. the return code i ended up using was:
return (int)((high - low) * random + low);
u said 'you're not sure exactly what this is supposed to do...', but then you explain it anyway. so i'm figuring you get it.
- RAND_MAX, of course, is the highest random integer (usually 32,728 or something).
- (double)(RAND_MAX) type casts the integer to a double
- rand()/((double)(RAND_MAX)+1); gives a small number (like 0.6523829)
- return (int)((high - low) * random + low); should type cast the double into an integer. and this is supposed to be inclusive, but i couldn't prove it. choosing a range of 1 and 3, the answer was always 2 (after 10 tries...i'm not gonna keep running the program at 3am!)
anyway...everything's turned in, and i'm onto the next assignment. now we're doing file i/o with a high score file, creating an encrypted file and decrypting a file....oh goodie!