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