双函数不返回任何内容

Double Function Not Returning Anything

本文关键字:任何内 返回 函数      更新时间:2023-10-16
#include <iostream>
using namespace std;
void userBalance(double balance1, double balance2);
//Displays the account balance for both the accounts and also a message that a service charge will be imposed for every 
//transaction if balance is less than the minimum balance and also suggests the user to deposit money in the account.
void deposit(double& balance, double amount);
//Adds the amount of deposit and displays the new balance to user
void paybill(double& balance, double amount, int accountType);
//Checks it there are sufficient funds by calling the checkBalance() function. If it returns false, print that transaction 
//cannot be completed, try again and display the main menu. If it returns true, deduct the amount from the balance and 
// print a message of successful payment. Call the serviceCharge() function for any applicable service charges.
void transfer(double &balance1, double &balance2, double amount, int accountType);
// Check for sufficient funds before allowing this transaction by calling the checkBalance() function. If it returns true, 
// then change the savings and checking account balances accordingly and print a message for a successful transfer. Call
// the serviceCharge() function with appropriate arguments to check for any applicable charges on the from account.
//Update and display the final balances
void miniStatement();
//Just prints a message to screen that a statement will be mailed to the registered email. This function will not display any 
//actual transactions 
double serviceCharge(double balance, int accountType);
//Checks the existing balance and applies the pre-determined service charge if the balance is less than the minimum 
//balance for every transaction. This function should print the service charge being applied and return the amount of the 
//service charge
bool checkBalance(double balance, double amount);
// Checks if the user can make a transfer/transaction based on the amount entered. The function should return true if the 
// balance is more than the transaction amount and false if the balance is less than the transaction amount
int main()
{
    bool default = false;
    string x;
    int choice = 0;
    int account_type;
    double checking_deposit, savings_deposit;
    double checking_balance = 0.0, savings_balance = 0.0;
    double bill_amount;
    double transfer_amount;
    while (choice != 6)
    {//happy
        cout << "------------------------------------------" << endl;
        cout << "Welcome to Jayhawk online banking Services" << endl;
        cout << "------------------------------------------" << endl;
        cout << "You can use the following features for your account- " << endl;
        cout << "1. View account balancen2. Deposit money into an accountn3. Pay online Bills.n4. Transfer funds between savings and checking accountsn5. Send a mini-statement of his accountn6. Exit the programn";
        cout << "Please enter a choice: " << endl;
        cin >> choice;
        switch (choice)
        {
            case 1: userBalance(checking_balance, savings_balance);
                break;
            case 2: cout << "Please enter type of account (0 - Checking, 1 - Savings)" << endl;
                cin >> account_type;
                switch (account_type)
                {
                    while (default == true)
                    {
                    case 0: cout << "Please enter amount: " << endl;
                        cin >> checking_deposit;
                        deposit(checking_balance, checking_deposit);
                        userBalance(checking_balance, savings_balance);
                        break;
                    case 1: cout << "Please enter amount: " << endl;
                        cin >> savings_deposit;
                        deposit(savings_balance, savings_deposit);
                        userBalance(checking_balance, savings_balance);
                        break;
                    default: cout << "Not a valid selection. Please choose again. " << endl;
                        break;
                    }
                }
                break;
            case 3: cout << "Please enter type of account (0 - Checking, 1 - Savings)" << endl;
                cin >> account_type;
                switch (account_type)
                {
                case 0: cout << "Enter the amount of bill: " << endl;
                    cin >> bill_amount;
                    if (checkBalance(checking_balance, bill_amount))
                    {
                        paybill(checking_balance, bill_amount, account_type);
                        userBalance(checking_balance, savings_balance);
                    }
                    else if (!checkBalance(checking_balance, bill_amount))
                    {
                        serviceCharge(checking_balance, account_type);
                    }
                    break;
                case 1: cout << "Enter the amount of bill: " << endl;
                    cin >> bill_amount;
                    if (checkBalance(checking_balance, bill_amount))
                    {
                        paybill(savings_balance, bill_amount, account_type);
                        userBalance(checking_balance, savings_balance);
                    }
                    else if (!checkBalance(checking_balance, bill_amount))
                    {
                        serviceCharge(savings_balance, account_type);
                    }
                    break;
                default: cout << "Not a valid selection. Please choose again. " << endl;
                }
                break;
            case 4: cout << "Choose the account from which you would like to transfer (0 - Checking, 1 - Savings)" << endl;
                cin >> account_type;
                switch (account_type)
                {
                case 0: cout << "Enter the amount to be Transferred: " << endl;
                    cin >> transfer_amount;
                    if (checkBalance(checking_balance, transfer_amount))
                    {
                        transfer(checking_balance, savings_balance, transfer_amount, account_type);
                        userBalance(checking_balance, savings_balance);
                    }
                    else if (!checkBalance(checking_balance, transfer_amount))
                    {
                        transfer(checking_balance, savings_balance, transfer_amount, account_type);
                        serviceCharge(checking_balance, account_type);
                        userBalance(checking_balance, savings_balance);
                    }
                    break;
                case 1: cout << "Enter the amount to be Transferred: " << endl;
                    cin >> transfer_amount;
                    if (checkBalance(checking_balance, transfer_amount))
                    {
                        transfer(savings_balance, checking_balance, transfer_amount, account_type);
                        userBalance(checking_balance, savings_balance);
                    }
                    else if (!checkBalance(checking_balance, transfer_amount))
                    {
                        serviceCharge(savings_balance, account_type);
                    }
                    break;
                default: cout << "Not a valid selection. Please choose again. " << endl;
                    break;
                }
                break;
            case 5: cout << miniStatement;
                break;
            case 6: cout << "Thank you for using Jayhawk banking services. You have been logged out..." << endl;
                break;
            default: cout << "Not a valid option, you warlock. " << endl;
                break;
        }
    }
    system("Pause");
    return (0);
}
void userBalance(double balance1, double balance2)
{
    double checking_balance = balance1;
    double savings_balance = balance2;
    if (checking_balance == 0.0 && savings_balance == 0.0)
    {
        cout << "You do not have any balance in your savings and checking account. Please make a deposit to your accounts. " << endl;
    }
    else
    {
        cout << "You have $" << checking_balance << " in your Checking account and $" << savings_balance << " in your Savings account " << endl;
    }
}
void deposit(double& balance, double amount)
{
    balance = balance + amount;
    cout << "One moment please, Scanning the check ............nYour deposit is Successful.." << endl;
}
void paybill(double& balance, double amount, int accountType)
{
    balance = balance - amount;
    accountType = balance;
    cout << "Processing.............nThe Bill payment is Successful " << endl;
}
void transfer(double &balance1, double &balance2, double amount, int accountType)
{
    balance1 = balance1 - amount;
    balance2 = balance2 + amount;
    accountType = balance2;
    cout << "Transfer of amount is Successful.." << endl;
}
void miniStatement()
{
    cout << "A mini-Statement is being generated and will be sent to your registered e-mail" << endl;
}
double serviceCharge(double balance, int accountType)
{
    if (accountType == 0)
    {
        cout << "A service charge of $25.00 is imposed on your Checking account for dropping below the minimum balance" << endl;
        return(balance - 25.0);
    }
    else if (accountType == 1)
    {
        cout << "A service charge of $15.00 is imposed on your Savings account for dropping below the minimum balance" << endl;
        return(balance - 15.0);
    }
}
bool checkBalance(double balance, double amount)
{
    if (balance-amount > 500)
    {
        return(true);
    }
    else if (balance-amount < 500)
    {
        return(false);
    }
}

