Not sure how to make this function work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace 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, const char * 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.

It's from this tutorial: http://noobtuts.com/programming/c-plus-plus/ask-oracle
Last edited on
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));
    //...
}
Thanks for correcting me.

However, how do I get the "it happened" message to appear?
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
Ah ha, thanks man.
Topic archived. No new replies allowed.