Variable not passing between functions

The purpose of the below program is to roll a variable number of 8-sided die (specified when run in function combat), check if they meet the hit requirement (the input), and then output the number of hits. The problem is, when variable attack goes from where it is initialized in function combat to function roll (as variable h) and then back to combat (in output "Shots: ") it doesn't keep its value (see Returns: below the code).
Maybe I'm just missing something obvious, but in previous versions I had less in function roll and more in func combat and was able to output an entire array from roll() into combat() using arg[]. What is the problem? I put "Hits: " into the roll function twice, at the beginning and end, to make sure roll() wasn't the problem, so it can only be the transition that is at fault. I have no idea where the strange number output with "Shots: " comes from.
Thanks in advance!

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
81
82
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <time.h>

using namespace std;

int roll(int b, int v, int h, int e) {
/* b is die number, v is hit value, h is hits, e is used later in the program for special cases */
    srand(time(0));
    int x[6] = {};
    int y;
    h = 0;
    int z = 0;
    if (b == 0 or v == 0) {cout << "No Die\n";}
    else {// For Attack numbers are b = 6, v = _4_, h = 0, e = 0
        cout << "You Rolled: ";
        while (z < 6) {
// Stores rand integers to x[6] and shows numbers rolled up to x[b]
            x[z] = 1 + (rand() %8);
            if (z < b) { // Displays Roll
                cout << x[z] << ", ";
            } // End if
            z++;
        } // End while1
        cout << endl;
        z = 0;
        // Number of Hits
        while (z < b) { // Totaling Hits
            if (v - (x[z]) >= 0) {
                h++;
            } // End if
            else {}
            z++;
        } // End while2
        cout << "Hits: " << h << endl;
        /*
        ….
        Extraneous Code
        ….
        */
    } // End else
    z = 0;
    x[6] = {};
    cout << "Hits: " << h << endl;
    return 0;
}

int combat() {
    srand(time(0));
    /*
    ….
    Extraneous Code
    ….
    */
    int av, bv, dv, exp, attack, exphit, block, defend, dexphit;
    long dexp, dexpdie;
    int SMtab[3][3] = {{1,0,-1}, {0,-1,-2}, {1,0,-1}};
    int COtab[2] = {0,-1};
    int RMtab[3] = {1,0,-1};
    cout << "Enter Attack Value: ";
    cin >> av;
    int dieattack = (4 + SMtab[0][0] + RMtab[0] + COtab[0]);
    // Equivalent to 4 + 1 + 1 + 0 = 6
    int dieblock = (4 + SMtab[1][0] + RMtab[1] + COtab[0]);
    cout << endl; // Clarity
    cout << "Attack:\n";
    roll(dieattack, av, attack, 0);    // Attack Roll
    cout << "Shots: " << attack << "\n" << endl;
/*
    ....
    Extraneous Code
    ....
*/
}

int main()
{
    combat();
    return 0;
}


Returns:
Enter Attack Value: __3__

Attack:
You Rolled: 2, 6, 5, 1, 1, 8,
Hits: 3

Hits: 3
Shots: -711831741

Using cplusplus's C++ shell (cpp.sh) it outputs similar, but
cout << "Shots: " << attack
is always 0. This seems to make even less sense, because variable attack is not given a value in func combat, only in roll when it is set equal to var h. So why would it use the initial value of h (0) to output in func combat rather than the final value of h (0-6)?
Last edited on
Topic archived. No new replies allowed.