I've already researched this but I still don't understand clearly the issue. So, I have this code:
int is_negative(int a)
{
if(a < 0)
return a;
else while (a>=0)
{
cout << "Insert a negative value, please: ";
cin >> a;
if(a < 0)
return a;
}
}
This function is called in the main function because I want the user to enter a negative number, and to store the value only if it's negative. If it's not, I want it to loop until a negative one is entered.
Why am I getting the error :"Control reaches end of non void function [-Wreturn-type]"? Thanks! :)
PS. I have to use simple syntax like a while or a for loop (exercise requires to do so).
while(a >= 0), anything following that block implicitly hasa < 0, and ifa < 0, the loop is never entered. You can safely return your value as the closing line. If you like recursion, this wouldn't be a bad time for it; instead of looping, you can simply call the same function again:if(a >= 0) { ...; cin >> a; return is_negative(a); } return a;