v[s]; } } 无效计算百分比(整数v[],双p[],整数和总和){ int-total=0; 对于(int i=0;i>n[s]>>v[s]; } },c++,segmentation-fault,coredump,C++,Segmentation Fault,Coredump" /> v[s]; } } 无效计算百分比(整数v[],双p[],整数和总和){ int-total=0; 对于(int i=0;i>n[s]>>v[s]; } },c++,segmentation-fault,coredump,C++,Segmentation Fault,Coredump" />

运行我的代码时出现分段错误(内核转储)问题 我正在做的一个作业,我不知道为什么我在尝试运行程序时,它会产生一个错误:“分段错误(内核转储)。已经尝试了一段时间,我看不出我出了什么地方。我研究了分段错误,逐行地通过了我的代码,但我仍然不知道。在C++中,编码还是很新的。 //This program reads the last names of the candidates and the number of votes they receive. The\ program sorts and outputs the candidates based on votes received in descending order. #include <fstream> #include <iostream> #include <iomanip> #include <string> using namespace std; void openFile(ifstream&); void getData(ifstream&, string [], int [], int &); void computePercentages(int[], double [], int &); void sortVotes(string [], int [], double [], int); int findSmallestIndex(const int[], int, int); const int SIZE = 30; int main(){ string name[SIZE]; double percentages[SIZE]; int votes[SIZE]; int size, sum; ifstream in; openFile(in); getData(in, name, votes, size); computePercentages(votes, percentages, sum); sortVotes(name, votes, percentages, size); for(int i = 0; i < size; i++) cout << name[i] << " " << votes[i] << " " << setprecision(4) << percentages[i] << endl; in.close(); return (0); } void openFile(ifstream& in){ string filename; cout << "Enter file name: "; cin >> filename; in.open(filename.c_str()); } void getData(ifstream& in, string n[], int v[],int& s){ int x = 0; for (int s = 0; s < SIZE; s++){ n[s] = x; v[s] = x; } in >> n[s] >> v[s]; while (!in.eof()){ s++; in >> n[s] >> v[s]; } } void computePercentages(int v[], double p[], int& sum){ int total = 0; for (int i = 0; i < SIZE; i++) total = total + v[i]; double factor = 100.0 / total; for(int i = 0; i < SIZE; i++) p[i] = factor * v[i]; } void sortVotes(string n[], int v[], double p[],int s){ for(int index = 0; s - 1; index++){ int smallestIndex = findSmallestIndex(v, s, index) swap (n[index], n[smallestIndex]); swap (v[index], v[smallestIndex]); swap (p[index], p[smallestIndex]); }} int findSmallestIndex(const int v[], int s, int start){ int smallestIndex = start; for (int i = start +1; i < s; i++) if (v[i] < v[smallestIndex]) smallestIndex = i; return smallestIndex; } void swap(string &a, string &b){ string temp = a; a = b; b = temp; } void swap(int &a, int &b){ int temp = a; a = b; b = temp; } void swap(double &a, double &b){ double temp = a; a = b; b = temp; } //此程序读取候选人的姓氏和他们获得的票数\ 程序根据收到的选票按降序排列并输出候选人。 #包括 #包括 #包括 #包括 使用名称空间std; void openFile(ifstream&); void getData(ifstream&,string[],int[],int&); 无效计算百分比(int[],double[],int&); void sortVotes(字符串[],整数[],双精度[],整数); int findsmalletindex(常量int[],int,int); 常数int SIZE=30; int main(){ 字符串名称[大小]; 双百分比[大小]; 整数投票[大小]; 整数大小,和; 如果输入; openFile(in); getData(输入、名称、投票、大小); 计算百分比(票数、百分比、总和); sortVotes(名称、投票、百分比、大小); 对于(int i=0;i>v[s]; } } 无效计算百分比(整数v[],双p[],整数和总和){ int-total=0; 对于(int i=0;i>n[s]>>v[s]; } }

