Using struct to get files quesiton.

So I am writing a code to process a .dat file that contains numbers for a bank account number and the money being deposited each time. It looks like when I run the code only the last two values are read, and I dont know how to do it one step at a time instead. If anyone could show me with a pseudo-code on how to get to the next value in the file I would greatly appreciate it. My data file reads as follows:


2000 1 1225.72 1 463.81 3 200 1 632 2 1500 1 300 2 1800


when I run the code, It just continues forevor with the same values, and doesn't go to the next section of the file. I checked my book and I couldn't find anything on the matter.



my code:
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
  #include <fstream>
#include <iostream>
#include <iomanip>
using namespace std; 
struct bankRec
{
	int transaction;
	double bankBalance;

};
// Function Prototypes
	

	bankRec BuildRec(ifstream&);
	void DisplayRec(bankRec);


	void displayTitle();
	double getBegBal(bankRec);
	void displayBal(double);
	void getData(bankRec&, int& , double&);
	double processCheck(double, double);
	double processDeposit(double, double);
	double processATM(double, double);
	double processSvcChg(double);


//Global Constants
const double	CHARGE = 10,
				ATMFEE =  2;

int main()
{
	//Variable Declarations
	bankRec emp;
	int transCode;
	double balance,
		   transAmt;
	//file loading
	ifstream inFile;
	inFile.open("D:\\checkIn.dat");
	if(inFile.fail())
	{
		cout << "The file didn't load";
	}
	else 
		cout <<"the file loaded";
	
	while(! inFile.eof())
	{
		emp = BuildRec(inFile);
		//DisplayRec(emp);
	
	}
	
	
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);



	displayTitle();
	balance = getBegBal(emp);
	getData(emp,transCode, transAmt);
	
	while(transCode != 0)
	{
		switch(transCode)
		{
			case 1: balance = processCheck(balance, transAmt); break;
			case 2: balance = processDeposit(balance, transAmt); break;
			case 3: balance = processATM(balance, transAmt); break;
		}
		displayBal(balance);
		if(balance < 0)
			balance = processSvcChg(balance);
		getData(emp, transCode,transAmt);
	}
	inFile.close();
	system("pause");
	return 0;
}	

	bankRec BuildRec(ifstream& inFile)
	{
		bankRec temp;
		inFile >> temp.transaction >> temp.bankBalance;
		return temp;

	}

	void DisplayRec(bankRec temp)
	{
		cout << temp.transaction << setw(4) << temp.bankBalance << endl;
	}



	void displayTitle()
	{
		cout << "\n       Check Register\n\n";
	}

	double getBegBal(bankRec temp)
	{
		double bal;
		cout << "  Enter beginning balance ";
		bal=temp.bankBalance;
		cout << bal;
		return bal;
	}

	void displayBal(double x)
	{
		cout << "\t\tBalance = $" << setw(10) << x;
	}

	void getData(bankRec& temp, int& code, double& amt)
	{

		cout << "\n\n  Enter transaction code (0 to exit) ";
		code=temp.transaction;
		cout << code;
		
		if(code > 0)
		{
			cout << "\n  Enter transaction amount ";
			amt=temp.bankBalance;
			cout << amt;
		}
	}

	double processCheck(double bal, double amt)
	{
		cout << "\n  Check =    " << setw(10) << amt;
		return (bal - amt);
	}

	double processDeposit(double bal, double amt)
	{
		cout << "\n  Deposit =  " << setw(10) << amt;
		return (bal + amt);
	}
	double processATM(double bal, double amt)
	{
		cout << "\n  ATM     =  " << setw(10) << amt;
		bal = bal - amt;
		displayBal(bal);
		bal = bal - ATMFEE;
		cout << "\n  ATM Fee =  " << setw(10) << ATMFEE;
		return (bal);
	}
	double processSvcChg(double bal)
	{
		cout << "\n  Service chg =" << setw(8) << CHARGE;
		bal = bal - CHARGE;
		displayBal(bal);
		return (bal);
	}
Last edited on
This part of your code works OK for me

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
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

struct bankRec
{
    int transaction;
    double bankBalance;
};

// Function Prototypes
bankRec BuildRec(ifstream&);
void DisplayRec(bankRec);

int main()
{
    //Variable Declarations
    bankRec emp;

    //file loading
    ifstream inFile;
    inFile.open("checkIn.dat"); // was D:\\checkIn.dat
    if(inFile.fail())
        cout << "The file didn't load\n"; // added \n
    else
        cout <<"the file loaded\n"; // added \n
    
    while(! inFile.eof())
    {
        emp = BuildRec(inFile);
        DisplayRec(emp); // was commented out
    }

    return 0;
}

bankRec BuildRec(ifstream& inFile)
{
    bankRec temp;
    inFile >> temp.transaction >> temp.bankBalance;
    return temp;
}

void DisplayRec(bankRec temp) // tweaked to use comma
{
    //4 isn't a big enough width?
    //cout << temp.transaction << setw(4) << temp.bankBalance << endl;
    cout << temp.transaction << ", " << temp.bankBalance << endl;
}


with input file (as given above?)

2000 1 1225.72 1 463.81 3 200 1 632 2 1500 1 300 2 1800

I get the following output

the file loaded
2000, 1
1225, 0.72
1, 463.81
3, 200
1, 632
2, 1500
1, 300
2, 1800


Is this not what you expected?

(Actually, the start looks a bit suspicious -- what does the 2000 mean?)

But I don't see you storing the values you get (in an array?)

Andy

PS It would be better to tweak your BuildRec to this

istream& BuildRec(istream& strm, bankRec& rec)
{
strm >> rec.transaction >> rec.bankBalance;
return strm;
}

so you can use the while loop more idiomatically, like this

1
2
3
4
5
6
7
8
    bankRec emp;

    ....
    
    while(BuildRec(inFile, emp))
    {
        DisplayRec(emp);
    }
Last edited on
When I run with DisplayRec it shows all the values fine. The issue is that when I am trying to get the numbers from the .dat file into the "ATM", it only reads 2 and 1800. Instead of processing the first numbers for the transaction ID (first is 1), and the amount (first is 2000), it goes straight to 2 and 1800. I don't know how to fix that.
It reads all values from the file fine -- I showed you the o/p when I ran your code.

You need to store all the values you read from the file (in an array?), not just the last one.

Andy
Topic archived. No new replies allowed.