need help with aranging data from an in file

been working on a bank project for school that runs from files accounts and transactions

im stuck now with getting the input from the second input file only one line at a time and then getting the next line the same way

the input file looks like this

123 d32.32 d44.33 d44.33 w55.55
143 d33.33 w44.33 d99.99 d666.66
111 w33.33 d34.23 d9999.99


the first input per line is the acount numbers and the values are deposits and withdrawls. i can only seem to pull the whole file into my string or array i need to be able to work with it by line or by account number.

i hope that makes sense

im new to this and this problem has been picking my brain for a solid 15 hours now let me know what to try . i appreciate any help in advance
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
 
#include <iomanip>
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;
#define withdrawl (isit == 'W' || isit == 'w' )
#define deposit ( isit== 'D' || isit == 'd' )

struct PersonAcct
{
	int acct_num;
	double acct_bal;
	string name;

};



string getAccountFileName(); // function prototype for file name 
void printReport(int a, int w, int d, double eb, double bb, double ad, double aw, string name);
void errorCheck();
int main()
{
	ifstream accounts; // account file handle
	ifstream transactions; // transaction file handle
	
	
	string fileName; // File name variable incase not automatically found
	string acct = "accounts.txt"; // File name of account for placing
	                             //with exe
	string trans = "transactions.txt"; // file name of transactions input 
	string trans2 = "monthly_transactions.txt"; // File name of monthly
	// transaction list 

	int const PEEPS = 50; // number of accounts for array
	int const U = PEEPS; // number of accounts to create structs
	int acnum2[PEEPS]; // array for comparison
	int acnum[PEEPS]; // array for account numbers
	string name[PEEPS]; // array for account names
	double bBalance[PEEPS]; // array for begining balances
	PersonAcct p[U]; // struct array 
	
	string line, line2;
	int amountline = 0;
	

	char isit;
	int dCount = 0;
	int	wCount = 0;

//	printReport(123, 1, 3, 283.11, 34.67, 294.11, 45.67, "daffy");

	accounts.open(acct.c_str(), ios::in); // Trys to automatically open accounts.txt


	if (!accounts)    // error check
	{
		cerr << "Your Accounts.txt file was not automatically found " << endl << endl;
		
		fileName = getAccountFileName(); // Calls function to get acounts file location 
		                                // if not already found

		accounts.open(fileName.c_str(), ios::in);
		if (!accounts)
		{
			errorCheck(); // calls function to print error and close program
		}
	}

	while (accounts)
	{
			for (int i = 0; i < PEEPS; i++)
			{
				accounts >> acnum[i] >> name[i] >> bBalance[i];
				
				p[i].acct_num = acnum[i];
				p[i].name = name[i];
				p[i].acct_bal = bBalance[i];
			}
			
			accounts.clear(); // clear bad state after eof
			accounts.seekg(0);

			while (getline(accounts, line))

			{

				amountline++;



			}

			
			
			accounts.close(); // closes input file
		}
	
	
	      transactions.open(trans.c_str(), ios::in); // Trys to automatically open trans file

		  if (!transactions)    // error check
		  {
			  cerr << "Your Transactions.txt file was not automatically found " << endl << endl;
			  transactions.open(trans2.c_str(), ios::in); // trys to open the other file
			  if (!transactions)
			  {
				  cerr << "Still not found one more try : " << endl;
			  }
              fileName = getAccountFileName(); // Calls function to get acounts file location 
			  // if not already found

			  transactions.open(fileName.c_str(), ios::in);
			  if (!transactions)
			  {
				  errorCheck(); // calls function to print error and close program
			  }
		  }
	
		 

			  transactions.get(isit); // prime

			  while (transactions) // prints and reads file
			  {
				  cout << isit;
				  transactions.get(isit);
				  if (isit != '\n' || '\r')
				  {
					  if (deposit) // counts the number of deposits
					  {
						  dCount++;
						  cout << dCount;
					  }
					  if (withdrawl) // counts the number of withdrawls 
					  {
						  wCount++;
						  cout << wCount;
					  }

				  }
			  }




	//		  while (getline(transactions, line2))
	//		  {
	//			  cout << line2;
	//			  cout << "   \n\n";

	//		  }



			  transactions.close(); // closes input file
		  
	
			  
	
	int persons = 0; // printer control
	persons = amountline-1;
	//cout << "enter the number of accounts you want printed: " << endl;
	//cin >> persons;

	for (int counter = 0; counter < persons; counter++)   // printer
	{

		cout << fixed << setprecision(2) << p[counter].acct_num << " " << p[counter].name
			<< " " << p[counter].acct_bal << "\n";
    }




	system("pause"); // holds window 
	return 0;

}


string getAccountFileName()
{
	string fName; // fully qualified name of the file

	cout << "Please enter the fully qualified name of the " << endl
		<< "accounts text file (i.e. including the path): ";
	cin >> fName;
	cout << endl;

	return fName;
}

