ID:186622
 
I believe this post is related to Computers and Technology but I wasn't sure what area to post it in, so if this is the wrong place please move my thread.

I have been working on C++ by myself for a few weeks trying to learn it as best as I can, with a crappy little book I bought at Barns & Noble. I have learned a few things so I decided to make a program to show off what I have learned, a Magic 8 ball program. It would show an example of what I learned except one thing the book didn't teach me, Random functions or how to make one. I really could use some help with my 8 ball function or if anyone could please, make a random function/library for me that uses something like byonds. Rand(int a, int b);. I would be oh so gratefull if someone could help me with this. Alas, my book doesn't teach me anything about RAND_MAX and the formula I stole from some web site. Anyway also if you could fix my code that would be helpfull plus the rand function. It might also be helpfull to know that my rand line of code always returns 2 for some reason...so it always displays Answer[2]

#include <iostream> //Built in libs
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std; //So we don't have to type std::
int pause; //Declare pause as a variable

int main()
{
int j; //Declare a local variable
const int N=5; //Declare a constant variable
string disregardedQuestion; //Declares a variable you can type into as text
string answer[5]={ //Declares a string array
"Yes",
"No",
"Not a chance",
"Could be",
"Maybe"};
for(int j; j<25; j++){ // I stole this off the website, I would think it would have to be J<5, typo?
rand(); //Call rand if J is less than 25
}
cout << "Enter your question\n";
cin >> disregardedQuestion; //Asks for a value for Disregarded question
j = (int) N * rand() / (RAND_MAX + 1.0); // I can barely understand this, I stole this from a web site.
cout << answer[j]; //Display the output of J in the array, which should be 0-4 or 1-5 I hate the counts from 0 thing!

cout << "\n\n The program has ended, press any key to continue.\n";
cin >> pause;
return 0;
}
rand(1,100)//Use this to randomly pick a number between 1 and 100

There you go, if you need any more help just use the Resources to the left
<----
In response to Kasumi Tomonari
Uhm, did you even read the post? C++ has no rand() function unless you create it yourself or include a library with it.
If I remember correctly rand() returns a random unsigned int between 0 and the max value of an int(this varies depending on the cpu being developed for). So a good way to adjust the range of your random value is with the modulus. For instance

rand() % 10

will generate values from 0 to 9. If you want to change the minimum range just subtract the minimum from the max and add it again after the modulus.

rand() % (10 - 2) + 2

This will generate a number from the range 2 to 9.

For a function to do this you could have

inline int randInt(int min, int max)
{
return rand() % (max-min+1) + min
}

The demo code you posted looks like it is generating floating point random numbers. The reason it divides by RAND_MAX + 1.0 is to scale the number from 0 to RAND_MAX to 0 to 1. Then when you have a number from 0 to 1 you just multiply this number by the max value you want from it which in the case of the demo code is 0 to N.

This is however much slower than my version because of the int to floating point conversions(then back to int again). If you just want an integer value just stick with my version.
In response to Nadrew
Yeah, that was pretty rude. Assuming im just some stupid newbie. Thanks alot to the first replier. This will hopefully help me quite alot. If it doesn't, ill come back and bitch. *grin*
In response to InuTracy
Okay, that ultimatley solved my problem. Now I have two more problems. One is, I don't think the program is seeding it more than once. It generated one answer and it won't give me a new one because it won't reseed. Can you help me get it to reseed the program now? That's all that I really need help with besides one other thing. I have a problem where if I enter a space into the disregardedQuestion variable, then more text it sort of crashes almost and closes without running the rest of the program. Am I not supposed to use a string? Maybe I should make it a char variable.

Here is the source code after I changed it
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int pause;


inline int randInt(int min, int max)
{
return rand() % (max-min+1) + min;
}

int main()
{
int j;
const int N=5;
string disregardedQuestion;
string answer[5]={
"Yes",
"No",
"Not a chance",
"Could be",
"Maybe"};
cout << "Enter your question\n";
cin >> disregardedQuestion;
j=randInt(0,4);
cout << answer[j];

cout << "\n\n The program has ended, press any key to continue.\n";
cin >> pause;
return 0;
}



