【发布时间】:2018-09-26 14:45:39
【问题描述】:
我正在构建一个类似于 ATM 的程序,您可以在其中使用功能存取余额。起初我注意到,在我第一次存款后,它工作正常,但在第二次使用存款后,它不会将其添加到当前余额中,而是将其添加到之前的余额中。
例如:
(第一次尝试)
*余额 = 1000
*我存入500
*现在余额 = 1500
(第二次尝试)
*我存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 << "\n\nEnter command: " <<endl;
cin >> menu;
switch(menu)
{
//----------------------------------------------------------------CASE 1------------------------------------------------------------------------------//
//----------------------------------------------------------------REGISTER----------------------------------------------------------------------------//
case 1:
system ("CLS");
cout << "<-------REGISTER------->\n\n";
cout << "Enter Name: ";
cin >> user;
newuser = user;
cout << "Enter Password: ";
cin >> pass;
newpass = pass;
cout <<"REGISTERED SUCCESSFULLY!" << endl;
cout <<"\n\nPress Any key to contniue" << endl;
cin >> pktc;
system ("CLS");
break;
//------------------------------------------------------------END OF REGISTER--------------------------------------------------------------------------//
//----------------------------------------------------------------CASE 2------------------------------------------------------------------------------//
//-----------------------------------------------------------------LOGIN------------------------------------------------------------------------------//
case 2:
system ("CLS");
cout << "<-------LOGIN------->\n\n";
cout << "Enter Username: ";
cin >> newuser;
cout << "Enter Password: ";
cin >> newpass;
if(newuser != user || newpass != pass)
{
//-------------------------------------------------------------FAILED LOGIN----------------------------------------------------------------------------//
cout << "\nInvalid account" << endl;
cout <<"\n\nPress Any key to contniue" << endl;
cin >> pktc;
system ("CLS");
}
else if (newuser == user || newpass == pass)
{
//----------------------------------------------------------------CASE 2.1------------------------------------------------------------------------------//
//------------------------------------------------------------SUCCESFULL LOGIN--------------------------------------------------------------------------//
system ("CLS");
cout << "\n\n<-------------------------------WELCOME------------------------------->\n\n" << endl;
cout<<"\nWelcome To Banco De Nelio";
cout<<"\nUSERNAME: "<< user << endl;
cout<<"\n\n TIP ATM MACHINE";
cout<<"\n 1. Balance";
cout<<"\n 2. Deposit";
cout<<"\n 3. Withdraw";
cout<<"\n 4. Exit";
do{
cout<<"\n\nChoose Transaction[1-3]:";
cin>>trans;
switch(trans)
{
//----------------------------------------------------------------ATM CASE 1------------------------------------------------------------------------------//
//--------------------------------------------------------------CHECK BALANCE--------------------------------------------------------------------------//
case 1:
system ("CLS");
cout << "\n\n<-------------------------------WELCOME------------------------------->\n\n" << endl;
cout<<"\nWelcome To Banco De Nelio";
cout<<"\nUSERNAME: "<< user << endl;
cout<<"\n\n TIP ATM MACHINE";
cout<<"\n 1. Balance";
cout<<"\n 2. Deposit";
cout<<"\n 3. Withdraw";
cout<<"\n 4. Exit";
cout<<"\n\nYour total balance is: "<< deposit(x, y) ;
break;
//----------------------------------------------------------------ATM CASE 2------------------------------------------------------------------------------//
//--------------------------------------------------------------BEFORE DEPOSIT--------------------------------------------------------------------------//
case 2:
system ("CLS");
cout << "\n\n<-------------------------------WELCOME------------------------------->\n\n" << endl;
cout<<"\nWelcome To Banco De Nelio";
cout<<"\nUSERNAME: "<< user << endl;
cout<<"\n\n TIP ATM MACHINE";
cout<<"\n 1. Balance";
cout<<"\n 2. Deposit";
cout<<"\n 3. Withdraw";
cout<<"\n 4. Exit";
cout<<"\n\nEnter the amount:" ;
cin>>y;
//--------------------------------------------------------------AFTER DEPOSIT--------------------------------------------------------------------------//
system ("CLS");
cout << "\n\n<-------------------------------WELCOME------------------------------->\n\n" << endl;
cout<<"\nWelcome To Banco De Nelio";
cout<<"\nUSERNAME: "<< user << endl;
cout<<"\n\n TIP ATM MACHINE";
cout<<"\n 1. Balance";
cout<<"\n 2. Deposit";
cout<<"\n 3. Withdraw";
cout<<"\n 4. Exit";
cout<<"\n\nYour 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<<"\n\nThank 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 语句中的冲突。 提前致谢
编辑请先注册:)
【问题讨论】:
-
deposit的回报你在哪里设置平衡? -
您正在打印新的平衡,并希望某人或某事会神奇地使其被记住。它不会发生,因为没有魔法。
-
这不是minimal reproducible example,至少不是Minimal。如此多的
cin和cout表明你的deposit()在cout内部被调用,而没有将结果存储在任何地方...... -
在函数中而不是手动计算它,我使用函数然后在我的 ATM 案例 2 中调用它。正确吗?
-
旁注:我会养成给你的变量一些有意义的名字的习惯。像
x和y这样的变量实际上并没有任何实质内容,除非它们实际上是 x/y 坐标。
标签: c++ function switch-statement