错误:如何通过参数传递来解决此问题
ERROR: How could i fix this issue with parameter passing?
#include "stdafx.h"
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::exit;
using std::string;
void pin(double &Balance);
void menu(double &Balance);
void helpMenu();
void withdraw(double &Balance);
void deposit(double &Balance);
void overdraft(double &Balance);
void overdraftMenu(double &Balance);
void viewBalance(double &Balance);
void BuisnessOverdraft(double &Balance);
void OverFiveOverdraft(double &Balance);
void UnderFiveOverdraft(double &Balance);
int main()
{
double Balance;
pin(Balance);
return 0;
}
void pin(double &Balance)
{
int AccountPin = 8376;
int AttemptNum = 0;
int Pin;
do
{
cout << "Enter your pin: ";
cin >> Pin;
if (Pin == AccountPin)
{
menu(Balance);
}
else
{
cout << "Wrong pin, please try again." << endl;
AttemptNum++;
}
} while (AttemptNum < 3);
cout << "You have used all of your attempts, you can no longer attempt with this card" << endl;
return;
}
void menu(double &Balance)
{
int choice;
do {
cout << "Please choose from these options:n1: Withdrawn2: Depositn3: View balancen4: Apply for an Overdraftn5: Help menun6: ExitnPlease choose between 1 and 6: ";
cin >> choice;
switch (choice)
{
case 1:
withdraw(Balance);
break;
case 2:
deposit(Balance);
break;
case 3:
viewBalance(Balance);
break;
case 4:
overdraftMenu(Balance);
break;
case 5:
helpMenu();
break;
case 6:
exit(EXIT_FAILURE);
break;
default:
cout << "Please try again" << endl;
break;
}
} while (choice < 0 || choice > 6);
}
void helpMenu()
{
cout << endl << "When prompted by the interface please input a number between the numbers it says.nYou cannot withdraw more than £200nIf you have nothing left in your account you can apply for a overdraft" << endl << endl;
return;
}
void withdraw(double &Balance)
{
double withdraw;
cout << "How much would you like to withdraw: ";
cin >> withdraw;
menu(Balance);
Balance = Balance - withdraw;
if (Balance < 0) {
cout << "You need to apply for an overdraft" << endl;
Balance =- withdraw;
}
return;
}
void deposit(double &Balance)
{
double deposit;
cout << "How much you like to deposit: ";
cin >> deposit;
menu(Balance);
Balance =+ deposit;
return;
}
void viewBalance(double &Balance)
{
menu(Balance);
double balance = Balance;
cout << "You have " << char(156) << balance << " in your bank account" << endl;
return;
}
void overdraftMenu(double &Balance)
{
char choice;
do {
cout << "Would you like to apply for a overdraft, yes or no? ";
cin >> choice;
tolower(choice);
if (choice == 'y') {
overdraft(Balance);
}
else if (choice == 'n') {
return;
}
else cout << "Please try again";
} while (choice != 'y' || choice != 'n');
}
void overdraft(double &Balance)
{
int choice;
do {
cout << "Are you:n1: A buisness ownern2: A customer of over 5 yearsn3: A customer of under 5 yearsnPlease enter a number between 1 and 3";
cin >> choice;
switch (choice)
{
case 1:
BuisnessOverdraft(Balance);
break;
case 2:
OverFiveOverdraft(Balance);
break;
case 3:
UnderFiveOverdraft(Balance);
break;
default:
cout << "Input a correct valuenPlease try again";
break;
}
} while (choice < 1 || choice > 3);
}
void BuisnessOverdraft(double &Balance)
{
}
void OverFiveOverdraft(double &Balance)
{
}
void UnderFiveOverdraft(double &Balance)
{
}
我正在为学校工作创建一个ATM计划,我需要使用参数通过来获取资产价值和存款,撤回并申请透支。但是,当我运行代码然后存入余额时,余额不会增加。我还需要添加一个用于保存用户详细信息的系统,并且帐户详细信息将需要保存到文本文件中。
您需要在此处进行一些更正。首先,您使用的是函数本地局部的本地变量,一旦控制从函数返回,此局部变量值将不再可用。首先了解全局变量和局部变量。更好的是,您应该使用
之类的结构 customer{
char customerName[200];
double balance = 0;
};
如果您不知道结构声明并使用它更好地学习。
我只是在修改您的代码以临时修复。
double Balance = 1000.00;
int main()
{
}
//declare Balance as global variable just above your main function.
//modify your balance method like below
void balance(double & balance)
{
balance = Balance;
}
void deposit()
{
double deposit;
//double balance;//this is your local variable
cout << "How much you like to deposit: ";
cin >> deposit;
//balance(balance);
//Balance = balance + deposit;
//here in above Balance is your global variable
//better use a setBalance() method like below
setBalance(deposit);
menu();
}
void setBalance(double deposit)
{
Balance = Balance + balance;
}
//This is your new method
这将解决您的问题。但是它仍然没有达到标记。关于在文件中存储值,只需了解如何在C 或C中创建和读取文件。首先自己尝试。如果您面临任何麻烦,我们可以为您提供帮助。
祝你好运!
您的代码中有几个主要缺陷:
-
您不应在每个功能的末尾调用
menu()
- 这会导致递归。仅来自功能的return
,do while
循环将继续迭代。 -
您在
menu
中的环路的状况错误。您可以在case 6
上执行while(true)
和return
,也可以进行while (choice != 6)
。您不需要在那里进行exit(EXIT_FAILURE)
。这样,它将返回控制到main
并正确退出程序。 -
请勿在功能中本地声明
Balance
。仅声明一次(menu
看起来像个好地方),并将其传递给需要它的每个功能。
问题是,您将平衡定义为局部变量。这意味着它在函数body =>外部被销毁。
是无效的。最简单的解决方案 - 如果可以接受,如果我正确理解您的问题 - 是您将其全局(当然可以删除本地定义)。
全局不可能:
然后,您需要通过参考将平衡传递给每个弹性。功能叛逃的第一个示例:
void pin(double& Balance){ .... }
称其为:
int main()
{
double Balance;
...
pin(Balance);
...
}
从PIN您的调用菜单中,您必须对菜单()进行相同的操作:
void menu(double& Balance) { ... }
并以相同的方式修改您从菜单调用的每个功能。
下一个删除
的所有本地定义double Balance;
!!但是!! - 仅留在main()
- Project Euler问题4的错误解决方案
- 在java中解决这段代码时面临循环中的问题
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 难以理解某些人解决IOI问题的源代码
- 节俭并发:未解决的外部问题
- IpOpt拒绝解决不受约束的问题
- 错误LNK2005无法解决问题的解决方案
- 如何解决g++问题"internal compiler error: Illegal instruction min() _GLIBCXX_USE_NOEXCEPT { return __FLT_MI
- 必须使用尾随返回类型的示例,因为无法用旧方法解决问题
- 如何解决问题"unresolved extenal symbol _DllMainCRTStartup@12"?
- 如何使用OlesetClipboard将我的COM对象放置在剪贴板上时解决问题
- 与C++析构函数相比,C#析构函数和GC并没有真正解决问题
- 没有调用函数,而是使用垃圾值.如何调用函数并解决问题
- 所有的组合算法和解决C++问题的一般方法
- 模板过载解决问题
- 对"pthread_key_create"的未定义引用;参数顺序不能解决问题
- 如何在使用cplex解决问题时设置间隙
- 选择一门语言学习真的很重要吗?他们都使用相似的技术来解决问题
- Iostream解决c++问题
- 递归是一件坏事吗?在递归似乎最有用的地方,是否有更清晰或更有效的方法来解决问题?