Probably a problem in recursion

Back to Problem Solutions forum

Snow_Owl     2019-01-09 12:08:20

The problem is as follows:

Problem #24: Neumann's Random Generator

I solved the problem and in Visial Studio 2017 everything works without errors. After adding the code to the site and generating the error:

solution.cc: In function ‘int Loops(int)’:solution.cc:44:1: error: control reaches end of non-void function [-Werror=return-type] } ^cc1plus: some warnings being treated as errors

Tell me please, maybe I don’t notice errors somewhere or what am I doing wrong? Here is the code:

...
int Loops(int value) {
   int EndCalculation = 0;
   EndCalculation = CutTwoNumbers_BeginningAndEnd(selfMultiplication(value));
   LoopResultsArray.push_back(EndCalculation);

   if (FindRepeatInLoopResult(EndCalculation)) {
      return (LoopResultsArray.size() - 1);
   }
   else {
      Loops(EndCalculation);
   }
}
...
Rodion (admin)     2019-01-09 15:01:07
User avatar

Hi there!

C/C++ is a language with many compilers and they have usually different set of rules, that's why some things could be treated differently by different compilers...

In your case the message tells us:

In function ... int Loops(int) ... control reaches end of non-void function

It also gives us a line number (and position in line) in form of 44:1. So matter is that the function ends without return but as it has a type other than void it should return some value.

I think it is about return in else branch...

Compiler treats it usually as warning, not error (and function just returns something unpredictable), but the compiler used by checker on this site has switched "warnings as errors" behavior, to help easier find out such minor mistakes, as they may cause very bewildering program output... :)

Snow_Owl     2019-01-10 07:22:50

Yes Yes Yes!))) As I woke up, I thought that I should go to the forum. I started to remember the code and it dawned on me. I am right in my head I saw a line with an error!) A silly mistake through carelessness)). Thank you very much for the explanation!)

Please login and solve 5 problems to be able to post at forum