I ran a C program in XcodeUpdate 5 and encountered an error

I ran a C++ program in Xcode(Update 5), and encountered an error warning saying \"Control may reach end of non-void function\". Here is the code:

int priority (char x)
{
     if (x == \'(\')
         return 0;
     if (x == \'+\' || x == \'-\')
         return 1;
     if (x == \'*\' || x == \'/\')
         return 2;
}

Solution

HI, Please find my explanation:

int priority (char x)
{
if (x == \'(\')
return 0;
if (x == \'+\' || x == \'-\')
return 1;
if (x == \'*\' || x == \'/\')
return 2;
}

Hi, Your function only returning value if x=\'(\' or x=\'+\' or x=\'-\' or x=\'*\' or x=\'/\'

So, what function returns if x value is other than these 5 values ??

So, you have to return some value for any value of x, thats why you are getting this warning.

You can do this:
int priority (char x)
{
if (x == \'(\')
return 0;
if (x == \'+\' || x == \'-\')
return 1;
if (x == \'*\' || x == \'/\')
return 2;

// if x value is other than those 5 value, then return -1
return -1;
}

I ran a C++ program in Xcode(Update 5), and encountered an error warning saying \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site