Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我的程序给了我一个分段错误(c+;+;)_C++_Loops_File Io_Fault - Fatal编程技术网

C++ 我的程序给了我一个分段错误(c+;+;)

C++ 我的程序给了我一个分段错误(c+;+;),c++,loops,file-io,fault,C++,Loops,File Io,Fault,我的程序逐行读取一个文本文件,以获取关于一群捐款人的信息。它使用一组结构来存储信息(姓名、捐赠金额)。文本文件的第一行是参与者的数量。从那时起,这些行代表捐赠的名称和价值。例如: 3 Kyle Butler 10340 James Wright 5006 John Smith 10000 然后,如果他们的捐赠是10000或更多,他们的姓名和捐赠价值将在“大赞助人”标题下输出到屏幕上,否则,将在“赞助人”标题下显示 问题是,当我运行程序时,它给我一个分段错误,没有证据表明程序向屏幕输出了任何东西

我的程序逐行读取一个文本文件,以获取关于一群捐款人的信息。它使用一组结构来存储信息(姓名、捐赠金额)。文本文件的第一行是参与者的数量。从那时起,这些行代表捐赠的名称和价值。例如:

3
Kyle Butler
10340
James Wright
5006
John Smith
10000
然后,如果他们的捐赠是10000或更多,他们的姓名和捐赠价值将在“大赞助人”标题下输出到屏幕上,否则,将在“赞助人”标题下显示

问题是,当我运行程序时,它给我一个分段错误,没有证据表明程序向屏幕输出了任何东西。有人能告诉我这里发生了什么事吗?我对这门语言相当陌生

#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <cctype>
#include <cstdlib>

using namespace std;

struct contribute {
    char Name[100];
    int contribution;
};

int main()
{

    char tempstore[100];
    int linecount=1;
    int pointerindex=0;
    ifstream inputfile("myfile.txt");

     if (!inputfile.is_open()){
         cout << "Error opening file";
     }

    inputfile.getline(tempstore, 20);
    int contributors = atoi(tempstore);
    contribute carray[contributors]; 

    while (!inputfile.eof()){
        if(linecount ==1){
            linecount++;
            continue;
        }

        inputfile.getline(tempstore, 20);


         if ((linecount % 2) == 0){
            strcpy( (carray[pointerindex]).Name, tempstore);

         }
         else {
             (carray[pointerindex]).contribution = atoi(tempstore);
         }


         ++linecount;
         ++pointerindex;
    }




    cout << "\n#####--#-Grand Patrons-#--#####\n";

    for (int i=0; i<contributors; i++){
        if (carray[i].contribution >= 10000){
            cout << "Name:  " << carray[i].Name << endl;
            cout << "Contribution:  " << carray[i].contribution << endl << endl;
        }
    }

    cout << "\n#####--#-Patrons-#--#####\n";

    for (int d=0; d<contributors; d++){
        if (carray[d].contribution < 10000){
            cout << "Name:  " << carray[d].Name << endl;
            cout << "Contribution:  " << carray[d].contribution << endl << endl;
        }
    }  

    inputfile.close();

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构贡献{
字符名[100];
国际贡献;
};
int main()
{
char-tempstore[100];
int linecount=1;
int pointerindex=0;
ifstream输入文件(“myfile.txt”);
如果(!inputfile.is_open()){

cout在循环的每一次传递中,您都会增加
pointerindex
,而不是仅仅在您读入贡献金额之后。因此,当您读入
5006
时,您已经有了
pointerindex==3
,这会导致
carray[pointerindex]
上出现segfault


++pointerindex
移动到
else
语句中。

哪一行出错?您是否尝试过在调试器中运行它?您使用的是什么IDE、操作系统?contribute carray[贡献者];您的编译器让您这样做?它不应该是contribute*carray=new Contributes[贡献者]?@JonathanHenson不,这不是一个动态数组,只是一个结构数组,我最初使用的是动态数组,但我认为这可能是segfault背后的原因,所以我改为静态数组。无论哪种方式都有效。我的观点是,在大多数编译器上都不会编译。大多数编译器都不允许在没有exp的情况下创建具有动态大小的数组合法地使它成为一个动态数组。无论如何,如果它能工作,它就工作了。非常感谢!我知道它会很简单,而且突然变得很有意义!