Outputting transactions from a text file to the screen

Hi I am working on a wage application with four options which are as follows:

Subtracts an amount from the shop bank account (The account is text file "shop"). You do not need to add the same amount to another account but you should record the transaction with a timestamp in a separate file. The application should prevent the account balance from becoming overdrawn.

List the five most recent transactions to the screen. If there haven’t yet been five transactions then list all of them.

Print the account name, number and current balance to the screen.

Exit the program.

I have completed 1,3 and 4 but I am completely stumped as to how to go about number 2. I was hoping someone would be able to point me in the right direction.



#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();
}
what you need is a an array for the recent transactions and a count. Like so:
1
2
int recent_transactions[5] = {0};
int count_transactions = 0;


in case 1: write it like so:
1
2
recent_transactions[count_transactions] = amount;
++count_transactions;


Take care that not more than 5 transactions are stored.

Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
Topic archived. No new replies allowed.