I have been working on this program for my class and I have everything working fine it seems up until I get my New Balance output. What I get for the output is below.

Date Account Type Amount New Balance
06/19 9858-5420-3025-7452 P 5500.00 -2694.35
06/19 1111-1111-1111-1111 C 430.00 -2264.35
06/20 7307-8304-0929-4295 C 36.45 -2227.90
06/20 1132-2648-4476-0945 P 130.00 -2357.90
06/20 5540-8530-4034-5532 C 275.23 -2082.67
06/20 7307-8304-0929-4295 P 250.00 -2332.67

This is what my output is suppose to look like.

Transaction Report

Date Account Type Amount New Balance

06/19 9858-5420-3025-7452 P 5500.00 5843.82
06/19 1111-1111-1111-1111 C 430.00 *** Invalid account number ***
06/20 7307-8304-0929-4295 C 36.45 243.90
06/20 1132-2648-4476-0945 P 130.00 2675.65
06/20 5540-8530-4034-5532 C 275.23 *** Credit limit exceeded ***
06/20 7307-8304-0929-4295 P 250.00 6.10CR

I went ahead and attached the header and .cpp files to this post, the only thing I can't give you is the input files which I know makes it hard to help but they are on our unix accounts and there is no other way for me to get it.

Here are the links to my assignment as well:
http://www.cs.niu.edu/~mcmahon/cs241/Assign/as2241m09.html
http://www.cs.niu.edu/~mcmahon/cs241/Assign/output2.txt

Any help on this would be greatly appreciated, thanks for your time in advance.

Attachments
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <cstring>
#include "Name.h"
#include "Date.h"
#include "CreditAccount.h"

using namespace std;

int read_accounts(CreditAccount []);
void process_transactions(CreditAccount [], int);
void print_accounts(CreditAccount [], int);
void sort_accounts(CreditAccount [], int);

int main()
{
	CreditAccount accountList[20];
	int numAccounts;

	read_accounts(accountList);
	
	numAccounts = read_accounts(accountList);

	sort_accounts(accountList, numAccounts);

	print_accounts(accountList, numAccounts);

	process_transactions(accountList, numAccounts);

	print_accounts(accountList, numAccounts);

	return 0;
}
/**************************************************************************/
int read_accounts(CreditAccount accountList[])
{
	ifstream inFile;
	inFile.open("accounts",ios::binary);
	if(inFile.fail())
	{
		cout << "Unable to open accounts file.\n";
		exit(1);
	}
	int count = 0;
	inFile.read((char*)&accountList[count],sizeof(CreditAccount));
	while(inFile)
	{
		count++;
		inFile.read((char*)&accountList[count],sizeof(CreditAccount));
	}
	inFile.close();

	return count;
}
/************************************************************************/
void process_transactions(CreditAccount accountList[], int numAccounts)
{
	ifstream inFile;
	inFile.open("trans.txt");
	if(inFile.fail())
		{
		cout << "Unable to open transactions file.\n";
		exit(1);
		}

	cout << "Transaction Report" << endl << endl;

	cout << "Date     Account               Type   Amount      New 	Balance" << endl;

	char tranDate[6];
	char tranAcct[20];
	char chargeType[2];
	double tranAmount;

	inFile >> tranDate;
	while(inFile)
		{
		inFile >> tranAcct;
		inFile >> chargeType;
		inFile >> tranAmount;
			
		int i;
		for (i=0; i < numAccounts; i++)
		{	
		strcmp(accountList[i].get_accountNumber(),tranAcct)==0;
			break;	
		}
		if(i==numAccounts)
			cout << "Searched Failed";
		else
			accountList[i].get_accountNumber();

		if(strcmp(chargeType, "P") == 0)
			accountList[i].process_payment(tranAmount);
		else
			accountList[i].process_charge(tranAmount);
		
		cout << tranDate << "    " << tranAcct << "   " << 
		chargeType << "      " << tranAmount << "      " << 
		accountList[i].get_balance() << endl;
		inFile >> tranDate;
		}	
		inFile.close();
}
/*******************************************************************/
void print_accounts(CreditAccount accountList[], int numAccounts)
{
	cout << "Credit Account Listing" << endl << endl;

	int i;

	for(i=0; i  < numAccounts; i++)
		accountList[i].print();
		
}
/**********************************************************************/
void sort_accounts(CreditAccount accountList[], int numAccounts)
{
	int i, j;
	CreditAccount bucket;

	for(i=1;i<numAccounts;i++)
		{
		bucket = accountList[i];
		for(j=i;(j > 0) && strcmp(accountList[j-1].get_accountNumber(), bucket.get_accountNumber()) > 0 ;j--)
			accountList[j] = accountList[j-1];
		accountList[j] = bucket;
		}
}
#include "CreditAccount.h"
#include "Name.h"
#include "Date.h"
#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

	CreditAccount::CreditAccount()
	{
	strcpy(accountNumber, " ");
	creditLimit = 0;
	balance = 0;
	}

	const char* CreditAccount::get_accountNumber() const
	{
	return accountNumber;
	}

	double CreditAccount::get_balance() const
	{
	return balance;
	}

	void CreditAccount::process_payment(double payment)
	{
	balance = balance - payment;
	}

	bool CreditAccount::process_charge(double charge)
	{
	if((charge+balance) > creditLimit)
		return false;
	else
		balance = balance + charge;
		return true;
	}

	void CreditAccount::print() const
	{
	cout << "Account Number: " << accountNumber << endl;
	cout << "Name: " << customerName.get_lastName() << ", "	<< 
	customerName.get_firstName() << endl;
	cout << "Expiration Date: " << expDate.get_month() << "/" <<  
	expDate.get_day() << "/"  << expDate.get_year() <<  endl;
	cout << "Credit Limit: $" << showpoint << fixed << 
	setprecision(2) << creditLimit << endl;
	cout << "Current Balance: $" << balance << endl << endl;
	}
