How do i list the last 6 recent transactions?

This is the piece of code that I am struggling on, I want the case 2 to be able to read in the last 6 recent transactions but not from the text file they are recorded on, when I run case 1 and withdraw an amount, this creates a timestamp in "TransList.txt", case 2 If pressed needs to show the last 6 recent transactions I made using case 1, any help here guys?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <fstream>
#include <cstring>
#include <string>
#include <iostream>
#include <ctime>
#include <iomanip>

using namespace std;

int main(int argc, char *argv[]) {

	const string user= "dan123";
	const string pass = "password";
	const string accnum = "2364288";
	string entnum;
	string entpass;
	int inputted;
	int portbalance;
	string translist;
	int attempts = 0;
	int attempt = 0;
	int subtramount;
	time_t now = time(0);
	char* dt = ctime(&now);


	fstream bank;
	fstream transactions;

	bank.open("PortAccount.txt");
	transactions.open("TransList.txt", ios::app);


	cout << "Welcome to Port Wage Manager v1.2" << endl;
	cout << "Please enter your account number : " << endl;
	cin >> entnum;

	while (entnum !=accnum && attempt < 3)
	{
		cout << "Account number incorrect try again: " << endl;
		cin >> entnum;
		attempt++;
	}

	if (attempt < 3)
	{
		cout << "User Identified, Welcome " << user << endl;
	}
	else
	{
		cout << "All attempts failed, system will now close" << endl;

		system("Pause");

		return 0;
	}

	cout << "Please enter password " << user << endl;
	cout << "Enter here" << endl;
	cin >> entpass;

	while (entpass !=pass && attempts < 3)
	{
		cout << "Access Failed try again: " << endl;
		cin >> entpass;
		attempts++;
	}

	if (attempts < 3)
	{
		cout << "Access Granted ";
	}
	else
	{
		cout << "Three attempts failed, system will now shut down" << endl;

		system("Pause");

		return 0;
	}

	do
	{

		system("cls");

		bank >> portbalance;

		cout << "Account Balance is = " << portbalance << endl << endl;

		cout << "Enter 1 for Bank Transfer" << endl;
		cout << "Enter 2 to display last 5 transactions" << endl;
		cout << "Enter 3 to display account details" << endl;
		cout << "Enter 4 to exit program" << endl;


		cin >> inputted;

		if (inputted > 4)
		{
			cout << "Only enter values 1 to 4" << endl;
		}



		switch (inputted)	
		{
		case 1:
			cout << "How much do you want to withdraw? : " <<  endl;
			cin >> subtramount;

			transactions << " -" << subtramount << setw(5) << " User: " << user << setw(5) << dt;

			cout << "Transaction successful, you withdrew " << subtramount << " GBP at " << dt << endl;

			portbalance = portbalance - subtramount;

			if (portbalance <= 0)
			{
				cout << "Error, withdrawal cancelled, you have insufficient funds" << endl;

				transactions << "Withdrawal attempt cancelled for " << setw(3) << subtramount << "at " << dt; 

				break;
			}

			cout << "Your balance is " << portbalance << endl;

			{
				ofstream bank;
				bank.open ("PortAccount.txt");
				bank << portbalance;
				bank.close();
			}


			break;

		case 2:

			transactions.open("TransList.txt");

			cout << "your last six transactions are " << endl << endl;



			break;
		case 3:

			cout << "The current balance is " << portbalance << endl;
			cout << "Account name: " << user << endl;
			cout << "Account number: " << accnum << endl << endl;
			break;

		case 4:

			cout << "System will now shut down, Goodbye " << user << endl;

			system("pause");

			return 0;
		}


	} while (inputted!=4);





	bank.close();
	transactions.close();


	cin.get();
	cin.ignore();
	return 0;


}	

you'll need to keep a track of transactions in a collection like a vector.
how many pieces of information does one transaction entail? is it just a number? or a number and a date? or something else?

edit: another thing. When you print out your balance for the first time:
cout << "Account Balance is = " << portbalance << endl << endl;

That's going to print out rubbish because you have not initialised 'portbalance'.

this will also cause all of your calculations under case 1 to be wrong too.
Last edited on
Topic archived. No new replies allowed.