C++ getSmallest program

C++ getSmallest program

本文关键字:program getSmallest C++      更新时间:2024-05-10

我一直在获取函数定义,在int getSmallest(int numbers[],int SIZE(;之后的"{"是不允许的。我很难弄清楚如何修复它,也很难编译这个程序。这就是我目前所拥有的:

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
// Function prototypes
int getSmallest(int numbers[], int SIZE);
int main()
{
int count = 0;
int numbers[SIZE];
string inFile;
cout << "Enter input file name:";
cin >> inFile;
ifstream file(inFile);
//Reading from file
for (count = 0; count < SIZE; count++) {
cout << SIZE << "numbers read from file." << endl;
cout << "The smallest value is: " << getSmallest(numbers, SIZE) << endl;
}
}
int getSmallest(int numbers[], int SIZE)
{
smallest = numbers[0];
for (count = 1; count < SIZE; count++) {
if (numbers[count] < smallest) {
smallest = numbers[count];
}
return smallest;
}
}

问题出在您的函数中。变量smallestcount未定义。。。您没有指定类型。您在main中定义了它们,但您的函数对main中的变量一无所知。只是您传递的变量(数字和大小(。这样试试:

int getSmallest(int numbers[], int SIZE)
{
int smallest = numbers[0];
for (int count = 1; count < SIZE; count++) {
if (numbers[count] < smallest) {
smallest = numbers[count];
}
return smallest;
}
}

*注意smallestcount之前的int

我还注意到,这个函数在一次循环迭代后立即返回。您应该在循环的外部编写返回语句

int getSmallest(int numbers[], int SIZE)
{
int smallest = numbers[0];
for (int count = 1; count < SIZE; count++) {
if (numbers[count] < smallest) {
smallest = numbers[count];
}
}
return smallest;
}

此外,我不知道SIZE是否在任何头文件中的任何地方定义,但它并没有在您的程序中定义。

您也没有从文件中读取。也许这个链接将帮助您了解如何从文件中读取:http://www.cplusplus.com/doc/tutorial/files/