#ifndef CREDITACCOUNT_H
#define CREDITACCOUNT_H
#include "Name.h"
#include "Date.h"
#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

class CreditAccount
	{
	private:
		char accountNumber[20];
		Name customerName;
		Date expDate;
		double creditLimit;
		double balance;
	public:
		CreditAccount();
		const char* get_accountNumber() const;
		double get_balance() const;
		void process_payment(double payment);
		bool process_charge(double charge);
		void print() const;		
	};

#endif /* CREDITACCOUT_H */
/****************************************************************
   FILE:      Date.cpp
   AUTHOR:    Justin R. Smith
   LOGON ID:  Z136340
   DUE DATE:  6/25/2009

   PURPOSE:   Contains the method implications for the Date class.
****************************************************************/


#include "Date.h"
#include <iostream>
#include <iomanip>

using namespace std;

      /*Created the default construtor for the Date class,  which sets 
	the inital values for three variable.*/      

      Date::Date()
      {
      month = 1;
      day = 1;
      year = 2009;
      }
      
      /*This method returns the month of the data member.*/

      int Date::get_month() const
      {
          return month;
      }
      
      /*This method sets the month. If the month entered is between 1
	and 12 it sets newMonth equal to that value, else it sets month
	equal to 1.*/

      void Date::set_month(int newMonth)
      {
          if (newMonth >=1 and newMonth <=12)
             month = newMonth;
          else
             month = 1;
      }
      
      /*This method returns the day of the data member*/

      int Date::get_day() const
      {
          return day;
      }

      /*If the value of newDay is between 1 and 31 it sets day equal to
	new day, else it sets day equal to 1*/
      
      void Date::set_day(int newDay)
      {
          if (newDay >= 1 and newDay <=31)
             day = newDay;
          else
              day = 1;
      }
      
      /*The method returns the year data member*/

      int Date::get_year() const
      {
          return year;
      }
      
      /*If newYear is greater than or equal to 2009 it sets year equal
	to the value of newYear, if not it sets year to 2009.*/

      void Date::set_year(int newYear)
      {
           if (newYear >= 2009)
              year = newYear;
           else
               year = 2009;
      }

      /*Used to print the date in the format of month/day/year.*/

      void Date::print() const
      {
           cout << month << "/" << day << "/" << year;
      }
