C++ &引用;运行时检查错误“2-围绕变量堆叠”;arr";“腐败了”;当我使用一个有很多数字的文件时
这应该是从一个文件中最多读取1000个数字到一个数组中,然后对它们进行分析。除非我使用一个有一百万个数字的文件,否则它工作得完美无缺。我可能是因为我有一个无限循环,但我找不到在哪里。我不应该超过1000个元素C++ &引用;运行时检查错误“2-围绕变量堆叠”;arr";“腐败了”;当我使用一个有很多数字的文件时,c++,arrays,loops,C++,Arrays,Loops,这应该是从一个文件中最多读取1000个数字到一个数组中,然后对它们进行分析。除非我使用一个有一百万个数字的文件,否则它工作得完美无缺。我可能是因为我有一个无限循环,但我找不到在哪里。我不应该超过1000个元素 #define _USE_MATH_DEFINES #include <cmath> #include <iostream> #include <iomanip> #include <fstream> #include <string&
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
const long N=1000;
using namespace std;
//declaring the functions
int readArray(ifstream& ifile, long arr[]);
void sortArray(long arr[], int numberInTheArray);
int main()
{
//variable declaration
int n=0;
int i=0;
int numberInTheArray=0;
long minimum=0;
long maximum=0;
long arr[N]={0};
ifstream ifile;
string strVar;
cout << "Input File Name: ";
cin >> strVar;
cout << "Which number do you want to return? ";
cin >> i;
cout << endl;
ifile.open(strVar.c_str());
if(!ifile)
{
cout << "That file does not exist!" << endl;
return 1;
}
numberInTheArray = readArray(ifile,arr);
if (numberInTheArray == 0){
cout << "The file is empty!" << endl;
}
else{
maximum = arr[n];
minimum = arr[n];
n++;
while (n<=N){
if (arr[n] <= minimum){
minimum = arr[n];
}
if (arr [n] >= maximum){
maximum = arr[n];
}
n++;
}
cout << "Before Sort:\n" << " Min is {" << minimum << "}.\n" << " Max is {"
<< maximum << "}.\n";
if (i>N){
cout << " " << i << " is bigger than 1000!" << endl;
}
else if (numberInTheArray < N){
cout << " WARNING: Only " << numberInTheArray
<< " numbers were read into the array!" << endl;
if (i <= numberInTheArray){
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
else {
cout << " There aren't that many numbers in the array!" << endl;
}
}
else {
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
sortArray(arr,numberInTheArray);
cout << "\nAfter Sort:\n" << " Min is {" << minimum << "}.\n" << " Max is {"
<< maximum << "}.\n";
if (i>N){
cout << " " << i << " is bigger than 1000!" << endl;
}
else if (numberInTheArray < N){
cout << " WARNING: Only " << numberInTheArray
<< " numbers were read into the array!" << endl;
if (i <= numberInTheArray){
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
else {
cout << " There aren't that many numbers in the array!" << endl;
}
}
else {
cout << " Value " << i << " is {" << arr[i-1] << "}." << endl;
}
}
return 0;
}
int readArray(ifstream& ifile, long arr[])
{
int n=0;
long num;
while (n<=N && ifile){
ifile >> arr[n];
n++;
}
n=n-1;
return n;
}
void sortArray(long arr[], int numberInTheArray)
{
int a;
int b;
int temp;
for (a=0; a<numberInTheArray; a++)
{
for (b=0; b<numberInTheArray; b++)
{
if (arr[a] < arr[b])
{
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}
}
}
定义使用数学定义
#包括
#包括
#包括
#包括
#包括
常数长N=1000;
使用名称空间std;
//声明函数
int readArray(ifstream&ifile,long arr[]);
空数组(长数组[],整数数组);
int main()
{
//变量声明
int n=0;
int i=0;
int numbernthearray=0;
长最小值=0;
长最大值=0;
长arr[N]={0};
IFIFile;
字符串strVar;
cout>strVar;
cout>i;
cout问题在于循环条件n