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;

Dani AI

Generated

As reported, two separate issues are visible: only the first record seems to load, and cout << statement prints garbage. Those come from how the file is read and how you try to print the array. A fixed for (0..19) will keep looping after EOF, operator>> stops a name at the first space (so multiword names get split), and streaming an array name to cout just prints a pointer, not the fields. was right to ask for specifics, and 's hint to use line-based reading is on the right track — prefer std::string/std::getline plus parsing rather than a raw char[].

A robust pattern is: read each line with std::getline, split it into tokens, treat the first token as the account number and the last token as the balance, and join the middle tokens for the name. Push each parsed PersonAcct into a std::vector so you only store actual records. Example (not a copy of earlier snippets):

#include <vector>
#include <string>
#include <sstream>
#include <iomanip>

std::vector<PersonAcct> statement;
std::string line;
while (std::getline(accountsinFile, line)) {
    if (!line.empty() && line.back() == '\r') line.pop_back(); // handle CRLF
    if (line.empty()) continue;
    std::istringstream iss(line);
    std::vector<std::string> tok;
    std::string t;
    while (iss >> t) tok.push_back(t);
    if (tok.size() < 3) continue; // malformed line
    PersonAcct p;
    p.acct_num = std::stoi(tok.front());
    p.acct_bal = std::stod(tok.back());
    p.name.clear();
    for (size_t i = 1; i + 1 < tok.size(); ++i) {
        if (!p.name.empty()) p.name += ' ';
        p.name += tok[i];
    }
    statement.push_back(p);
}

for (const auto &p : statement) {
    std::cout << std::setw(8) << p.acct_num << ' '
              << std::setw(25) << p.name << ' '
              << std::fixed << std::setprecision(2) << p.acct_bal << '\n';
}

Quick troubleshooting notes: print each raw line while debugging to confirm file layout; handle trailing \r if the file is from Windows; validate tok.size() before stoi/stod (they can throw), or use strtol/strtod for safer conversions; if names are quoted, consider std::quoted (C++14+) or parse differently. Using a vector and a while (getline) loop will let you read every record cleanly and avoid the "only first line" and "garbage" symptoms.

Recommended Answers

All 3 Replies

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.