【发布时间】:2017-02-03 03:19:24
【问题描述】:
我做了一个函数来检查数组是否有负数,然后返回值;它将int testArray[] 作为第一个参数,int n=14 作为数组大小。我使用了一个 for 循环来遍历数组。我正在使用if 语句来比较testArray[i]<0,并且我有一个else 语句来打印一条消息,指出没有找到负数。代码编译没有错误,但我没有输出。我收到警告:
In function 'int countNegative(int*, int)':
28:1: warning: control reaches end of non-void function [-Wreturn-type]
我怀疑参数传递给函数的方式可能存在问题。
#include <iostream>
#include <cstdlib>
using namespace std;
int countNegative(int testArray[],int n);
int main(){
int testArray[] = {-2,0,44,12,-45,17,934,-21,67,88,91,1,0,6};
int n = 14;
countNegative(testArray,n);
system("PAUSE");
//EXIT_SUCCESS;
return 0;
}
int countNegative(int testArray[],int n){
for(int i=0; i<n; i++){
if(testArray[i]<0){
int index = testArray[i];
return index;
}
else{
cout << "No Negative Numbers";
}
}
}
【问题讨论】:
标签: c++ arrays function for-loop control-flow