如何解决这个警告问题?
#include <iostream.h>
#include <stdlib.h>
typedef char datatype;
int binerysearch(datatype a[],datatype goal,int low,int hight)
{
int mid=(low+hight)/2;
datatype midvalue=a[mid];
if(midvalue==goal) return mid;
else if(goal <midvalue&&low!=hight)
binerysearch(a,goal,low,mid-1);
else if(goal> midvalue&&low!=hight)
binerysearch(a,goal,mid+1,hight);
else
{
cout < < "No that number!! " < <endl;
return -1;
}
}
void main()
{
int i;
char a[6]={ 'a ', 'b ', 'c ', 'd ', 'e ', 'f '};
i=binerysearch(a, 'c ',0,5);
cout < <i < <endl;
}
warning C4715: 'binerysearch ' : not all control paths return a value
[解决办法]
int binerysearch(datatype a[],datatype goal,int low,int hight)
{
int mid=(low+hight)/2;
datatype midvalue=a[mid];
if(midvalue==goal) return mid;
else if(goal <midvalue&&low!=hight)
binerysearch(a,goal,low,mid-1);
else if(goal> midvalue&&low!=hight)
binerysearch(a,goal,mid+1,hight);
else
{
cout < < "No that number!! " < <endl;
return -1;
}
return 0;//add
}
[解决办法]
//这样
#include <iostream>
#include <cstdlib>
using namespace std;
typedef char datatype;
int binerysearch(datatype a[], datatype goal, int low, int hight)
{
int mid=(low + hight)/2;
datatype midvalue = a[mid];
if(midvalue == goal)
return mid;
else if( goal < midvalue && low != hight)
return binerysearch(a, goal, low, mid-1);
else if(goal > midvalue && low!=hight)
return binerysearch(a, goal, mid+1, hight);
else
{
cout < < "No that number!! " < <endl;
return -1;
}
}
void main()
{
int i;
char a[6]={ 'a ', 'b ', 'c ', 'd ', 'e ', 'f '};
i = binerysearch(a, 'b ',0,5);
cout < <i < <endl;
}
[解决办法]
DonaldKnuth 正解,
[解决办法]
int binerysearch(datatype a[],datatype goal,int low,int hight)
{
int mid=(low+hight)/2;
datatype midvalue=a[mid];
if(midvalue==goal) return mid;
else if(goal <midvalue&&low!=hight)
return binerysearch(a,goal,low,mid-1);
else if(goal> midvalue&&low!=hight)
return binerysearch(a,goal,mid+1,hight);
else
{
cout < < "No that number!! " < <endl;
return -1;
}
}