双函数不返回任何内容
Double Function Not Returning Anything
#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);
尽管仅通过引用传递余额而不具有返回类型会更简单。
相关文章:
- 为什么 c++ 中的 main() 函数不采用除 int 和 void 之外的任何其他返回类型
- 是否有任何内置的哈希图函数来处理 c++ 中的值输入?
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- 函数使用任何输入返回 0
- C++ 中是否有任何内置阶乘函数?
- C++中的函数是否有任何默认返回类型
- 是否有任何内置函数可以告诉编译器分支是否可预测
- 如何检查copy_if是否返回使用ostream_iterator范围内的任何内容
- vector::size() 如何在常量时间内返回向量的大小
- 我需要在C /C#中使用图形.我可以使用任何内置的C /C#库
- 是否有任何内置类可以处理C 中的大整数值
- c ++中是否有任何内置功能可以在计划的时间运行任务
- 是否有任何内置的 UTF 在 c++ 在窗口中
- 在VC++2010中执行任何内置的ARRAY函数
- C++:如何在不使用任何内置函数的情况下将字符串转换为整数
- 是否有任何内置的CUDA函数允许CUDA内核向主机代码报告错误?
- Io_getevents在小于timeout的时间内返回的作业数少于请求的作业数
- 我如何在常数时间(O(1))内获得字母表(1-26)中字符的数值/位置,而不使用任何内置方法或函数
- C++中的任何内置哈希方法
- c++库中的任何内置夹子方法