Not sure of function type, strange error

Hello all. I started making my own function for combat to use in a program I'm working on. The problem is, I'm not sure what type the function should be: void or int? And if it is int, what do I return? Also, I'm getting a strange error that reads as: "label must be followed by statement." Uh...what?

Here's my function thus far:
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
void combat()
    {
     do 
       {
       cout << "1) Attack\n";
       cout << "2) Defend\n";
       cout << "3) Run\n";
       switch (x)
       {
         case 1:
           cout << charclass << " does " << dam(str) << " damage!\n";
           enemhealth-=dam(str);
           cout << enemname << " Health: " << enemhealth << "\n";
           if (enemhealth<=0) goto victory1;
           cout << "Monster Attacks!\n";
           cout << enemname << " does " << enemdam(enemstr) << " damage!\n";
           health-=enemdam(enemstr);
           cout << charclass << " Health: " << health << "\n";
           break;
         case 2:
           cout << "You just stand there.\n";
           cout << "Monster Attacks!\n";
           cout << enemname << " does " << enemdam(enemstr) << " damage!\n";
           health-=enemdam(enemstr);
           cout << charclass << " Health: " << health << "\n";
           break;
         case 3:
           cout << "You tried to run, but the monster got you.\n";
           goto loop;
           break;
         default:
           cout << "Invalid\n";
           goto loop;
           break; 
       }
         if (health<=0)
         {
           cout << "You died.\n";
           goto loop;
         }
     }
     while (health > 0 && enemhealth > 0);
     victory1:
     xp+=1;
     cout << "You were Victorious!\n";
     cout << "Gained XP!\n";
     cout << xp << "/10 XP\n";
     loop:
    } 
Last edited on
The complaint is on line 48. loop is a label. It must have a statement after it, not a brace.

As for return code, it entirely depends on what you want the function to do.

It looks like the function returns when either the player or the enemy dies, so perhaps you want
the function to return true if the player is still alive and false if the player dies.

OH! *face palm*
That makes so much sense. I don't think I entirely understand what return is supposed to do, but that certainly helps. Mucho Kudos jsmith.
1
2
3
4
5
// This function returns x^2.
int square( int x ) {
    int result = x * x;
    return result;             // Tells the compiler that the value you want to result is stored in result
}


Without the "return" statement, the compiler would not know what value to return from the function to the caller.
Topic archived. No new replies allowed.