您好我正在处理工资申请,其中有四个选项,如下所示:
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();
}
答案 0 :(得分:1)
这是你的第二个开关案例中的内容:
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;
}