string getTransactionsFileName()
{
	string fName; // fully qualified name of the file

	cout << "Please enter the fully qualified name of the " << endl
		<< "Transactions text file (i.e. including the path): ";
	cin >> fName;
	cout << endl;

	return fName;
}
void printReport(int a, int w, int d, double eb, double bb, double ad, double aw, string name)
{
	cout << "Account Number :               " << a << endl;
	cout << "Name :                       " << name << endl;
	cout << "Begininning Balance :        " << bb << endl;
	cout << "Ending Balance :            " << eb << endl;
	cout << "Amount Deposited :          " << ad << endl;
	cout << "Number of Deposits :             " << d << endl;
	cout << "Amount Withdrawn :           " << aw << endl;
	cout << "Number of withdrawls :           " << w << endl;
	cout << endl << endl;




}
void errorCheck()
{
	cerr << "file is still not found" << endl << endl;
	system("pause"); // holds window for error check 
	exit(0);

} 
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/176194/
Let's avoid another VOID code deletion shall we.
Last edited on
yes i didnt realize it was rude to do that ! total noob here still i do appologize.
also if it would help if i posted the .txt files ive been let me know
closed account (48T7M4Gy)
This might be a bit new to you but istringstreams are an ideal way to get the data in.

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
	string line = "";
	char type = ' ';
	int accountNo = 0;
	double amount = 0;

	ifstream myfile("bank.txt");
	istringstream iss;

	if (myfile.is_open())
	{
		while (getline(myfile, line))
		{
			iss.str(line);
			iss.clear();

			iss >> accountNo;
			cout << "Account no. : " << accountNo << endl;
			cout << "Transactions: " << endl;

			while (iss >> type >> amount)
				cout << type  << " - " << amount << endl;
			cout << endl;
		}
		myfile.close();
	}
	else
		cout << "Unable to open file";
    return 0;


bank.txt

123 d32.32 d44.33 d44.33 w55.55
143 d33.33 w44.33 d99.99 d666.66
111 w33.33 d34.23 d9999.99


And the output is:
Account no. : 123
Transactions:
d - 32.32
d - 44.33
d - 44.33
w - 55.55

Account no. : 143
Transactions:
d - 33.33
w - 44.33
d - 99.99
d - 666.66

Account no. : 111
Transactions:
w - 33.33
d - 34.23
d - 9999.99

Press any key to continue . . .
Last edited on
closed account (48T7M4Gy)
You still have to interpret the d's and w's along with converting the remainder of the transaction strings to numbers but that is fairly easy.

Even better, I changed the code above to do that for us much more easily.
Last edited on
Hi,

Some additional things:

You already have an array of your struct, so it seems unnecessary to have separate arrays for each of the things that are in the struct anyway.

Consider a std::vector, if allowed.

Try to avoid having using namespace std; Google to see why this is bad. Just put std:: before each std thing - it's the best way in the end, all the expert people do it that way. You can use Find and Replace in your editor to change them all quickly.

Also avoid endl, it's not a problem now - but in future it could be inefficient because it flushes the buffer each time. Just use '\n' instead.

With this:
1
2
void printReport(int a, int w, int d, double eb, double bb, double ad, double aw, string name)
{


Consider having better meaningful variable names, don't worry about longer names - IMO it's a furphy to try to abbreviate everything too much. If one has good variable & function names, the code should read like telling a story. Here is a link to a comical version of what I mean, there is no reason why we all can't try to make our serious code read like this - without the humour I mean :+)

http://www.cplusplus.com/forum/lounge/176684/#msg872881


One can use this style to make the code easier to read:

1
2
3
4
5
6
7
8
9
void printReport(const int AccoutNumber,   // use const when you know the value should not be changed
                  const int NumberOfWithdrawls, 
                  const int NumberOfDeposits, 
                  const double BalanceEnd, 
                  const double BalanceBegin , 
                  const double DepositAmount, 
                  const double WithdrawlAmount, 
                  const std::string& name) // pass by reference for containers
{


With the getFileName functions, consider having a string as an argument for the type of account that it is, then you can have only one function, and avoid code duplication. The same idea applies to when the file was not found - only need 1 function for that as well.

For the #defines on lines 10 and 11, instead consider making use of the toupper function to convert the input to upper case, then you won't need the logic of upper or lower case.

Also, it's a good idea to declare your variables just before you use them. For example dcount (DepositCount) is declared on line 52 but not used until line 136.

Consider how many lines of code you have in functions. There is a sort of a rule that functions should not be longer than 40 LOC, some go for even less, and this includes main. Some of the things I mentioned earlier will help with this, but there is room for improvement. For example lines 128 to 146 should be a function. You have too much vertical white space - some is good, but too much isn't. The other concept is that a function should do 1 thing. Any compound statement (code in braces) is a good candidate for a function. This idea aids understanding because it is a simple form of abstraction.

What I mean about the abstraction, some pseudo code:

1
2
3
for (AllTheCars) {
       WashCar();
}


I don't care HOW WashCar() works, all that I care about is that it washes a car. If I wrote that function, and someone else wants to use it, then they probably don't care either.

Anyway, Good Luck and we look forward to seeing your new code :+)


Last edited on
kenmort thank you i will be working with that code i havent learned about istringstreams yet . this was very helpful and makes a lot of sense. also idea man my professor takes off when we dont use namespace im not sure why but im gonna try and organize my code better like you are saying. we never were taught any rules just the language so i always enjoy learning the right way to code thank you both ill post the new bit when im done working on it !
Topic archived. No new replies allowed.