嗨,我上有四個選項的工資應用程序的工作1.4.3事務的情況如下:從文本文件到屏幕
減去量從商店的銀行賬戶(賬號爲文本文件「店」)。您不需要在 另一個帳戶中添加相同的金額,但您應該使用時間戳將交易記錄在單獨的文件中。 應用程序應該防止帳戶餘額透支。
列出五個最近的交易到屏幕上。如果還沒有五筆交易 則列出所有這些交易。
將帳戶名稱,編號和當前餘額輸出到屏幕上。
退出程序。
我已完成1,3和4,但我完全難以理解如何去2號。我希望有人能夠指出我在正確的方向。
#include <limits>
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
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();
}
提示 - 保留最近5次交易的[循環緩衝區](http://en.wikipedia.org/wiki/Circular_buffer)。 – Dukeling