Edit: I just saw thoes DM pictures, sort of avatars in the forums. Is that something new being implemented? I don't have one next to my name. Perhaps it's only administrators and/or moderators?
In response to InuTracy
Read the latest announcment.
In response to Smoko
Whoa..I've installed it. I don't know what to think. It made my heart rate increase alot. This is intresting, I like where it's going sort of. I mean i've been waiting for byond 4.0 for 2 or 3 years now. It's about bloody time. It's not cool that it's going to have to pay to become a byond member to use thoes extra features though.

BACK TO TOPIC
I would like someone to help me with what I replied a few posts ago.
In response to InuTracy
I would use a c-style string (char array), the use getline() for input. Strings get a bit funky, at least for beginners like me and you.

Also, I believe you're looking for randseed(), srand(), or seedrand(). I forgot what function C++ uses for seeding rand, but I bet it's one of those (I'm pointing more towards srand()).

Prodigal Squirrel
In response to Prodigal Squirrel
Thanks alot! I did some googlin on getline and srand to find out the correct syntax and I worked it all out. The final program looked like this.


#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int pause;


inline int randInt(int min, int max)
{
srand ( time(NULL) );
return rand() % (max-min+1) + min;
}

int main()
{
int j;
const int N=5;
char disregardedQuestion[256];
string answer[5]={
"Yes",
"No",
"Not a chance",
"Could be",
"Maybe"};
cout << "Enter your question\n";
cin.getline (disregardedQuestion,256);
j=randInt(0,4);
cout << answer[j];

cout << "\n\n The program has ended, press any key to continue.\n";
cin >> pause;
return 0;
}
Ok, when I was using VC++ 6 I would always have my main() before the rest of my functions, but when I tried to compile something in GCC it tossed this cookie:

<code>'functionnamehere' undeclared (first use this function) </code>

I was like, "huh what the..." so I tried moving the function ABOVE main() and it went ahead and compiled...

Why is that? Do non-VC++ 6 compilers require functions to be declared physically above their use?

Thanks all C++ gurus =P

~Kujila
In response to Prodigal Squirrel
Yeah I don't use the "string" variable type, either. Long arrays for me -_-;;

My teacher said learning the string variable type before learning arrays and such would be like learning to ride a bike before learning to run... you could do it, but it's probably better to learn how to run first =D

And yes, I agree Squirrel and think you should use cin.getline() if you're grabbing a string input...

cin >> can be troublesome with array input, especially with spaces are involved! =D

~Kujila
In response to Kujila
Kujila wrote:
Ok, when I was using VC++ 6 I would always have my main() before the rest of my functions, but when I tried to compile something in GCC it tossed this cookie:

<code>'functionnamehere' undeclared (first use this function) </code>

I was like, "huh what the..." so I tried moving the function ABOVE main() and it went ahead and compiled...

Why is that? Do non-VC++ 6 compilers require functions to be declared physically above their use?

Thanks all C++ gurus =P

~Kujila

I think that the function and its arguments need to be declared first, but the actual function with code can be filled out later.
In response to Jon88
AHHHHHHHH I FORGOT PROTOTYPES AHHHH

...but thanks fer' reminding me XD


I feel so dumb now :'(


~Kujila
In response to Jon88
Jon88 wrote:
I think that the function and its arguments need to be declared first, but the actual function with code can be filled out later.

More or less. If you wrote a function above your main() function, and defined it there, too, you can use it. Just so long as the function is known to exist before main(), everything's okay. So you could put every function at the top of your code, and not have to use prototypes. But, it's better to stick to prototyping, makes looking through the code easier to look at.

Prodigal Squirel
In response to Prodigal Squirrel
Prodigal Squirrel wrote:
Jon88 wrote:
But, it's better to stick to prototyping, makes looking through the code easier to look at.

Prodigal Squirel

Ja ja, I totally forgot about prototyping in my original post hehe ^^;;

~Kujila
In response to Kujila
I wasn't replying to what you said, just Jon88.