References and Functions

So I just learned how to put use references and functions together. Put I keep getting a problem.
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
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;

int lunge(int& attack_hit);
int stab(int& attack_hit);
int slash(int& attack_hit);

int main()
{
    
    string attack;
    cout << "Please choose an attack options: ";
    cout << "Lunge(l)" << endl;
    cout << "Slash(sl)" << endl;
    cout << "Stab (s)" << endl;
    cin >> attack;
    
    
    if (attack == "l")
    {
               int l = lunge(int& attack_hit);
               cout << "You hit a " << l << endl;
    }
    
    
    if (attack == "sl")
    {
               int sl = slash(int& attack_hit);
               cout << "You hit a " << sl << endl;
    }
    
    
    if (attack == "s")
    {
               int s = stab(int& attack_hit);
               cout << "You hit a " << s << endl;
    }
    
    else
    cout << "You can't do that";
    
    system("pause");
    return 0;
}

int lunge(int& attack_hit)
{
    srand(time(0));
    int hit = rand();
    int x = attack_hit;
    attack_hit = (hit % 30) + 1;
    return x;;
}

int slash(int& attack_hit)
{
    srand(time(0));
    int hit = rand();
    int x = attack_hit;
    attack_hit = (hit % 30) + 1;
    return x;;
}

int stab(int& attack_hit)
{
    srand(time(0));
    int hit = rand();
    int x = attack_hit;
    attack_hit = (hit % 30) + 1;
    return x;
}


Any advice ?
Last edited on
You are not calling your functions correctly. Calling a function with a reference as a parameter is not really any different than any other type of function; just like srand(), you shouldn't be putting the type inside the parentheses when calling your functions.
You are trying to convert a string into an int implicitly. That is a no-go in C++. You can use atoi to convert it but it's not something a newbie should be doing.
I would use an enumerated type that defines what lunge, slash, and stab are as integers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

enum Attack { LUNGE = 10, SLASH = 8, STAB = 15 };

  if (attack == "l")
    {
               int l = lunge(LUNGE);
               cout << "You hit a " << l << endl;
    }
    
    
    if (attack == "sl")
    {
               int sl = slash(SLASH);
               cout << "You hit a " << sl << endl;
    }
    
    
    if (attack == "s")
    {
               int s = stab(STAB);
               cout << "You hit a " << s << endl;
    }


Topic archived. No new replies allowed.