Array/integer comparison error

I'm trying to write a practice program that creates a character for a pen and paper rpg. Here is my problem code:

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
int a,b,c,d,e,f;
a = rand() % 15 + 3;
b = rand() % 15 + 3;
c = rand() % 15 + 3;
d = rand() % 15 + 3;
e = rand() % 15 + 3;
f = rand() % 15 + 3;
int attributes[] = {a,b,c,d,e,f};
while(1)
        {
          cout << "Assign Strength: " << endl;
          cin >> str;
          for(int i=0; i<=6; i++)
          {
            if(attributes[i]==str)
            {
              attributes[i]=0;
              break;
            }
            else if (i==6)
            {
              cout << "Ability score doesn't exist. Pick a valid option." << endl;
              break;
            }
          }
          if(i==6)
          {
            continue;
          }
          else
          {
            break;
          }
        }


I get the errors:

warning: pointer to a function used in arithmetic
error: ISO C++ forbids comparison between pointer and integer
warning: pointer to a function used in arithmetic
error: invalid operands of types ‘void(int&, int&, int&, int&, int&, int&)’ and ‘int’ to binary ‘operator*’
error: in evaluation of ‘operator*=(void(int&, int&, int&, int&, int&, int&), int)’


Go ahead and say it's horrible code but at least correct me :)
Thanks in advance.
There's an error in the logic in your for loop. the "else if (i == 6)" may never get called when it should, and it indexes out-of-range memory which could cause a segfault. Second problem, "i" is out of scope when you use it in the if statement at the end of the loop. I would suggest a solution but it's probably best to just re-write it entirely.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int attributes[6];
for (int i = 0; i < 6; ++i) attributes[i] = (rand() % 15) + 3; //use a for loop!, 6 less lines

int str = 0;
bool str_valid = false;
do {
    cout << "Assign Strength: " << endl;
    cin >> str;
    for (int i = 0; i < 6; ++i) {
        if (str == attributes[i] && attributes[i] != 0) {
            attributes[i] = 0;
            str_valid = true;
            break;
        }
    }
    if (!str_valid)
        cout << "Ability score doesn't exist. Pick a valid option." << endl;
} while (!str_valid);


While you can make it work your way, just using a temporary "is it valid" tracking variable makes the code a lot cleaner in this case.
Thanks!
It had never occurred to me to use a bool like that. And I guess I needed reminding about for loops...
Topic archived. No new replies allowed.