双重功能不返回任何东西

时间:2014-10-24 04:37:08

标签: c++

#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 balance\n2. Deposit money into an account\n3. Pay online Bills.\n4. Transfer funds between savings and checking accounts\n5. Send a mini-statement of his account\n6. Exit the program\n";

        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中的所有内容都在工作(它是什么)......我对这个函数做错了什么?

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

非常感谢任何帮助/建设性批评!

谢谢!

3 个答案:

答案 0 :(得分:1)

如果您需要更新传递给function的balance,您应该通过引用传递它:

double serviceCharge(double& balance, int accountType)

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

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

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

答案 1 :(得分: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);
}

答案 2 :(得分:0)

您永远不会使用函数返回的值。你可以在四个地方做到这一点:

serviceCharge(checking_balance, account_type);

也许你的意思是:

checking_balance = serviceCharge(checking_balance, account_type);

虽然通过引用传递余额更简单,但没有返回类型。