Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.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++ 用.txt文件中的数据填充数组,但数字不是';正在将文本文件中的t添加到数组中_C++_Arrays_Io - Fatal编程技术网

C++ 用.txt文件中的数据填充数组,但数字不是';正在将文本文件中的t添加到数组中

C++ 用.txt文件中的数据填充数组,但数字不是';正在将文本文件中的t添加到数组中,c++,arrays,io,C++,Arrays,Io,我试图用文本文件中的数字填充数组。除了文件中没有的数字被添加到数组中之外,一切都很顺利。无论我使用什么输入文件,我用来显示数组中的数字的for循环都会返回2.122e-314或另一个非常小的数字(取决于输入文件)作为数组的最后一个元素。我的ifstream、while(infle>>列表[i])或其他内容中是否存在错误 const int MAXSIZE = 20; void get_data(ifstream &inFile, int &amount, doub

我试图用文本文件中的数字填充数组。除了文件中没有的数字被添加到数组中之外,一切都很顺利。无论我使用什么输入文件,我用来显示数组中的数字的for循环都会返回
2.122e-314
或另一个非常小的数字(取决于输入文件)作为数组的最后一个元素。我的
ifstream
while(infle>>列表[i])
或其他内容中是否存在错误

 const int MAXSIZE = 20;     

 void get_data(ifstream &inFile, int &amount, double list[]){
    char filename[256];
    cin >> filename;
    inFile.open(filename);
    if(inFile.fail()){
        cout << "The file failed to open.\n";
        exit(1);
    }
    inFile >> amount; // gets the number of sales reports in the file
    cout << amount << " sales reports in the file." << endl;
    if(amount > MAXSIZE){
        cout << "There are too many different stores in the file.\n"
             << "Must be less than or equal to 20.\n";
    }
    else{
        double a;
        int i=0;
        while(inFile >> a){
            list[i] = a;
            i++;
        }    
        for(int x = 0; x <= amount; x++ ){
            cout << list[x] << endl;
        }
    }
    inFile.close();
}
这是我从输出数组元素的for循环中获得的输出示例:

62458
81598
98745
53460
35678
89920
78960
124569
43550
45679
2.122e-314

你在最后一个循环中循环太远了

 for(int x = 0; x <= amount; x++ )
     cout << list[x] << endl;
确保只打印实际写入值的索引处的值。从您的输出中可以很清楚地看到,您的循环打印了11个值,而实际上您只插入了10个值

 for(int x = 0; x <= amount; x++ )
     cout << list[x] << endl;
 for(int x = 0; x < amount; x++ )