#Control Reaches End of Non-void Function

28 messages · Page 1 of 1 (latest)

wraith condor
#

Hello! I'm working on a school project and running into an issue where I can't execute my code due to this error message. I can include other pieces of the code if it's helpful, but I think this is the only part that's relevant. Can anyone help me find what's causing it? Thanks o7

float setnet(string nettype, int days) {
    system ("CLS");
    cout << "\t\t Internet Accessed (Y/N): "; getline(cin, nettype);
    nettype = toupper(nettype[0]);

    if (nettype == "N")
    nettype = "No Internet";
    return NONE;

    if (nettype == "Y") {
        cout << "\t\tAccess\n\n";
        cout << "\t1- Wired\n";
        cout << "\t2- Not-Wired (Wireless)\n";
        cout << "\t\tEnter choice 1 or 2:"; getline(cin, nettype);
        nettype = toupper(nettype[0]);
        
        if (nettype == "1", "W", "w") {
            nettype = "Wired";
            return days * WIRED;
        }

        else if (nettype == "2", "N", "n") {
            nettype = "Wireless";
            return days * WIRELESS;
        }
        
        
    }
}```
quaint ventureBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

floral forum
#

That just means you got to the end of the function without returning anything

#

since the function has to return something, that's an error

#

(well, undefined behavior, so it still compiles, but it shouldn't be used)

elfin urchin
#
    if (nettype == "N")
    nettype = "No Internet";
    return NONE;
``` wouldn't it always return `NONE`?
wraith condor
#

I can't get it to run without fixing this first so I'm afraid I can't actually test if it's returning NONE or not atm

#

But based on the above

#

I guess it's not returning anything

graceful comet
#

It's complaining because there's a case that's not covered. What if nettype is not "1" and is also not "2"?

#

also, why are you passing in a string and then immediately overwriting it?

#

I'll stop there, I see more issues

wraith condor
#

For my project I have to use prewritten code for int main() and then fill out functions so I can't actually add more variables to the function to avoid overwriting it which is what I would've done if I wrote it myself 💔

#

I'll try to account for the edge case and reprot back

#

Okay so as far as I can tell it didn't seem to change anything when I added a trailing else

floral forum
#

what happens if the first input is neither a "N" nor a "Y"? You have the same issue twice

empty cliff
#

Nvm none of the other messages were loading

wraith condor
#

It's alright I hadn't seen what you sent

#

I've tried adding these and still no luck:

#
// Creates a menu that allows the user to input their Internet preferences and returns the price of their chosen plan.

float setnet(string nettype, int days) {
    system ("CLS");
    cout << "\t\t Internet Accessed (Y/N): "; getline(cin, nettype);
    nettype = toupper(nettype[0]);

    if (nettype == "N") {
        nettype = "No Internet";
        return NONE;
    }

    else if (nettype == "Y") {
        cout << "\t\tAccess\n\n";
        cout << "\t1- Wired\n";
        cout << "\t2- Not-Wired (Wireless)\n";
        cout << "\t\tEnter choice 1 or 2:"; getline(cin, nettype);
        nettype = toupper(nettype[0]);
        
        if (nettype == "1", "W", "w") {
            nettype = "Wired";
            return days * WIRED;
        }

        else if (nettype == "2", "N", "n") {
            nettype = "Wireless";
            return days * WIRELESS;
        }
    
        else {
            return NONE;
        }
    }
    else {
        return NONE;
    }
}```
empty cliff
#

That looks like it should work to me

#

Do you get the error from your IDE or from compiling?

wraith condor
#

From compiling

#

It appears specifically on the first of the else - return NONE;s

#

I'm using gcc in vscode

empty cliff
#

Can you show the error output exactly

slate flicker
#

same with nettype == "2", "N", "n" obviously