0

I have an array of structs that contains an account number and a balance. I open a file which contains an account number and an amount that needs to be either added or subracted from the correct account. My problem is it is adding and subracting to the first account and never moves to any of the other accounts. It uses a pointer to move through the array. Any tips, hints, or comments are greatly appreciated! Thanks in advance for your help!

//**********Class Definitions/Function Prototypes**********
struct budget
{
    int budget_num;
    string name;
    double budget_value;
};
//********End Class Definitions/Funtion Prototypes********

//Declare file streams
ifstream inFile;
ofstream outFile;

int main()
{
    //Declare variables
    int SIZE = 15;
    budget c[SIZE];
    budget *ptr = c;

    //1. Open inFile - Ledger
    inFile.open("ledger.dat");

    while(!inFile)
    {
        cout << "Error opening the ledger file." << endl;
        return 1;
    }

    //Open outFile
    outFile.open("LedgerResults.txt");

    while(!outFile)
    {
        cout << "Error opening the outFile." << endl;
        return 1;
    }

    //2. Read Ledger file into array
    char space[3];
    char AcctName[26];

    while(!inFile.eof())
    {
        inFile >> (*ptr).budget_num;
        //cout << (*ptr).budget_num;

        inFile.get(space,3);
        //cout << space;

        inFile.get(AcctName,26);
        (*ptr).name = AcctName;
        //cout << (*ptr).name;

        inFile >> (*ptr).budget_value;
        //cout << (*ptr).budget_value << endl;

        ptr++;
    }

    //3.  Close Ledger file
    inFile.close();

    //4. Open inFile - Journal
    inFile.open("journal.dat");

    while(!inFile)
    {
        cout << "Error opening the journal file." << endl;
        return 1;
    }

    //5.  Read from journal file
    int AcctNum = 0;
    int RandomNum = 0;
    char Date[12];
    char JournalEntry[32];
    string MyString;
    char JournalAmount[10];
    double Amount = 0.0;

    while(!inFile.eof())
    {
        //Read information from journal file
        inFile >> AcctNum;
        //cout << AcctNum << " ";

        inFile >> RandomNum;
        //cout << RandomNum << " ";

        inFile.get(Date, 12);
        //cout << Date << " ";

        inFile.get(JournalEntry,32);
        //cout << JournalEntry << " ";

        inFile.get(JournalAmount, 10);
        //cout << JournalAmount << " ";

        //Take spaces out of string
        for(int i = 0; i < 10; i++)
        {
            if(JournalAmount[i] != ' ')
            {
                MyString = MyString + JournalAmount[i];
            }
        }

        //Convert string to double
        istringstream stm;
        stm.str(MyString);
        stm >> Amount;

        //****Problem with this loop****
        //Add/Subtract from correct account
        ptr = &c[0];

        if(((*ptr).budget_num == AcctNum))
        {
            (*ptr).budget_value = (*ptr).budget_value + (Amount);
            ptr++;
        }
        else
        {
            ptr++;
        }
    }

    //6.  Close journal file
    inFile.close();

    //7.  Display the results
    ptr = &c[0];

    for(int i = 0; i < 15; i++)
    {
        cout << (*ptr).budget_num << "  ";
        cout << (*ptr).name;
        cout << fixed << setprecision(2) << (*ptr).budget_value << endl;
        outFile << (*ptr).budget_num << "  ";
        outFile << (*ptr).name;
        outFile << fixed << setprecision(2) << (*ptr).budget_value << endl;

        ptr++;
    }

    //Close the files
    //inFile.close();
    outFile.close();

    //Exit program
    return EXIT_SUCCESS;
}
2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by np complete
0

Solved your own question ?

And as this is C++, you might want to change to class rather than keeping struct.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.