使用数组和文件时收到未知的访问冲突

发布于 2024-12-17 10:07:49 字数 2836 浏览 1 评论 0原文

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

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

所以 begindata.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+061333655222402111251-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;
}

I've just started learning how to program a few months ago and this is my first programming language. I've written up this program to pull a customer number from "BeginningBalance.dat" along with customer purchases and payments.

After reading in the charges for each customer it will add a finance charge and total everything, then it will write the new ending balances for each customer number in the opposite order.

So beginningdata.dat looks like

111
200.00
300.00
50.00
222
300.00
200.00
100.00

and NewBalance.dat should look like

222
402.00
111 
251.00

Now all that is being read into NewBalance.dat is
-858993460-9.25596e+061333655222402111251-858993460

Heres my code if anyone can help.

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滥情空心 2024-12-24 10:07:49
while(new_balance_file << customer_number[count])
{
    new_balance_file << new_balance[count];
    count--;
}

不对。由于不限制计数,因此您将遍历数组的开头并开始负访问。

while(new_balance_file << customer_number[count])
{
    new_balance_file << new_balance[count];
    count--;
}

Is not right. You'll run over the beginning of the array and start negative accesses since you don't limit count.

漆黑的白昼 2024-12-24 10:07:49

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

In general, an access violation during a memory read means that either you've made a pointer math mistake (not likely in this case since you don't do any pointer math) or you've read significantly past the end or before the beginning of an array.

∞觅青森が 2024-12-24 10:07:49

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

Learn to compile with all warnings enabled and debugging information. For GCC, that means g++ -Wall -g. Then learn how to use a debugger (like gdb for Linux) and run a program step by step.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文