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++ 使用数组和文件时接收未知访问冲突_C++_Arrays_File Io_Compiler Errors - Fatal编程技术网

C++ 使用数组和文件时接收未知访问冲突

C++ 使用数组和文件时接收未知访问冲突,c++,arrays,file-io,compiler-errors,C++,Arrays,File Io,Compiler Errors,几个月前我才开始学习编程,这是我的第一门编程语言。我编写这个程序是为了从BeginningBalance.dat中提取客户编号,以及客户购买和付款 在读取每个客户的费用后,它将添加财务费用并合计所有费用,然后它将按相反顺序写入每个客户编号的新期末余额 所以beginingdata.dat看起来像 111 200.00 300.00 50.00 222 300.00 200.00 100.00 而NewBalance.dat应该是 222 402.00 111 251.00 现在读入NewB

几个月前我才开始学习编程,这是我的第一门编程语言。我编写这个程序是为了从BeginningBalance.dat中提取客户编号,以及客户购买和付款

在读取每个客户的费用后,它将添加财务费用并合计所有费用,然后它将按相反顺序写入每个客户编号的新期末余额

所以beginingdata.dat看起来像

111
200.00
300.00
50.00
222
300.00
200.00
100.00
而NewBalance.dat应该是

222
402.00
111 
251.00
现在读入NewBalance.dat的所有内容都是 -858993460-9.25596e+0613336555222402112151-858993460

如果有人能帮忙,这是我的密码

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
    int count = 0;
    const int size = 100;
    double customer_beg_balance, customer_purchases, customer_payments, finance_charge_cost, finance_charge = .01;
    int customer_number[size];
    double new_balance[size];
    double total_beg_balances=0,total_finance_charges=0,total_purchases=0,total_payments=0,total_end_balances=0;

    ifstream beginning_balance;
    beginning_balance.open("beginningbalance.dat");

    while(beginning_balance>>customer_number[count])
    {
        beginning_balance >> customer_beg_balance;
        beginning_balance >> customer_purchases;
        beginning_balance >> customer_payments;

        finance_charge_cost = customer_beg_balance * finance_charge;

        new_balance[count] = customer_beg_balance + finance_charge_cost + customer_purchases - customer_payments;
        total_beg_balances+=customer_beg_balance;
        total_finance_charges+=finance_charge_cost;
        total_purchases+=customer_purchases;
        total_payments+=customer_payments;
        total_end_balances+=new_balance[count];

        cout<<fixed<<setprecision(2)<<setw(8)
            <<"Cust No  "<<"Beg. Bal.  "<<"Finance Charge  "<<"Purchases  "<<"Payments  "<<"Ending Bal.\n"
            <<customer_number[count]<<"        "
            <<customer_beg_balance<<"        "
            <<finance_charge_cost<<"       "
            <<customer_purchases<<"     "
            <<customer_payments<<"         "
            <<new_balance[count]<<endl;


        count++;
    }

    cout<<"\nTotals     "
        <<total_beg_balances<<"        "
        <<total_finance_charges<<"       "
        <<total_purchases<<"     "
        <<total_payments<<"         "
        <<total_end_balances<<endl;

    ofstream new_balance_file;
    new_balance_file.open("NewBalance.dat");

    while(new_balance_file << customer_number[count] && count >= 0)
    {
        new_balance_file << new_balance[count];
        count--;
    }

    new_balance_file.close();

    system("pause");
    return 0;
}

学习在启用所有警告和调试信息的情况下编译。对于GCC,这意味着g++-Wall-g。然后学习如何使用gdb for Linux之类的调试器并逐步运行程序。

学习在启用所有警告和调试信息的情况下编译。对于GCC,这意味着g++-Wall-g。然后学习如何使用gdb for Linux之类的调试器,并逐步运行程序

while(new_balance_file << customer_number[count])
{
    new_balance_file << new_balance[count];
    count--;
}
这是不对的。由于不限制计数,因此将遍历数组的开头并启动负访问


这是不对的。由于不限制计数,您将遍历数组的开头并启动负访问。

通常,内存读取过程中的访问冲突意味着您可能犯了指针数学错误,在这种情况下,这是不可能的,因为您没有进行任何指针数学运算,或者您读取的数据远远超过了数组的末尾或开头。

一般来说,内存读取期间的访问冲突意味着您可能犯了一个指针数学错误,在这种情况下,这是不可能的,因为您没有进行任何指针数学计算,或者您的读取明显超过了数组的结尾或开头。

没有帮助。具有讽刺意味的是,对于-Wall,唯一的消息是警告:忽略“int systemconst char*”的返回值……没有帮助。讽刺的是,使用-Wall时,唯一的消息是警告:忽略“int systemconst char*”的返回值……我可以做什么来限制计数出现负数?类似于:whilenew\u balance\u file=0,或者更简单的是,如果出现任何中断,只需执行;要跳出循环。这一点现在起作用了。我在读取正确的值时遇到了问题:/我会结束这个问题,因为你已经解决了这个特定的问题,自己玩一玩,如果你不能解决它,就抛出另一个问题,说明你已经走了多远。现在,您很有可能解决了以前认为存在的文件访问错误。我会稍后再查,如果它还没有打开,我会再看一看祝你好运我能做些什么来限制计数不出现负数?比如:whilenew\u balance\u file=0,或者更简单,但只要有任何中断就可以做;要跳出循环。这一点现在起作用了。我在读取正确的值时遇到了问题:/我会结束这个问题,因为你已经解决了这个特定的问题,自己玩一玩,如果你不能解决它,就抛出另一个问题,说明你已经走了多远。现在,您很有可能解决了以前认为存在的文件访问错误。我会稍后再查,如果它还没有打开,我会再看一看祝你好运