OK I have a function that calls an array of class types and I need to remove any duplicates from the array. That is what the function is for.
So I created an operator overload for the '+' operator and made a 'Changebalence()' mutator that add two doubles for me while still keeping the class structure.

And they work quite well for what I need to do. The problem arrises when I start a 'for' loop to go through my array, and then an 'if' statement finds two equal class structures it adds the double and send s the result back. When it does this it does not add it corectly.

yet when I manually test the Fuction and the '+' operator they work perfectly. I dont know what is going on. Can someone please clarify what is happening and what needs to be done to fix this.

Here is my code.

Header File:

// Loanc.h

#pragma once
#include <iostream>
using namespace std;

class Loanc{	// TODO: Add your methods for this class here.
	public:
	
	Loanc();
	Loanc(char f, char s, double b);

	char getFirst();
	char getSeccond();
	double getBalence();

	double changeBalance(double ch);

	Loanc operator+(const Loanc & l) const;

	bool operator<(const Loanc & l)const;
	bool operator>(const Loanc & l)const;
	bool operator<=(const Loanc & l)const;
	bool operator>=(const Loanc & l)const;
	bool operator!=(const Loanc & l)const;
	bool operator==(const Loanc & l)const;
	friend ostream & operator<<(ostream & os, const Loanc & l);
	
	
	private:
	
		char first;
		char seccond;
		double balence;
	};

Decloration file:

#include "StdAfx.h"
#include <iostream>
#include "Loanc.h"

Loanc::Loanc(){
first =  'A';
seccond =  'A';
balence = 0.0;
}

Loanc::Loanc(char f, char s, double b){
first = f;
seccond = s;
balence = b;
}
char Loanc::getFirst(){
return first;
}
char Loanc::getSeccond(){
return seccond;
}
double Loanc::getBalence(){
return balence;
}

double Loanc::changeBalance(double ch){
	double temp = 0;
	balence = balence + ch;
		if(balence >= 0){
			return 0;
		}
		if(balence < 0){
			balence = temp;
			balence = 0;
			return temp;
		}
}
Loanc Loanc::operator+(const Loanc & l)const{
Loanc result;
result.balence = balence + l.balence;
return result;
}
bool Loanc::operator<(const Loanc & l)const{
			if(first < l.first){
		return true;}
			if(first == l.first && seccond < l.seccond){
		return true;
			}else{
		return false;
	}
	
}
bool Loanc::operator>(const Loanc & l)const{
			if(first > l.first){
		return true;}
			if(first == l.first && seccond > l.seccond){
		return true;
			}else{
		return false;
	}
}
bool Loanc::operator<=(const Loanc & l)const{
			if(seccond == l.seccond && first == l.first){
		return true;
			}
			if(first < l.first){
		return true;}
			if(first == l.first && seccond < l.seccond){
		return true;
			}else{
		return false;
	}
}
bool Loanc::operator>=(const Loanc & l)const{
			if(seccond == l.seccond && first == l.first){
		return true;
			}
			if(first > l.first){
		return true;}
			if(first == l.first && seccond > l.seccond){
		return true;
			}else{
		return false;
	}
}
bool Loanc::operator!=(const Loanc & l)const{
			if(seccond == l.seccond && first == l.first){
		return false;
			}else{
		return true;
	}
}
bool Loanc::operator==(const Loanc & l)const{
			if(seccond == l.seccond && first == l.first){
		return true;
			}else{
		return false;
	}
}
ostream & operator <<(ostream & os, const Loanc & l){
os << l.first << l.seccond << " $" << l.balence;
return os;
}

Main cpp File:

#include "stdafx.h"
#include <iostream>
#include <cmath>
#include "Loanc.h"
#include <fstream>
#include <string>

using namespace std;

Loanc* readData(string fname, int & arrSz);
void printArray(Loanc arr[], int sz);
void sortLoan(Loanc arr[], int sz);
Loanc* removeDup(Loanc inArr[], int inArrSz, int & outArrSz);
void saveData(string fname, Loanc arr[], int sz);



int main(){

cout << "Test to Create and display Loans: " << endl;
Loanc l1;
Loanc l2('H', 'A', 700);
Loanc l3('H', 'X', 400);
cout << "Add $500 to AA, returns " << l1.changeBalance(500) << ", " << l1 << endl;
cout << "Subtract $500 from HA, returns " << l2.changeBalance(-500) << ", " << l2 << endl;
cout << "Subtract $600 from HX, returns " << l3.changeBalance(-600) << ", " << l3 << endl;
cout << l1 + l2;
cout << "END LOAN TEST." << endl;

cout << "Please enter the file of customers: ";
string fname;
cin >> fname;

int resultSize = 0;
int fileSize = 0;
	//Read test file and print it
	Loanc* fileArr = readData(fname, fileSize);
	if (fileArr != NULL){
		printArray(fileArr, fileSize);
		
	}else{
		cout << "File not read proporly";
	}

	sortLoan(fileArr, fileSize);
printArray(fileArr, fileSize);

cout << endl;
	
Loanc* newfileArr = removeDup(fileArr, fileSize, resultSize);
	printArray(newfileArr, resultSize);

cout << endl;



// Free up dynamic memory
		delete[] fileArr;
		delete[] newfileArr;
	system("pause");
return 0;
}

