Search code examples
c++recursionreturn

warning C4715: not all control paths return a value c++ - cant pass a test


I'm having an issue with this code:

The problem is I'm constantly getting warning C4715 despite the fact .exe is running correctly and it gives correct answer to a problem I'm trying to resolve. The warning makes it impossible to pass the task inside the app. Please give me a clue why 'return' used by me in the if sentences doesn't work.

#include <utility>
#include <iostream>


    std::pair<int, int> solve(int a, int b) {
    
        if (a == 0 || b == 0) {
            std::pair <int, int> kek(a, b);
            return kek;
    
        }
        else if (a >= 2 * b) {
            a = (a - (2 * b));
            solve(a, b);
        }
        else if (b >= 2 * a) {
            b = (b - (2 * a));
            solve(a, b);
        }
        else {
            std::pair <int, int> kek(a, b);
            return kek;
        }
        
    }
    
    int main() {
        bool result{ solve(22, 5) == std::make_pair(0,1) };
        std::cout << result;
        return 0;
    }

Solution

  • Your solve function won't execute return statement if a == 0 || b == 0 is not true and either one of a >= 2 * b or b >= 2 * a is true.

    It seems that the two solve(a, b); in the solve function should be return solve(a, b);.