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.