变量始终在函数中重置为默认值
Variable always resets to default in Functions
我正在构建一个ATM类型的程序,您可以在其中使用功能存入和提取余额。起初我注意到,在我第一次存款后,它工作正常,但在第二次使用存款后,它不会将其添加到当前余额中,而是添加到之前的余额中。
例如:
(第一次尝试(
*余额 = 1000
*存款500
*余额现在 = 1500
(第2次尝试(
*存款700
*余额现在是1700
它没有达到2200,而是在我第二次尝试之前重置回1000,结果为1700。 任何人都可以解释一下代码中出了什么问题吗?我不仅愿意获得正确的代码,而且还愿意学习它是如何完成的。
这是为了我的 c++ 培训。
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int deposit(int x, int y)
{
int newbal = x + y;
return newbal;
}
int withdraw(int x, int y)
{
int newbal = x - y;
return newbal;
}
int main()
{
int menu;
int selection;
double x = 1000, y;
int trans;
char user[20], pass[20], pktc[3];
string newuser, newpass;
do{
system ("CLS");
cout << "Welcome To Bank" << endl;
cout << "[1] Register" << endl;
cout << "[2] Login" << endl;
cout << "[3] Exit" << endl;
cout << "nnEnter command: " <<endl;
cin >> menu;
switch(menu)
{
//----------------------------------------------------------------CASE 1------------------------------------------------------------------------------//
//----------------------------------------------------------------REGISTER----------------------------------------------------------------------------//
case 1:
system ("CLS");
cout << "<-------REGISTER------->nn";
cout << "Enter Name: ";
cin >> user;
newuser = user;
cout << "Enter Password: ";
cin >> pass;
newpass = pass;
cout <<"REGISTERED SUCCESSFULLY!" << endl;
cout <<"nnPress Any key to contniue" << endl;
cin >> pktc;
system ("CLS");
break;
//------------------------------------------------------------END OF REGISTER--------------------------------------------------------------------------//
//----------------------------------------------------------------CASE 2------------------------------------------------------------------------------//
//-----------------------------------------------------------------LOGIN------------------------------------------------------------------------------//
case 2:
system ("CLS");
cout << "<-------LOGIN------->nn";
cout << "Enter Username: ";
cin >> newuser;
cout << "Enter Password: ";
cin >> newpass;
if(newuser != user || newpass != pass)
{
//-------------------------------------------------------------FAILED LOGIN----------------------------------------------------------------------------//
cout << "nInvalid account" << endl;
cout <<"nnPress Any key to contniue" << endl;
cin >> pktc;
system ("CLS");
}
else if (newuser == user || newpass == pass)
{
//----------------------------------------------------------------CASE 2.1------------------------------------------------------------------------------//
//------------------------------------------------------------SUCCESFULL LOGIN--------------------------------------------------------------------------//
system ("CLS");
cout << "nn<-------------------------------WELCOME------------------------------->nn" << endl;
cout<<"nWelcome To Banco De Nelio";
cout<<"nUSERNAME: "<< user << endl;
cout<<"nn TIP ATM MACHINE";
cout<<"n 1. Balance";
cout<<"n 2. Deposit";
cout<<"n 3. Withdraw";
cout<<"n 4. Exit";
do{
cout<<"nnChoose Transaction[1-3]:";
cin>>trans;
switch(trans)
{
//----------------------------------------------------------------ATM CASE 1------------------------------------------------------------------------------//
//--------------------------------------------------------------CHECK BALANCE--------------------------------------------------------------------------//
case 1:
system ("CLS");
cout << "nn<-------------------------------WELCOME------------------------------->nn" << endl;
cout<<"nWelcome To Banco De Nelio";
cout<<"nUSERNAME: "<< user << endl;
cout<<"nn TIP ATM MACHINE";
cout<<"n 1. Balance";
cout<<"n 2. Deposit";
cout<<"n 3. Withdraw";
cout<<"n 4. Exit";
cout<<"nnYour total balance is: "<< deposit(x, y) ;
break;
//----------------------------------------------------------------ATM CASE 2------------------------------------------------------------------------------//
//--------------------------------------------------------------BEFORE DEPOSIT--------------------------------------------------------------------------//
case 2:
system ("CLS");
cout << "nn<-------------------------------WELCOME------------------------------->nn" << endl;
cout<<"nWelcome To Banco De Nelio";
cout<<"nUSERNAME: "<< user << endl;
cout<<"nn TIP ATM MACHINE";
cout<<"n 1. Balance";
cout<<"n 2. Deposit";
cout<<"n 3. Withdraw";
cout<<"n 4. Exit";
cout<<"nnEnter the amount:" ;
cin>>y;
//--------------------------------------------------------------AFTER DEPOSIT--------------------------------------------------------------------------//
system ("CLS");
cout << "nn<-------------------------------WELCOME------------------------------->nn" << endl;
cout<<"nWelcome To Banco De Nelio";
cout<<"nUSERNAME: "<< user << endl;
cout<<"nn TIP ATM MACHINE";
cout<<"n 1. Balance";
cout<<"n 2. Deposit";
cout<<"n 3. Withdraw";
cout<<"n 4. Exit";
cout<<"nnYour total balance now is: " << deposit(x, y) <<endl;
break;
//----------------------------------------------------------------ATM CASE 3------------------------------------------------------------------------------//
//--------------------------------------------------------------WITHDRAW BALANCE--------------------------------------------------------------------------//
case 3:
cout<<"nEnter the amount:" ;
cin>>y;
if ( y > x)
{
cout<<"nYou cannot withdraw " << y;
cout<<" because the amount is higher than your balance" << endl;
break;
}
else
x = x - y;
cout<<"nYour Total Balance is now " << withdraw(x, y) << endl;
break;
//----------------------------------------------------------------ATM CASE 4------------------------------------------------------------------------------//
//-------------------------------------------------------------------BACK--------------------------------------------------------------------------------//
case 4:
cout<<"nnThank You!" << endl;
break;
default:
cout<<"nYou did not enter any valid number" << endl;
break;
}
}while (trans<=3);
}
break;
//----------------------------------------------------------------CASE 3------------------------------------------------------------------------------//
//-----------------------------------------------------------------EXIT-------------------------------------------------------------------------------//
case 3:
system ("CLS");
cout << "Thank you for using me!n";
return 0;
//-------------------------------------------------------------END OF EXIT----------------------------------------------------------------------------//
}
}while (menu<=3);
}
我不确定这里的问题是否是 switch 语句中的函数或冲突。 提前致谢
编辑请先注册:)
很难发现重要的部分,但基本上你的问题可以归结为类似的东西
int add(int x,int y) { return a+b; }
int sub(int x,int y) { return a-b; }
int main() {
int initial = 0;
// add 10 then subtract 5
std::cout << add(initial,10) << 'n'; // prints 10
std::cout << sub(initial,5) << 'n'; // prints -5
}
当你真正想要的是类似的东西时
int main() {
int initial = 0;
// add 10 then subtract 5 and update initial
initial = add(initial,10);
std::cout << initial << 'n';
initial = sub(initial,5);
std::cout << initial << 'n';
}
您的方法可以正确计算新数量,但是当您调用这些方法时,当您想要更新某些变量时,您会忽略返回值。
其他一些建议(按随机顺序(:
- 使用有意义的名称。
x
和y
不传达任何含义,int deposit(int current_balance, int amount)
与int deposit(int x,int y)
- 使用函数将代码拆分为较小的部分(例如,将输入、输出和逻辑分开是个好主意(。每当你发现自己在写一个描述代码块正在做什么的注释时,这个代码块是一个很好的候选函数,它得到一个正确的名称而不是一个注释。 当你
- 想结束一行时,不要使用
std::endl
(std::endl
结束一行并刷新流,这在大多数情况下不是你想要的(,改用n
- 不要使用
using namespace std;
.它不会对你当前的代码造成太大的伤害,但在这里阅读为什么你永远不应该在标题中这样做,最好不要从一开始就习惯坏习惯。
相关文章:
- 具有默认值的引用获取函数
- 为什么默认复制函数在按值发送参数时不调用?
- 如何使用默认值为构造函数中的枚举赋值?
- 变量始终在函数中重置为默认值
- 如何在C++中提供模板化函数作为另一个函数的参数,默认值?
- 函数不返回默认值
- 在 c++ 中,如果我创建一个接受一个具有默认值的参数的构造函数 - 它会用作默认(空)构造函数吗?
- 为什么 std::move 不将默认移动构造函数中的源变量更改为默认值?
- 为什么std::atomic的默认构造函数不默认初始化底层存储值
- 具有默认值的单个参数构造函数是否与默认构造函数相同?
- 在子类函数覆盖中省略具有默认值的参数
- 函数作为具有默认值的参数
- 结构中的默认成员值或默认构造函数参数
- 将复制构造函数设置为默认值在继承自 QObject 时不起作用
- 显式调用普通默认构造函数会导致额外的赋值调用
- 未设置默认值的 c++ 构造函数
- 没有默认构造函数的类对象的值初始化
- C++中出现意外的编译错误:将默认值传递给函数参数
- 组合 std::vector 默认值和填充构造函数
- 作为参数的空初始值设定项列表不调用默认构造函数