what is wrong with my randomizing a number?

im just trying to generate a number 1-4, declare a 'creature' for that number, and then display its stats

my error is on line 59 attempting to generate a number from 1 to 4 and put it into the variable 'enemy'
lines 60-63 are just going to display the stats based on the number generated
srand(time(0)); is in main()
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

class creature
{
    private:
        string name;
        int health; //each creatures health
        int attack; //each creatures attack value
    public:
        void disp_stat(); //each creatures sentences
        void detail_stat();
        void enemy_encounter(int enemy);
        int generate_random(int i); //create random creature encounter
        creature(int tempHealth = 0, int tempAttack = 0);
}small_goblin, large_goblin, small_orge, large_orge;

creature::creature(int tempHealth, int tempAttack)
{
    cout << "You are being attacked..." << endl;
    health = tempHealth;
}

void creature::disp_stat()
{
    detail_stat();
    cout << "**********" <<endl;
    cout << "Name: " << name <<endl;
    cout << "Health: " << health <<endl;
    cout << "Attack: " << attack <<endl;
    cout << "**********" <<endl <<endl;
}


void creature::detail_stat()
{
    small_goblin.name = "\tSmall Goblin";
    small_goblin.health = 5;
    small_goblin.attack = 2;

    large_goblin.name = "\tLarge Goblin";
    large_goblin.health = 10;
    large_goblin.attack = 5;

    small_orge.name = "\tSmall Orge";
    small_orge.health = 4;
    small_orge.attack = 3;

    large_orge.name = "\tLarge Orge";
    large_orge.health = 9;
    large_orge.attack = 6;
}

void creature::enemy_encounter(int enemy)
{
    int ans;
    generate_random(enemy);
        if(enemy == 1) small_goblin.disp_stat();
        if(enemy == 2)
        if(enemy == 3)
        if(enemy == 4)

    cout << "What do you want to do?" <<endl;
    cout << "1) Fight" <<endl;
    cout << "2) Run Away" <<endl;
    cout << "3) Plead Mercy" <<endl;
    cin >> ans;

    if (ans == 1) cout << "Are you sure you want to fight" <<endl;
    if (ans == 2) cout << "Are you sure you want to run" <<endl;
    if (ans == 3) cout << "Are you sure you want to plead" <<endl;
}

int generate_random(int i)//local variable i is returned
{
    i = (rand() % 4) + 1;
    return i;
}
Last edited on
If generate_random(int) returns an int, then you should have assigneg it to an int variable like:
int random_value = generate_random();

Annother question is, for what is in fuction generate_random(int) an int paramentr?
Let's see: int i is an parameter. Then to this i you assign a random value and return this i.
Make this fuction parameterless, I think it would be better.

This could resolve your problem with random numbers.

Greetings,


Topic archived. No new replies allowed.