【发布时间】:2014-01-20 00:41:06
【问题描述】:
您好,我正在处理一个工资申请,有以下四个选项:
1.从商店银行账户中减去一笔金额(账户是文本文件“shop”)。您不需要将相同的金额添加到另一个帐户,但您应该在单独的文件中记录带有时间戳的交易。应用程序应防止帐户余额透支。
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();
}
【问题讨论】:
-
你的目标平台和你使用的编译器是什么?
-
我正在使用 Microsoft Visual Studio。而且我什至不确定您所说的目标平台是什么意思,对此非常陌生
-
您可能必须将事务存储在内存中。你在课堂上学习过容器类吗?
-
不,我们还没有介绍。我想我现在必须研究容器类