I need to read a file into a struct so i can print out a monthly bank statement after adding all deposits and with drawkls from another file but im having trouble reading the fileinto the array this is what i have so far..

#include <iostream> // Need for cout,cin

#include <iomanip> // Need setf,fixed,showpoint,setprecision

#include <fstream> // Needed for files

#include <cstdlib> // Needed for exit function

#include <string> // Need for string class

using namespace std;

struct PersonAcct // struct account holds customer info

{

int acct_num; // customer account number

string name; // customers name

double acct_bal; // customers account balance

};

int main ()

{

PersonAcct statement[20];

int counter;

ifstream accountsinFile;

ifstream transactioninFile;

// try to open the file

accountsinFile.open("accounts.txt",ios::in);

transactioninFile.open("transactions.txt",ios::in);

if(!accountsinFile.is_open())

{

cerr << "Account File open Error" ;

cout << " Press enter to continue" << endl;

cin.ignore();

char ch = cin.get();

return 0;

}

if (!transactioninFile.is_open())

{

cerr << "Transaction File open error " ;

cout << " Press enter to continue" << endl;

cin.ignore();

char ch = cin.get();

return 0;

}

for ( counter = 0; counter < 20; counter++)
{
accountsinFile >> statement[counter].acct_num >> statement[counter].name >> statement[counter].acct_bal;
}
cout << statement<< endl;

// keeps program open untill user enters a key

cout.setf (ios::showpoint );

cout.setf( ios::fixed);

cout << setprecision(2);

cout << "\n\n Press Enter to continue" << endl;

cin.ignore();

char ch = cin.get();

return 0;

What problem(s) are you having? You can't take your car to a repairman and tell him "my car is broke please fix it".

My probelm is that when I read the file into the array struct it only reads the first line of the file. Which i see it do by using debug via VB second is when I try ti print out the aray statement i get garbage.

What problem(s) are you having? You can't take your car to a repairman and tell him "my car is broke please fix it".

Use this to read a file line by line

char buf[20];
while(accountsinFile.getline(buf,20))
{
         cout<<buf<<endl;
}

After this you can parse the string in buf and store its contents in the data members of the struct

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.