Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with charArray class

Status
Not open for further replies.

Roaders

Programmer
Aug 5, 2001
38
GB
I'm trying to set up a charArray array to load info from a file into and use ==, != operators and so on on it. Here is my test file:

//test.cpp

//chrArray test file

#include "chrArray.h"
#include <iostream>
using namespace std;

void main(){
chrArray newChar(10);
cout << newChar.getCharArray() << endl;

newChar = &quot;hi&quot;;
cout << newChar.getCharArray() << endl;

bool testOut = (newChar==&quot;hi&quot;);
cout << testOut << endl;
testOut = (newChar==&quot;by&quot;);
cout << testOut << endl;

testOut = (newChar!=&quot;hi&quot;);
cout << testOut << endl;
testOut = (newChar!=&quot;by&quot;);
cout << testOut << endl;


newChar.setCharArray(&quot;Goodbye&quot;);
cout << newChar.getCharArray() << endl;
}

###########################################################

Here is the output that I get:

10
4
²²²²¦¦¦¦¦¦¦
hi
1
0
0
1
Good ²²²²¦¦¦¦¦¦¦
Press any key to continue

###########################################################

the top two numbers are the size of array that should be created and the size that is created. Whatever size I try and create I only seem to get one 4 long and any strings longer than 3 I get strange results. What am I doing wrong.

Here is my specification file:

//chrArray.h

//chrArray header file



class chrArray{
private:
char *chrArrayPointer;
int size;
public:
chrArray(int intArrayLength);
~chrArray();
bool operator==(char charArrayComp[]);
bool operator!=(char charArrayComp[]);
void operator=(char charArraySet[]);
char *getCharArray();
void setCharArray(char charArraySet[]);
};

###########################################################

and the implementation file:

//chrArray.cpp

//chrArray Implementation file

#include &quot;chrArray.h&quot;
#include <iostream>
using namespace std;

chrArray::chrArray(int intArrayLength){
cout << intArrayLength << endl;
chrArrayPointer = new char[intArrayLength];
for (int i = 0; i < intArrayLength; i++){
chrArrayPointer = ' ';
}
size = sizeof(chrArrayPointer)/sizeof(char);
cout << sizeof(chrArrayPointer) << endl;
}

chrArray::~chrArray(){
delete chrArrayPointer;
}

bool chrArray::eek:perator==(char charArrayComp[]){
int loopCount;
int intCompArrayLength = sizeof(charArrayComp) / sizeof(char);
if (intCompArrayLength < size){
loopCount = intCompArrayLength;
}else{
loopCount = size;
}

for (int i =0; i < loopCount; i++){
if (chrArrayPointer != charArrayComp){
return false;
}
}

return true;
}

bool chrArray::eek:perator!=(char charArrayComp[]){
if (*this==charArrayComp){
return false;
} else {
return true;
}
}

void chrArray::eek:perator=(char charArraySet[]){
int loopCount;
int intCompArrayLength = sizeof(charArraySet) / sizeof(char);
if (intCompArrayLength < size){
loopCount = intCompArrayLength;
}else{
loopCount = size;
}

for (int i = 0; i < loopCount; i++){
chrArrayPointer = charArraySet;
}
}

char *chrArray::getCharArray(){
return chrArrayPointer;
}

void chrArray::setCharArray(char charArraySet[]){
*this = charArraySet;
}

###########################################################

Hope you guys can help.

Thanks

Giles Giles Roadnight
messenger: giles_roadnight@hotmail.com
ICQ: 81621370
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top