好吧,如果余额低于 500 美元,此功能应该对支票账户收取 25 美元的服务费,对储蓄账户收取 15 美元的服务费。假设 main 中的所有内容都在工作(确实如此(...我用这个函数做错了什么?

它不会更新余额。相反,如果是,则不会返回更新的值。

任何帮助/建设性的批评将不胜感激!

谢谢!

如果需要更新传递给函数balance,则应通过引用传递它:

double serviceCharge(double& balance, int accountType)

如果你只需要返回更新的值,你可能应该使用临时变量:

double serviceCharge(double balance, int accountType)
{
  double ret_balance = balance;
  ...
  return ret_balance;
}

你应该返回一些东西,以防accountType不是 0 或 1(可能会抛出异常(。

您似乎错过了accountType != 0 && accountType != 1的情况。 试试这个:

double serviceCharge(double& balance, int accountType)
{
    if (accountType == 0)
    {
        balance = balance - 25.00;
        cout << "A service charge of $25.00 is imposed on your Checking account for dropping below the minimum balance" << endl;
        return(balance);
    }
    else if (accountType == 1)
    {
        balance = balance - 15.00;
        cout << "A service charge of $15.00 is imposed on your Savings account for dropping below the minimum balance" << endl;
        return(balance);
    }
    return (balance);
}

从不使用从函数返回的值。您可以在四个位置执行此操作:

serviceCharge(checking_balance, account_type);

也许你的意思是:

checking_balance = serviceCharge(checking_balance, account_type);

尽管仅通过引用传递余额而不具有返回类型会更简单。