I'm having problems loading data from a text file, I'd appreciate a nudge in the right direction. I'm a bit of a NOOB, at this.
--------------------------------------------------------------------------------
void loadAccountDetails () {
ifstream theFile;
clrscr ();
int accNr;
string firstName, midname, surName;
double balance;
unsigned short pin, numCustomers =0;
char Type;
theFile.open ("accountDataM.txt");
if (theFile.good()) {
Okay , sorry , brand new to the forum thing. 1st timer here.
Right, I can read the file, as in list it's contents, that works just fine.
I can add new accounts no problem.
But I seem to be having problems loading the data and then using it to say make a deposit or to withdraw an amount.
Or especially when searching for a particular account.
I'm guessing that it is because the data is not being loaded correctly.
I'm 40 and trying to learn C++, loving it but not always getting it.
Below is a copy of the text file, and below that is how I am trying to load the details.
1 2 3 4 5 6 7 8 9 10 11 12
1 Michael Frank Callaghan c 294.87 4855
2 Bob the Builder s 493.87 4856
3 Frank Henry Spencer c 793.87 4857
4 David Hairy Muppet s 793.87 4859
5 Bosco Foxy Boy s 993.87 4845
6 James Earl Jones c 393.87 4835
7 Nora Vera Tipps s 593.87 4825
8 Amanda Betty Forde s 193.87 4815
9 ken god Muller c 123.45 4321
10 Ryan Dog Star s 350.67 2345
11 Paul Michael Sexton s 1000.25 3321
So if you're not sure if you're retrieving the information from the file correctly my first suggest ion would be to print the data out to see if you are reading the file correctly.
My next suggestion would be to create a structure to hold the information. Since all the information on each line is related this will keep it in one place.
The next problem you will have is that you only actually keep one (the last) "record". You will need to use a vector or an array to hold more than one "record".
Ive just knocked something up. Only had 3 hours sleep and i've been coding for 8, so this might be terrible.
You could approach a bit like this (im only elaborating what jlb's said really):
Create an account class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<string>
class Account
{
public:
Account();
~Account();
// Ideally these should be private
// and you should have some public getters/setters.
std::string m_firstName;
std::string m_middleName;
std::string m_lastName;
double m_balance;
int m_pinNumber;
};
and your main might look a bit like this (again, this is very quickly knocked up sorry). Also see comments.
#include "account.h"
#include <vector>
int main()
{
std::vector<Account> accountsList;
// open your file like you're doing now
// iterate through
// On each iteration:
// Read the line into one std::string object
// Parse this string into the constituent strings you need using a token or something like that
// populate your account object
// e.g. once you've got the first name in its own string object
std::string lastname("Michael");
Account account;
account.m_lastName = lastname; // ( should have setters for this)
// and repeat for the rest of the parsed strings
// once the account has been populated add to the collection
accountsList.push_back(account);
// The accountsList object would maybe have methods like WriteToFile() and ReadFromFile()
// i've just created these objects on the stack, but i think you'd need to create on the heap
// i.e. a vector of Account pointers, or a pointer to a vector of Accounts
//accountsList.WriteToFile(< your path to the file >);
return 0;
}
Just wanted to correct a few things mutexe said just incase you do use his class idea.
First if you do declare a constructor make sure you initialize the variables. The way you have it now Account(); will leave everything but the strings of the class uninitialized. It instead should be Account() : m_balance(0.0), m_pinNumber(0) {}.
Second please try to avoid getters and setters. The whole point of having members of a class private is to encapsulate them so that you separate the interface from the implementation. If you use getters and setters you are defeating the whole point of using a class in the first place. So it is best just to keep them as public members and not have them private. You can move them to private if you decide to add more member functions to the class to manipulate the data but do so in the proper fashion.
Otherwise it looks good and his suggestion to use a class to handle the Bank Account is a good idea.
Ideally I would have two classes, one class that handles each individual account (The Account class) and another that manages all the accounts (AccountManager Class).
Hi, yes it is reading in the info, it prints out the contents fine.
So, I just need to go and read up on Structures, haven't covered that yet, and then see how to handle that in an array. At least I have done a bit with Arrays.
Got some good info from Bucky on You tube. I think I'll leave Vectors for another time. ;-)
My advice is to use vectors always over arrays. If you already know a bit of arrays you will have no trouble understanding vectors since they are almost the same but much much easier to work with.
Thanks a bunch for all the input, but I'm a rank and file NOOB.
I have read up on Classes and Vectors and Structures, but I'm at the early stages of learning to program so they are a bit beyond me right now.
Although I can see the advantages of them I'm going to try to avoid them for now.
I'm trying to do this using functions, arrays and strings.
I'm told it's do-able by some-one who I really hope knows what she's talkin' about.
I need to go and rack my ole' grey matter and hammer out another solution.
But I must say , I LOVE this forum thing, pretty cool!!
Although I can see the advantages of them I'm going to try to avoid them for now.
It's not just the advantages of vectors, it's also the disadvantages of arrays. It is better (and easier) to learn vectors first and arrays second; arrays are far more complex than vectors are.
It is doable but it will be much harder. You really shouldn't try to use a bunch of parallel arrays when a single array of a structure will do the job better. Also since you are learning C++ don't shy away from the standard containers. They will make your job much easier.
Start with a basic structure that has variables for all the fields of your file:
jlb, Hi again,
I get what your saying and it does make sense.
Sooooo, I'm going to spend the night learning how to use the structure and I'll get back to you.