#include <iostream>
usingnamespace std;
bool askOracle(float percentChance)
{
// Decides if a decision should
// be done (true/false), when a
// certain Percent Chance is given
//
// -> Don't forget to call randomize
// everyone once in a while!
// generate random number between 0 and 99
int randomNumber = rand() % 100;
// scale it so it's between 0 and 1
float randomResult = float(randomNumber) * 0.01f;
// compare to see if it had "happened"
return randomResult < percentChance;
}
void dropItems()
{
// our event has a 90% chance (which means 0.90f)
// so lets ask the "oracle" to see if it happens
// or not
if (askOracle(0.90f)) {
// drop coins or a sword or whatever
std::cout << "it happened!";
}
}
int main(int argc, constchar * argv[])
{
askOracle(0.90f);
cout << dropItems << endl;
return 0;
}
Could someone please tell me how I can make the dropItems work? I get a "1" but I don't get a "it happened" message.
Two things:
On line 38, you forgot the parentheses following your call to dropItems(). Also, you can't "cout" a void function, so get rid of the cout statement in general. Now, you also need to include this statement at the beginning of your main function, so that you get a different random number each time:
1 2 3 4 5 6
#include <ctime>
//...
int main() {
srand(time(0));
//...
}
You should, if you made the changes I suggested. Of course, remember, there is still the 10% or so chance that it doesn't happen. Here I have a link to the code, compiled and the results it gave me. http://ideone.com/gs1puf