0

I am having an issue with my function in c++ that saves a structure to a file. It appears to save everything correctly, when I open the file back up everything has been saved. But when I run the program again the loading file goes into an infinite loop for some reason, I am uncertain as to why this is happening. Any input would be welcome.

#include <iostream> 
#include <iomanip> 
#include <string> 
#include <cmath> 
#include <fstream> 

using namespace std;

struct account
{
    string acctNum;
    string name;
    float cBal;
    float sBal;
};

int menu();
char subMenu();
int loadCustomers(account[]);
void saveCusomers(account[], int);
int newCustomer(account[], int);
int deleteCustomer(account[], int);
int findCustomer(account[], int);
void deposit(account[], int);
void withdrawl(account[], int);
void balance(account[], int);
void bankBalance(account[], int);

int main()
{
    account acc[20];
    int selection;
    int numAcc = 0;

    numAcc = loadCustomers(acc);

    saveCusomers(acc, numAcc);

    return 0;
}



int loadCustomers(account acc[])
{
    ifstream inFile;
    int numCustomers = 0, i = 0;

    inFile.open("customer.dat");

    if (!inFile)
    {
        cout << "No customer file found." << endl;
    }

    else
    {   
        cout << "Customer file found..." << endl << endl;

        while (!inFile.eof())
        {
            getline(inFile, acc[i].acctNum, '#');
            getline(inFile, acc[i].name, '#');
            inFile >> acc[i].cBal;
            inFile.ignore();
            inFile >> acc[i].sBal;

            i++;
            numCustomers++;
        }

        cout << "Number of customers found in file: " << numCustomers << endl;
    }

    inFile.close();

    return numCustomers;
}

void saveCusomers(account acc[], int numCustomers)
{
    ofstream outFile;

    outFile.open("customer.dat");

    for (int i = 0; i < numCustomers; i++)
    {
        outFile << acc[i].acctNum;
        outFile << '#';
        outFile << acc[i].name;
        outFile << '#';
        outFile << acc[i].cBal;
        outFile << '#';
        outFile << acc[i].sBal;
        outFile << '#';
    }

    outFile.close();
}

I am not sure if I am saving it infinitely or why it loops infinitely, any comments would help.

4
  • 2
    I don't have the energy needed to debug your code at the moment, but I can tell you this much; this line: while (!inFile.eof()) is almost (99.99% of the time) always wrong. You call getline twice after that check. Think about that for a moment. Commented Nov 29, 2014 at 8:01
  • Your data in the file is all in one line, each field and structure separated by the '#' character. You skip some of those separators when reading the file, but not all. I recommend using something else as a structure separator, like a newline, then the reading and parsing of the data could be made easier to handle errors, including a very easy fix for the problem pointed out by @EdS.. Commented Nov 29, 2014 at 8:05
  • Since you are using getline, you will need to output an equivalent std::endl one the lines. Commented Nov 29, 2014 at 8:36
  • @cup: getline accepts a separator token, in this case, #. Commented Nov 29, 2014 at 17:40

1 Answer 1

1

try replacing

while (!inFile.eof())

with

 while(getline(inFile, acc[i].acctNum, '#'))

and remove getline(inFile, acc[i].acctNum, '#'); as first statement in you while

Sign up to request clarification or add additional context in comments.

2 Comments

Changing that helped with the infinite loop issue, but for some reason my saving function loads a bunch of addresses for the function now, the first account loads fine then the next loads the addresses.
add inFile.ignore(); after inFile >> acc[i].sBal; too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.