#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: Withdraw\n2: Deposit\n3: View balance\n4: Apply for an Overdraft\n5: Help menu\n6: Exit\nPlease 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 £200\nIf 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 owner\n2: A customer of over 5 years\n3: A customer of under 5 years\nPlease 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 value\nPlease try again";
break;
}
} while (choice < 1 || choice > 3);
}
void BuisnessOverdraft(double &Balance)
{
}
void OverFiveOverdraft(double &Balance)
{
}
void UnderFiveOverdraft(double &Balance)
{
}
我正在为学校工作创建一个ATM程序,我需要使用参数传递来获得余额值,存款,取款和申请透支。但是当我运行代码然后存入余额不会增加。我还需要添加一个系统来保存用户详细信息,例如名称和帐户详细信息,这需要保存到文本文件中。
答案 0 :(得分:0)
问题是,您将Balance定义为局部变量。这意味着它在函数体外无效=&gt;它被摧毁了。
最简单的解决方案 - 如果可以接受,并且如果我正确理解您的问题 - 是您将其设为全局(当然还要删除本地定义)。
全球不可能:
然后你需要通过引用将Balance传递给每个func。 功能定义的第一个例子:
void pin(double& Balance){ .... }
称之为:
int main()
{
double Balance;
...
pin(Balance);
...
}
从您调用菜单的pin,所以您必须对菜单()执行相同的操作:
void menu(double& Balance) { ... }
并以相同的方式修改您从菜单中调用的每个功能。
下一步删除
的所有本地定义double Balance;
!! BUT !! - 只在main()
中保留答案 1 :(得分:0)
您的代码中存在几个主要缺陷:
你不应该在每个函数的末尾调用menu()
- 这会导致递归。只需return
来自函数,do while
循环将继续迭代。
menu
中的循环条件错误。您可以在while(true)
上执行return
和case 6
,也可以while (choice != 6)
。您不需要在那里exit(EXIT_FAILURE)
。这样,它会将控制权返回给main
并正确退出程序。
不要在函数中本地声明Balance
。只声明一次(menu
看起来像个好地方)并将其作为对需要它的每个函数的引用传递。
答案 2 :(得分:0)
你需要在这里进行一些修正。首先,您使用的是函数本地的局部变量,一旦控制从函数返回,该局部变量值将不再可用。首先要了解全局变量和局部变量。你应该使用像
这样的结构 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创建和读/写文件。首先自己尝试。如果您遇到任何麻烦,我们可以帮助您。
好运!