简体   繁体   English

C ++:运行程序时出现分段错误(从文件中输入数字到数组)

[英]C++: Getting a Segmentation Fault when I run my Program (inputting numbers from a file into arrays)

I'm trying to input numbers from a text file. 我正在尝试从文本文件中输入数字。 What I need to do is sort the numbers into an array that stores odd numbers, as well as one that stores even ones. 我需要做的是将数字排序为存储奇数的数组,以及存储偶数的数组。 From there, I just need to output the average, total, min, max, and then the numbers in the array that are greater than the average. 从那里,我只需要输出平均值,总数,最小值,最大值,然后输出数组中大于平均值的数字。

From researching, it's my understanding that a seg. 从研究来看,这是我对seg的理解。 fault occurs because of problems with memory allocation. 由于内存分配问题而发生故障。 I checked my arrays, and I don't think there should be a problem. 我检查了我的阵列,我不认为应该有问题。 The total amount of numbers in the file is 50, and so there should be room for the numbers, with what I allocated for my arrays in the beginning of main. 文件中的总数量是50,因此数字应该有空间,我在主数据开头为数组分配了数字。

Here is my input file: 这是我的输入文件:

46 30 82 90 56 17 95 16 48 26 4 58 0 78 92 60 12 21 63 47 19 41 90 85 14 -9 52 71 79 16 80 51 95 102 34 10 79 95 61 92 89 88 66 64 92 63 66 64 39 5 46 30 82 90 56 17 95 16 48 26 4 58 0 78 92 60 12 21 63 47 19 41 90 85 14 -9 52 71 79 16 80 51 95 102 34 10 79 95 61 92 89 88 66 64 92 63 66 64 39五

I am using Linux, by the way. 顺便说一下,我正在使用Linux。

#include <iostream>
#include <fstream>

using namespace std;

void oddOrEven(int [], int [], int [], ifstream &, int &, int &, int &);
void calcData(int [], int [], double &, double &, double &, double &, double &, int &, 
int &);

int main()
{

int i;
int a;
int b;
double totalEven = 0;
double totalOdd;
double average = 0;
double max = 0;
double min = 0;
int array[75] = {0};
int evenArray[50] = {0};
int oddArray[50] = {0};

ifstream fileIn;

fileIn.open("file.txt");

if (!fileIn){
cout << "\nError, could not open file.\n";
}

oddOrEven(array, evenArray, oddArray, fileIn, i, a, b);

calcData(evenArray, oddArray, totalEven, totalOdd, average, min, max, a, b);

fileIn.close();

return 0;

}

void oddOrEven(int array[], int evenArray[], int oddArray[], ifstream & fileIn, int & i, 
int & a, int & b){

while (fileIn >> i) {

fileIn>>array[i];

if (array[i] % 2 == 0){
evenArray[a]=array[i];
a++;
}

else{

oddArray[b] = array[i];
b++;

}}}

void calcData(int evenArray[], int oddArray[], double & totalEven, double & totalOdd,  
double & average, double & min, double & max, int & a, int & b){

//Find min, max, total and average for Even Array.

for (int i = 0; i < a; i++){

totalEven += evenArray[i];

if (i = 0){
min = evenArray[i];
max = evenArray[i];
}

if (evenArray[i] < min)
min = evenArray[i];

if (evenArray[i] > max)
max = evenArray[i];

}

average = totalEven/a;

cout << "\nMinimum is " << min;
cout << "\nMaximum is " << max;
cout << "\nTotal is " << min;
cout << "\nAverage is " << average;

//Numbers higher than the average for Even Array:

for (int k = 0; k < a; k++){
if (evenArray[k] > average/2)
cout << evenArray[k] << " ";
}

//Find min, max, total and average for Odd Array.

for (int i = 0; i < a; i++){

totalOdd += oddArray[i];

if (i = 0){
min = oddArray[i];
max = oddArray[i];
}

if (oddArray[i] < min)
min = oddArray[i];

if (oddArray[i] > max)
max = oddArray[i];

}

average = totalOdd/b;

cout << "\nMinimum is " << min;
cout << "\nMaximum is " << max;
cout << "\nTotal is " << min;
cout << "\nAverage is " << average;

//Numbers higher than the average for Odd Array:

for (int k = 0; k < b; k++){
if (oddArray[k] > average/2)
cout << oddArray[k] << " ";}
}

You use i , a and b to index into your arrays, but you never initialize them. 使用iab索引数组,但是从不初始化它们。

Also, the if (i = 0) should read if (i == 0) (a single = is assignment, not comparison). 此外, if (i = 0)应该读取if (i == 0) (单个=是赋值,而不是比较)。

I didn't read the code all that closely. 我没有仔细阅读所有代码。 There are probably other errors. 可能还有其他错误。

A couple of general recommendations: 一些一般性建议:

  1. When your code crashes, run it in a debugger. 当代码崩溃时,在调试器中运行它。 This will pinpoint the location of the crash, and will let you examine the state of the program. 这将精确定位崩溃的位置,并让您检查程序的状态。 This is often enough to understand the root cause of the problem. 这通常足以理解问题的根本原因。

  2. Use std::vector instead of raw arrays. 使用std::vector而不是原始数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM