c++ - 试图阻止银行程序在选择交易后结束。 C++

标签 c++ string variables if-statement getline

我制作了一个初学者银行程序,允许用户检查余额、存款和取款。但是,每次选择做某事的选项时,交易都会发生,我无法让程序“活着”,以便用户进行另一笔交易。我该如何解决这个问题?

#include <iostream>
#include <string>
using namespace std ;


int restart() ;
int main(){

float balance = 10.50 ;
float with ;
float deposit ;
string input ;


cout << "Welcome to your mom's Bank " << endl  ;

cout << "Your current BALANCE is: " << balance << "$" << endl ;
cout << "...how can we help you?" << endl ;
cout << endl ;
cout << "For a withdrawal, enter A. "<< endl ;
cout << "For a deposit, enter B. " << endl ;
cout << "For a balance check, enter C. " ;
cout << endl ;
cout << endl ;
getline(cin, input) ;

    if(input == "B")
            {
    cout << "How much would you like to deposit? " << endl ; 
    cout << "\t\t\t " ;
    cin >> deposit ;
    balance = balance + deposit ;

    cout << "You have deposited " <<deposit<< "$ " <<  endl ;
    cout << "Your current balance is " <<balance<< "$ " ;
    getline(cin , input) ;


    } 
        else if(input == "A")
        {
    cout << "How would you like to withdraw?" << endl ;
    cout << "\t\t\t" ;
    cin >> with ;

    balance = balance - with ;
    cout << "Your current balance is " <<balance<<"$ " ;
    getline(cin , input) ;


    } 
         else if(input == "C")
        {
    cout << "Your current balance is: " << balance << "$ " ;
    getline(cin , input) ;
}else
    {return -1 ; 
  } 



}

最佳答案

只需使用循环函数,例如 do..while.

string input ;
do{
cout << "Welcome to your mom's Bank " << endl  ;

最后……

}else
  {
                return -1 ; 
  } 
}while(1);
}

关于c++ - 试图阻止银行程序在选择交易后结束。 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20714836/

相关文章:

c++ - v8::Isolate::New 空访问冲突

c++ - 如何让 Eclipse CDT 从数据成员生成 C++ 类构造函数?

javascript - 如何拆分具有多个符号的字符串

java - 如何将字符串 "AXA || BXB && CXC || BXB"替换为 "AXA OR BXB && CXC OR BXB"?

c# - 像数据一样存储表的最佳方式

python - 如何在Python中重新初始化变量?

arrays - Perl:在数组中搜索项目

c++ - 如何使用 OpenGL 在 C++ 中创建 3D 选择工具?

c++ - 结构比较器访问 C++ 中的另一个字段

c# - 除了 Replace() 函数,还有其他替换方法吗?