/****************************************************************
   FILE:      Date.
   AUTHOR:    Justin Smith
   LOGON ID:  Z136340
   DUE DATE:  6/25/09

   PURPOSE:   does the declarion of the class Date
****************************************************************/


#ifndef DATE_H
#define DATE_H
#include <iomanip>
#include <iostream>

using namespace std;

class Date
      {
      private:
           int month;
           int day;
           int year;
	public:
		Date();
		int get_month() const;
		void set_month(int);
		int get_day() const;
		void set_day(int);
		int get_year() const;
		void set_year(int);
		void print() const;
      };
      
#endif /* DATE_H */
/****************************************************************
   FILE:      Name.cpp
   AUTHOR:    Justin R. Smith
   LOGON ID:  Z136340
   DUE DATE:  6/25/2009

   PURPOSE:   Contains method implications for the Name class.
****************************************************************/


#include "Name.h"
#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

	 /*Creates the default constructor of the the name class, which
	   sets the firstname and lastname.*/

         Name::Name()
         {
         strcpy(firstName, " ");
         lastName[0] = '\0';
         }

         /*Returns the first name data member.*/
         
         const char* Name::get_firstName() const
         {
               return firstName;
         }
         
         /*This method uses the strcpy() to copy the newFirst arguement
	   into the first name data member.*/

         void Name::set_firstName(char* newFirst)
         {
              strcpy(firstName, newFirst);
         }
         
         /*This method returns the last name data member*/

         const char* Name::get_lastName() const
         {
               return lastName;
         }
         
         /*This method uses strcpy to copy the newLast arguement into
	   the lastName member.*/

         void Name::set_lastName(char* newLast)
         {
              strcpy(lastName, newLast);
         }

	 /*Print the values of the two data members in the format
	   last name, first name.*/
         
         void Name::print() const
         {
              cout << lastName << ", " << firstName;
         }
/****************************************************************
   FILE:      Name.h
   AUTHOR:    Justin Smith
   LOGON ID:  Z136340
   DUE DATE:  6/25/09

   PURPOSE:   it does the declration of the name class
****************************************************************/


#ifndef NAME_H
#define NAME_H
#include <iostream>
#include <iomanip>

using namespace std;

class Name
   {
   private:
        char firstName[11];
        char lastName[16];

   public:
        Name();
        const char* get_firstName() const;
        void set_firstName(char []);
        const char* get_lastName() const;
        void set_lastName(char []);
        void print() const;
   };

#endif /* NAME_H */

Can you extract the problem into ~ 20 lines? Maybe make a hard coded name/date/account and then just call the function that is returning the wrong value?

assign.cpp calls read_accounts() twice -- why???

line 87: >> strcmp(accountList.get_accountNumber(),tranAcct)==0;


that should be an if statement -- without the if the loop will not work correctly. if( strcmp(accountList[i].get_accountNumber(),tranAcct)==0)

I called the function then called it again to store the number it was returning. I still been playing with the code and I don't see where my error is causing the incorrect output though. hm

That's just plain silly and grossly inefficient:icon_eek: ! -- the return value will the the same every time you call that function, so just call it once and save the return value then.

Alright I will do that do you have any suggestions as to why my output for new balance is so far off? I know its a pain with everything being in different files and I can't download the input files which complicates things even further. Any input for that issue would be great, thanks.

You mean the files are on the unix accounts at school and they won't give you a copy so that you can work with the files at home?

the permissions are set so they can only be put in our folder on unix and not downloaded onto our computers so we can use them with dev or something of the sort

This article has been dead for over six months. Start a new discussion instead.