Control may reach end of non-void function

I'm getting an error from this function, "control may reach end of non-void function" So how Would I go about and fix this. This is how I call the function gettability: tie(action, mana) = getability(pclass, mana, inte, level);
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
// Displays the abilities
pair<string, int> getability(string pclass, int mana, int inte, int level) {
    string input;
    string ability;
    int cost;
    cout << "Choose an ability" << endl;
    if (pclass == "Champion") { // Champion Section
        cost = inte*(level);
        cout << "[1] Cleaving Strike [" << cost << " mana]" << endl;
        cout << "[2] Melting Thrust [" << cost << " mana]" << endl;
        cout << "[3] Critical Bash [" << cost << " mana]" << endl;
        cost = inte*(level+1);
        cout << "[4] Purify [" << cost << " mana]" << endl;
        cost = inte*(level);
        cin >> input;
        if (input == "1") {
            ability = "cleaving strike";
            if (mana > cost) {
                mana = mana - cost;
                return {ability, mana};
            }
        } else if (input == "2") {
            ability = "melting thrust";
            if (mana > cost) {
                mana = mana - cost;
                return {ability, mana};
            }
        } else if (input == "3") {
            ability = "critical bash";
            if (mana > cost) {
                mana = mana - cost;
                return {ability, mana};
            }
        } else if (input == "4") {
            ability = "purify";
            if (mana > cost) {
                cost = inte*(level+1);
                mana = mana - cost;
                return {ability, mana};
            }
        }
    }
}
its saying that if all the possible else if combos fail, you don't have a final default return statement. It could be that your code never has this problem; the compiler can't tell. It may be logically impossible for your code to not trip one of the conditions.

If you are sure you code always hits one condition, you can ignore it or add a bogus return statement to silence it. If it can have this issue, you need a valid return code for that condition and a way to handle it elsewhere in the code (probably?).

warning, your code is looking too hard coded for any sort of re-use or maintainable design.
Last edited on
A single catch-all at the end is the simplest.

Or - if there's a good reason to do so, go through and plug all the gaps:
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
pair<string, int> getability(string pclass, int mana, int inte, int level) {
    string input;
    string ability;
    int cost;
    cout << "Choose an ability" << endl;
    if (pclass == "Champion") { // Champion Section
        cost = inte*(level);
        cout << "[1] Cleaving Strike [" << cost << " mana]" << endl;
        cout << "[2] Melting Thrust [" << cost << " mana]" << endl;
        cout << "[3] Critical Bash [" << cost << " mana]" << endl;
        cost = inte*(level+1);
        cout << "[4] Purify [" << cost << " mana]" << endl;
        cost = inte*(level);
        cin >> input;
        if (input == "1") {
            ability = "cleaving strike";
            if (mana > cost) {
                mana = mana - cost;
                return {ability, mana};
            }
            else
            {
                // return something --------------------------------
            }
        } else if (input == "2") {
            ability = "melting thrust";
            if (mana > cost) {
                mana = mana - cost;
                return {ability, mana};
            }
            else
            {
                // return something --------------------------------
            }            
        } else if (input == "3") {
            ability = "critical bash";
            if (mana > cost) {
                mana = mana - cost;
                return {ability, mana};
            }
            else
            {
                // return something --------------------------------
            }            
        } else if (input == "4") {
            ability = "purify";
            if (mana > cost) {
                cost = inte*(level+1);
                mana = mana - cost;
                return {ability, mana};
            }
            else
            {
                // return something --------------------------------
            }            
        }
        else
        {
            // return something --------------------------------
        }        
    }
    else
    {
        // return something --------------------------------
    }
}
Thanks that fixed the problem! What is hard coded? Cause thats just my normal programming...
Last edited on
Why do you need so many return statements? As far as I can see they are all
return {ability,mana}
so you can simply put that single return at the end, outside all the if blocks.

mana comes in as an argument, but you would need some default ability.
The only problem with just having a single return is that I won't be able to call the function, more like it won't allow me to choose an ability.
I won't be able to call the function, more like it won't allow me to choose an ability.

I don't understand the problem. You would call the function and use the resulting value exactly as you do at present.

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
// Displays the abilities
pair<string, int> getability(string pclass, int mana, int inte, int level) {
    string input;
    string ability = "unknown";             // set default ability
    int cost;
    cout << "Choose an ability" << endl;
    if (pclass == "Champion") { // Champion Section
        cost = inte*(level);
        cout << "[1] Cleaving Strike [" << cost << " mana]" << endl;
        cout << "[2] Melting Thrust [" << cost << " mana]" << endl;
        cout << "[3] Critical Bash [" << cost << " mana]" << endl;
        cost = inte*(level+1);
        cout << "[4] Purify [" << cost << " mana]" << endl;
        cost = inte*(level);
        cin >> input;
        if (input == "1") {
            ability = "cleaving strike";
            if (mana > cost) {
                mana = mana - cost;
            }
        } else if (input == "2") {
            ability = "melting thrust";
            if (mana > cost) {
                mana = mana - cost;
            }
        } else if (input == "3") {
            ability = "critical bash";
            if (mana > cost) {
                mana = mana - cost;
            }
        } else if (input == "4") {
            ability = "purify";
            if (mana > cost) {
                cost = inte*(level+1);
                mana = mana - cost;
            }
        }
    }
    
    return {ability, mana};    
}


Notice line 4. If you should ever see the ability value "unknown" appear after calling the function, you would need to investigate the cause. At present, that situation is not handled at all and would quite likely cause a program crash.
Topic archived. No new replies allowed.