嗨,我正在研究工资申请,有以下四种选择:
1.从商店银行帐户中减去一个金额(该帐户是文本文件“商店”)。您无需将相同的金额添加到另一个帐户,但应将带有时间戳的交易记录在单独的文件中。该应用程序应防止帐户余额透支。
2.在屏幕上列出五个最近的交易。如果还没有五笔交易,请列出所有交易。
3.在屏幕上打印帐户名称,号码和当前余额。
4.退出程序。
我已经完成了1,3和4,但是我完全不知道如何处理2号。我希望有人可以帮助我解决这个问题。
int read_balance(void);
void write_balance(int balance);
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int selection;
int total;
int attempts = 0;
string name;
string number;
cout << "Correct login details entered!"
<< "" << endl;
cout << "1. Transfer an amount" << endl;
cout << "2. List recent transactions" << endl;
cout << "3. Display account details and current balance" << endl;
cout << "4.Quit" << endl;
cout << "Please enter menu number" << endl;
cin >> selection;
switch (selection)
{
case 1:
cout << "How much do you wish to transfer?" << endl;
int amount = 0;
if (std::cin >> amount)
{
std::cout << "Transferred Amount:" << amount << "\n";
int balance = read_balance();
if (amount <= 0)
{
std::cout << "Amount must be positive\n";
}
else if (balance < amount)
{
std::cout << "Insufficient funds\n";
}
else
{
int new_balance = balance - amount;
write_balance(new_balance);
std::cout << "New account balance: " << new_balance
<< std::endl;
fstream infile("time.txt", ios::app);
std::time_t result = std::time(nullptr);
std::string timeresult = std::ctime(&result);
infile << amount << std::endl;
infile << timeresult << std::endl;
}
}
break;
case 2:
cout << "Here are you're recent transactions" << endl;
break;
case 3:
cout << "The account names is:" << name << endl;
cout << "The account number is:" << number << endl;
std::cout << "The current account balance is " << read_balance()
<< std::endl;
break;
case 4:
system("pause");
return 0;
default:
cout << "Ooops, invalid selection!" << endl;
break;
}
system("pause");
return 0;
}
int read_balance(void)
{
std::ifstream f;
f.exceptions(std::ios::failbit | std::ios::badbit);
f.open("shop.txt");
int balance;
f >> balance;
f.close();
return balance;
}
void write_balance(int balance)
{
std::ofstream f;
f.exceptions(std::ios::failbit | std::ios::badbit);
f.open("shop.txt");
f << balance;
f.close();
}
最佳答案
这是您的第二个开关案例的内容:
case 2:
{
std::cout << "Here are your recent transactions" << '\n';
for (int i = 0, size = v.size(); i < (size <= 5 ? size : 5); ++i)
{
std::cout << i << ". " << v.at(i) << '\n';
}
break;
}