Not quite. You need to make the array, and then you can start using it. In your code, you start using the array:
obj[count].assign(last, first, num,bal);
before you make it.
obj=new Account[MAX];
That just doesn't make sense.
While we're here,
void main(int argc, char *argv[])
is absolutely wrong in C++ and if your compiler doesn't complain, throw it away and get a better one for free.
main returns an
int, like this:
int main(int argc, char *argv[])
I misunderstood; I thought you were reading in a value that told you how many there would be. What you'll have to do is allocate space for, say, ten, and then if that's not enough, allocate space for ten more, and so on.
Here's some pseudocode:
Allocate an array of ten.
Start reading - if reach ten, create new array of twenty, copy the first ten across, delete original array, and carry on with new array.
If reach twenty, create new array of forty, copy the first twenty across, delete original array, and carry on with new array.
If reach forty, create new array of eighty, copy the first forty across, delete original array, and carry on with new array.
...
and so on.
This would be much, much easier with a proper grown-up C++ container class such as vector, but if you're forced to do it this way, you're stuck with it. If you can use proper containers, just use a vector.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
int main()
{
int count=0;
ifstream infile;
vector<obj> Account;
while(!infile.is_open()){
{
//code to open
}
while(!infile.eof())
{
infile>>last>>first>>num>>bal;
obj temp;
Account.push_back(temp);
Account[count].assign(last, first, num,bal);
count++;
}
}
|