C++ getSmallest program
C++ getSmallest program
我一直在获取函数定义,在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;
}
}
问题出在您的函数中。变量smallest
和count
未定义。。。您没有指定类型。您在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;
}
}
*注意smallest
和count
之前的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/
相关文章:
- Monty Hall program c++
- "Program ended with exit code: 0"出现在我的输出的开头?
- LCM program in C++
- 为什么 C++ 编译器在"c:program filesgnu emacsinclude"中查找包含文件?
- 在 EAX 模式下加密数据时"program has stopped working"
- 为什么删除执行组件指针会导致"Program.exe has triggered a breakpoint"
- for(;;) vs do..while() for main program loop
- C++ "Program has stopped working" - ROT-107(凯撒密码)
- OS X Eclipse C++ "Program File Does Not Exist"
- 在 Eclipse.It 中运行C++代码时出错,'Your program cannot output..'
- 如何设置Visual Studio来调试DLL ("Unable to start program error")
- 标准Windows 7用户如何能够删除C:Program Files下的文件
- xlC 警告,"The NOSTRICT option has the potential to alter the semantics of a program"
- 树莓派"error: stray 255 in program"
- 错误"<url> is not recognized as an internal or external command, operable program or batch file.
- 错误 PRJ0002:错误结果 -1073741515 从"C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.
- 很多杂散错误 - "error: stray ‘210’ in program in C++"
- free.c 抛出异常并带有"this program has stopped working"
- Ubuntu SDL program
- C++排序程序中"Stray 1 in program"错误