c++ - 变量总是在函数中重置为默认值

标签 c++ function switch-statement

我正在构建一种 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 语句中的冲突。 提前致谢

编辑请先注册:)

最佳答案

很难发现重要的部分,但基本上你的问题可以归结为

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';
}

您的方法可以正确计算新金额,但是当您调用这些方法时,您忽略了返回值,而您想要更新一些变量。

其他一些建议(顺序随机):

  • 使用有意义的名字。 xy 不表达任何意义,比较 int deposit(int current_balance, int amount)int deposit(int x, int y)
  • 使用函数将您的代码分成更小的部分(例如,将输入、输出和逻辑分开是个好主意)。每当您发现自己写了一条注释来描述一段代码的作用时,这段代码就是一个很好的函数候选者,该函数获得了一个正确的名称而不是注释
  • 当您想结束一行时不要使用std::endl(std::endl 结束该行并刷新流,这在大多数情况下不是您想要的),请改用 \n
  • 不要使用using namespace std;。它不会对您当前的代码造成太大伤害,但请阅读 here为什么你永远不应该在标题中这样做,最好不要从一开始就养成坏习惯。

关于c++ - 变量总是在函数中重置为默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52520475/

相关文章:

C++:将枚举连接到 std::string

c++ - 如何替换某些范围的 std::vector 数据

php - 为什么我不能在 Drupal 中使用 PHP 函数?

python - 多次点击按钮调用函数?

javascript - 触发具有 $this 的 Jquery 函数。加载和单击

xml - 如果 Switch 语句中不存在 xml 节点,如何通过断言使 groovy 脚本失败

swift - 为什么这个带有两个枚举的 Swift switch 语句不详尽?

c++ - 在结构 C++ 之间传递数组

c++ - ffmpeg 的 Windows 库不链接(visual studio)?

javascript - 在 JavaScript 中切换大小写