运行我的代码时出现分段错误(内核转储)问题 我正在做的一个作业,我不知道为什么我在尝试运行程序时,它会产生一个错误:“分段错误(内核转储)。已经尝试了一段时间,我看不出我出了什么地方。我研究了分段错误,逐行地通过了我的代码,但我仍然不知道。在C++中,编码还是很新的。 //This program reads the last names of the candidates and the number of votes they receive. The\ program sorts and outputs the candidates based on votes received in descending order. #include <fstream> #include <iostream> #include <iomanip> #include <string> using namespace std; void openFile(ifstream&); void getData(ifstream&, string [], int [], int &); void computePercentages(int[], double [], int &); void sortVotes(string [], int [], double [], int); int findSmallestIndex(const int[], int, int); const int SIZE = 30; int main(){ string name[SIZE]; double percentages[SIZE]; int votes[SIZE]; int size, sum; ifstream in; openFile(in); getData(in, name, votes, size); computePercentages(votes, percentages, sum); sortVotes(name, votes, percentages, size); for(int i = 0; i < size; i++) cout << name[i] << " " << votes[i] << " " << setprecision(4) << percentages[i] << endl; in.close(); return (0); } void openFile(ifstream& in){ string filename; cout << "Enter file name: "; cin >> filename; in.open(filename.c_str()); } void getData(ifstream& in, string n[], int v[],int& s){ int x = 0; for (int s = 0; s < SIZE; s++){ n[s] = x; v[s] = x; } in >> n[s] >> v[s]; while (!in.eof()){ s++; in >> n[s] >> v[s]; } } void computePercentages(int v[], double p[], int& sum){ int total = 0; for (int i = 0; i < SIZE; i++) total = total + v[i]; double factor = 100.0 / total; for(int i = 0; i < SIZE; i++) p[i] = factor * v[i]; } void sortVotes(string n[], int v[], double p[],int s){ for(int index = 0; s - 1; index++){ int smallestIndex = findSmallestIndex(v, s, index) swap (n[index], n[smallestIndex]); swap (v[index], v[smallestIndex]); swap (p[index], p[smallestIndex]); }} int findSmallestIndex(const int v[], int s, int start){ int smallestIndex = start; for (int i = start +1; i < s; i++) if (v[i] < v[smallestIndex]) smallestIndex = i; return smallestIndex; } void swap(string &a, string &b){ string temp = a; a = b; b = temp; } void swap(int &a, int &b){ int temp = a; a = b; b = temp; } void swap(double &a, double &b){ double temp = a; a = b; b = temp; } //此程序读取候选人的姓氏和他们获得的票数\ 程序根据收到的选票按降序排列并输出候选人。 #包括 #包括 #包括 #包括 使用名称空间std; void openFile(ifstream&); void getData(ifstream&,string[],int[],int&); 无效计算百分比(int[],double[],int&); void sortVotes(字符串[],整数[],双精度[],整数); int findsmalletindex(常量int[],int,int); 常数int SIZE=30; int main(){ 字符串名称[大小]; 双百分比[大小]; 整数投票[大小]; 整数大小,和; 如果输入; openFile(in); getData(输入、名称、投票、大小); 计算百分比(票数、百分比、总和); sortVotes(名称、投票、百分比、大小); 对于(int i=0;i>v[s]; } } 无效计算百分比(整数v[],双p[],整数和总和){ int-total=0; 对于(int i=0;i>n[s]>>v[s]; } },c++,segmentation-fault,coredump,C++,Segmentation Fault,Coredump,就像我说的,我不知道哪里出了问题。任何见解都会有帮助。在下面的代码中,第一个循环将“s”设置为“SIZE”,数组长度也都是“SIZE” void getData(ifstream& in, string n[], int v[],int& s){ int x = 0; for (int s = 0; s < SIZE; s++){ n[s] = x; v[s] = x; } i

就像我说的,我不知道哪里出了问题。任何见解都会有帮助。

在下面的代码中,第一个循环将“s”设置为“SIZE”,数组长度也都是“SIZE”

 void getData(ifstream& in, string n[], int v[],int& s){
       int x = 0;
       for (int s = 0; s < SIZE; s++){
          n[s] = x;
          v[s] = x;
       }
       in >> n[s] >> v[s];
       while (!in.eof()){
          s++;
          in >> n[s] >> v[s];
       }
    }
void getData(ifstream&in,字符串n[],int v[],int&s){
int x=0;
对于(int s=0;s>n[s]>>v[s];
而(!in.eof()){
s++;
在>>n[s]>>v[s];
}
}
第二个循环(while)继续递增“s”(现在是s>SIZE),这会导致在执行
in>>n[s]>>v[s];
时数组访问无效,因为
n[s]
v[s]
现在访问超过为长度大小的数组分配的内存


也可以,因为这是C++,使用<代码>:ST/:>或>代码> STD::vector < /COL> .< /P>当你调用<代码> GETDATABAS/COD>函数时,变量<代码>大小<代码>的值是什么?传递给GETDATA的大小变量没有被初始化,并且使用SOOOYE在<代码> GETDATABAS/COD>中有两个不同的<代码> S/<代码>变量。使用单字母变量名而不是描述性名称的危险。
s
应该是什么意思?对
n
v
等也要这样做。变量名每个字母不需要花费任何费用。你没有用初始值设置
大小
。这意味着它可以是任何东西。也许OP希望
getData
中的code>将设置
size
变量,因为它是通过引用传递的。当然,您看到的不是这种情况。OP的错误可能是
for(int s
当它应该是
for时)(s
@PaulMcKenzie我认为这只会掩盖输入参数
int&s
@SmexxyMoose取决于您试图做什么,这很难从您的代码中推断出来。