Loanc* readData(string fname, int & arrSz){
	
	Loanc* result = NULL;
	arrSz = 0;
	char f;
	char s;
	double bal;
	
	// Create a file object and open the file
	ifstream inStream;
	inStream.open(fname.c_str());
	
	// Only process file if opening it is successful
	if(!inStream.fail()){
	inStream >> arrSz;
	result = new Loanc[arrSz];

		// Read file contents into result, now that size is known
		 //create results array
		for(int i = 0; i < arrSz ; i++){
			inStream >> f >> s >> bal;
			result[i] = Loanc(f,s,bal); 
		}
			inStream.close(); //don't forget to close file
	}
	return result;
}
void printArray(Loanc arr[], int sz){
	cout << "{";
	for (int i = 0; i < sz; i++){
		cout << arr[i];
		if (i < sz - 1){
			cout << ",";
		}
	}
	cout << "}";
}

void sortLoan(Loanc arr[], int sz){
int indexOfMin;
int i;
int j;

for(i = 0;i< sz-1; i++){
	indexOfMin = i;

	for(j=i+1; j<sz; j++)
		if(arr[j] < arr[indexOfMin])
			indexOfMin = j;

	swap(arr[i], arr[indexOfMin]);
}
}
void swap(Loanc&x, Loanc&y){
Loanc temp;
temp = x;
x = y;
y = temp;
}
Loanc* removeDup(Loanc arr[], int arrSz, int & resultSz){
		Loanc sum;
	Loanc* result = NULL;
	Loanc* temp = new Loanc[arrSz];
	int next = 0; //records the next element of the results array
	
	cout << arr[0] << arr[1] << arr[2]<< endl;
	
	for(int i = 0; i < arrSz; i++){
		// Check to see if the next value is the same
		// Note that this is not done if i is the last value
		if(i < arrSz - 1 && arr[i] == arr[i+1]){
			cout << arr[i] << endl;
			cout<< arr[i+1] << endl;
			arr[i].changeBalance(arr[i+i].getBalence());
			arr[i+1] = arr[i];
			cout << arr[i] << endl;
			cout<< arr[i+1] << endl;
		}
		// Process value once counted, or end of array reached
		else{
				temp[next] = arr[i];
				next++;
			}
	}//for


	// Now that the process is complete we know how many modes there are
	// So the results array can be created and the contents of temp
	// transferred to it
	result = new Loanc[next]; // make new array
	// Copy temp to result
	for (int n = 0; n < next; ++n){
		result[n] = temp[n];
	}
	resultSz = next; //set the size of the result array
	delete[] temp; //free up dynamic memory for temp
	return result;
}

void saveData(string fname, int arr[], int sz){
	// Create a file object and open the file
	ofstream outStream;
	// Note that opening a file for writing will write over the file
	// not add to it (append to it)
	outStream.open(fname.c_str());


	outStream << sz;
	// Write array elements into file
	for (int i=0; i < sz; i++){
		outStream << arr[i] << endl;
	}
	outStream.close();
}

The function that I have issue with is in the main function called
Loanc* removeDup

here is it by itself.

Loanc* removeDup(Loanc arr[], int arrSz, int & resultSz){
		Loanc sum;
	Loanc* result = NULL;
	Loanc* temp = new Loanc[arrSz];
	int next = 0; //records the next element of the results array
	
	cout << arr[0] << arr[1] << arr[2]<< endl;
	
	for(int i = 0; i < arrSz; i++){
		// Check to see if the next value is the same
		// Note that this is not done if i is the last value
		if(i < arrSz - 1 && arr[i] == arr[i+1]){
			cout << arr[i] << endl;
			cout<< arr[i+1] << endl;
			arr[i].changeBalance(arr[i+i].getBalence());
			arr[i+1] = arr[i];
			cout << arr[i] << endl;
			cout<< arr[i+1] << endl;
		}
		// Process value once counted, or end of array reached
		else{
				temp[next] = arr[i];
				next++;
			}
	}//for


	// Now that the process is complete we know how many modes there are
	// So the results array can be created and the contents of temp
	// transferred to it
	result = new Loanc[next]; // make new array
	// Copy temp to result
	for (int n = 0; n < next; ++n){
		result[n] = temp[n];
	}
	resultSz = next; //set the size of the result array
	delete[] temp; //free up dynamic memory for temp
	return result;
}

I place all the cout calls to see what was happening but they are not relative to the issue.

Basically, don't try to do two things at once.

You have (A) an array of Loanc and want (B) another array of Loanc.

So

int next(0);
temp[0]=arr[0];       // MUST remember that you don't test the first
for(int i=0;i<arrSz-1;i++)
   if (arr[i]!=arr[i+1])
      temp[next++]=arr[i+1];

If arr is not sorted then you will fail. You need a check to see if the size is zero. You would be a millions time better off using a std::vector or list, (which can be resized). It would also be much
more pleasant to calculate the correct size first before allocating
temp.

Please on future post, create a short test version.

Finally, if this is going to work. You will need to improve your memory handling, it is not obvious that you free the memory you allocate.

I dont understand how I could be doing two things at once or how to implement that code. I really just need a reason why the balences are not being added up properly.

When I remove that line, the array I want returned is populated properly, I just have the wrong numbers.

If you can explain how that code will fix this problem it would be helpful. and ill make sure to take note of the other things you mentioned.

in one line answer is

Loanc operator+(const Loanc & l) const;

	bool operator<(const Loanc & l)const;
	bool operator>(const Loanc & l)const;
	bool operator<=(const Loanc & l)const;
	bool operator>=(const Loanc & l)const;
	bool operator!=(const Loanc & l)const;
	bool operator==(const Loanc & l)const;

make all the above functions global or static members.
then call them
just do it and give me the reason why it works now :)

Im sorry i dont understand. Your talking to someone that has less than 3 months under him of C++ knowledge. I dont know how to do either of those things or why that would help.. more over the WHY part. since that is really what I want to know for the whole issue.

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.