关于c ++中支票簿平衡程序代码的问题

时间:2014-11-17 03:15:08

标签: c++ function

我正在尝试编写一个程序来平衡c ++中的支票簿。以下是我的确切说明

  

编写一个代表您支票簿中一个月交易的程序。提示   用户获得起始余额,然后允许用户输入所需数量的交易。

     

交易应采用C500(支票500美元)或D250(存款250美元)或E(月末支票)的形式。

     

每项交易的服务费如下:每张支票15美元,每笔押金0.10美元;此外,如果月内任何时间的余额低于500美元,则会收取5美元的一次性服务费,而每张支票的10美元服务费会产生负余额。

     

最后,如果用户的余额介于0美元和50美元之间,您将向用户发出警告。运行演示程序并密切关注所需的输出。

     

(请注意,每次交易后都会打印当前余额和总服务费,但是在用户为月末宣布E之前,服务费不会从余额中扣除。)

到目前为止我的代码:

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

void MoneyFunct(float &CashLeft, float StartBal, char TransacType,
    float Transac, int Q, float TotalServiceCharge);

int main() {
    char TransacType;
    float StartBal;
    float Transac;
    float CashLeft = StartBal;
    float TotalServiceCharge;
    cout << "Enter your starting balance: ";
    cin >> StartBal;
    do {
        cout << "Now enter your transaction."
            << "Enter E when your month has ended." << endl;
        cout << "Enter transaction type: ";
        cin >> TransacType;
        cout << endl;
        cout << "Enter the amount for your transaction: ";
        cin >> Transac;
        cout << endl;
        MoneyFunct;
        cout << "You have $" << CashLeft 
            << " remaining in your account" << endl
            << "Your total service charges are "
            << TotalServiceCharge << endl;
    } while (TransacType != 'E');

    CashLeft = CashLeft + TotalServiceCharge;
    cout << endl << "Your final balance is " 
        << CashLeft << "." << endl
        << "Your total service charges are "
        << TotalServiceCharge << "." << endl
        << "Thanks for using the checkbook balancer!" << endl;
    return 0;
}

void MoneyFunct(float &CashLeft, float StartBal, char TransacType, 
    float Transac, int Q, float TotalServiceCharge)
{
    Q == 4;
    TotalServiceCharge = 0;
    if (TransacType == 'C') {
        CashLeft = CashLeft - Transac;
        TotalServiceCharge = TotalServiceCharge + 0.15;
    } else if (TransacType == 'D') {
        CashLeft = CashLeft + Transac;
        TotalServiceCharge=TotalServiceCharge+0.10;
    }

    if (CashLeft < 500) {
        while (Q == 1) {
            cout << "Your balance has fallen below $500."
               << "A one time charge of $5 will be subtracted "
               << "from your balance." << endl;
            TotalServiceCharge = TotalServiceCharge+5;
            Q++;
        }
    }

    if (CashLeft <= 50 && CashLeft >= 0) {
        cout << "Your balance is lower than $50." <<
            << "If your balance becomes negative "
            << "you will be charged $10" << endl;
    }

    if (CashLeft < 0) {
        cout << "Your balance has become negative." <<
            << "A $10 charge will be subtracted "
            << "from your balance." << endl;
        TotalServiceCharge = TotalServiceCharge+10;
    }
}

这会返回类似于此的内容:

Enter your starting balance: 500
Now enter your transaction. Enter E when your month has ended.
Enter transaction type: D

Enter the amount for your transaction: 500

You have $4.42913e+032 remaining in your account
Your total service charges are nan
Now enter your transaction. Enter E when your month has ended.
Enter transaction type: E

Enter the amount for your transaction: 500

You have $4.42913e+032 remaining in your account
Your total service charges are nan

Your final balance is nan.
Your total service charges are nan.
Thanks for using the checkbook balancer!

Process returned 0 (0x0)   execution time : 7.874 s
Press any key to continue.

它返回的平衡足够大,我可以假设我在某处有一个无限循环。

1 个答案:

答案 0 :(得分:0)

此...

float CashLeft = StartBal;

...在StartBal初始化cin >> StartBal之前出现,因此您从未初始化的变量读取未定义行为。只需将上面一行移到cin >> StartBal;

之后

另外,检查您的流媒体操作是否真正有效是一个好习惯。例如:

if (cin >> StartBal)
    CashLeft = StartBal;
else
{
    std::cerr << "unable to read StartBal from cin\n";
    exit(EXIT_FAILURE);
}