Control may reach end of non void function

I am getting the error message that the control may reach end of non void function and not exactly sure what is wrong.

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
 double LinkedListStudent::searchStudent(string name)
{
    int position = 0;
    bool flag = false;
    if (start == NULL)
    {
        cout << "List is empty." << endl;
        return -1;
    }
    else
    {
        struct createNode *N;
        N = start;
        while (N != NULL)
        {
            position++;
            if (N->Name == name)
            {
                flag = true;
                return position;
            }
            N = N -> next;
        }
        if (!flag)
        {
            cout << "Element " << name << " was not found." << endl;
            return -1;
        }
    }
}
If the list is not empty we get inside the else loop and within that loop if(flag) what is the return value?
Each node has a name and a GPA. I am trying to return the GPA of the name that I search for. So like this: (i get same error)

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
double LinkedListStudent::searchStudent(string name)
{
    int position = 0;
    bool flag = false;
    if (start == NULL)
    {
        cout << "List is empty." << endl;
        return -1;
    }
    else
    {
        struct createNode *N;
        N = start;
        while (N != NULL)
        {
            position++;
            if (N->Name == name)
            {
                flag = true;
                return N->GPA;
            }
            N = N -> next;
        }
        if (!flag)
        {
            cout << "Element " << name << " was not found." << endl;
            return -1;
        }
    }
}
in other words : if line 17 is false, what happens?

Avoid using NULL, nullptr was invented as a better alternative.

Pass std::string by reference:

1
2
3
4
5
double LinkedListStudent::searchStudent(const std::string& name) const

{

}
If line 17 is false, it should move to the next node N = N -> Next and then run the loop until if the if statement is true. If it never is true it should exit and check the if statement following the loop. Obviously I'm missing something obvious right now, but I cant get my head to come up with it.
The if statement here:
1
2
3
4
5
        if (!flag)
        {
            cout << "Element " << name << " was not found." << endl;
            return -1;
        }


What happens when the condition is false? You could add an else clause and return whatever value is appropriate.


Or just before the closing brace at the end of the function, add a statement which returns some default value.

It may be that if all the data and logic in the rest of the program is working correctly, that additional return statement should never be executed. However the compiler doesn't know that, it simply sees that under some circumstance the function may end without returning a proper value.

Edit:
Actually, thinking about it , the bool flag is not really needed. If the end of the while loop is reached without finding a match and returning a result, then the element was not found. There's no need for an if, or an if/else. Just unconditionally output the message and return -1.

Last edited on
Thanks for help. If I had else so that it looks like this:

1
2
3
4
5
6
7
        if (!flag)
        {
            cout << "Element " << name << " was not found." << endl;
            return -1;
        }
        else
            return -1;


the error goes away, but I get a "Apple Mach-O Linker (Id) Error"
That's a linker error, it means something was not defined. Usually the message should contain more information about exactly what it was that could not be found.

By the way, the function could be simplified to this, though it doesn't distinguish between empty and not found:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double LinkedListStudent::searchStudent(string name)
{
    struct createNode *N = start;
    
    while (N != nullptr)
    {
        if (N->Name == name)
        {
            return N->GPA;
        }
        N = N->next;
    }
    
    cout << "Element " << name << " was not found." << endl;
    return -1;
}



Okay thanks. I got the function to run. Just another question. Is return N->GPA correct? I want the GPA that is in the same node as the searched name to print, but nothing prints when the function runs. Sorry, I should know this, but I'm not the best programmer... I would call my print function but I cant do that as my professor wants the search function to be a double. Thanks!
I got it. Thanks for the help!
Is return N->GPA correct?

It seems ok - without seeing the rest of the code that's just a guess.

Since the search function returns a double, then it is the responsibility of the code which calls it to display the result. Possibly all the cout statements should be removed from the search function, and instead test the returned value, if it -1, print "not found" else print the value.

(But I don't know what your professor would want).
Topic archived. No new replies allowed.