So I am basically a beginner. I have used c and c++ for compuatation but beyond that I have done nothing. I have a problem and I have read other forum topics and tutorials but they are just not helping. My problem is basic as I will describe. Any help would be great.
I am attempting to write an object orientated program that compares two words and determines if they are anagrams of each other. My problem is attempting to assign the user defined words to the private members of a class. I am using a set function to do this but I can not pass the char arrays to the set function correctly.
I understand that when passing char arrays we are actually dealing with pointers so I should be simply able to use the dereference operater (*) once in my set function but no matter how I arrange the syntax I get an error which is always along the lines of "invalid conversion from `char*' to `char'" or "incompatible types in assignment of `char' to `char[100]' ". Here's my relevant code;
int main(void)
{
char w1[100], w2[100];
Anagram a;
cout<<"Enter the first word: ";
cin>>w1;
cout<<"Enter the second word: ";
cin>>w2;
a.setWord(w1,w2);
a.printword(); //Function to test the assigning of words
//a.Comparison(); //Function to determine if words are anagrams.not relevant
cout<<"\nEND of Program. Press any key to close window...";getch();
return 0;
}
void Anagram::setWord(char* w1, char* w2)
{
word1=*w1; //word1 and word2 are private members of the class "Anagram"
word2=*w2;
}
In your setWord() function, the syntax you are using on the right-hand side of the equal sign will retrieve the first character in the string (if there is one.) Also, you need to check that w1 and w2 are not NULL, or you have a nasty suprise. What are the types of word1 and word2?
I've done a similair program and I can share the source if you want.
As for your problem, I recommend using std::string's here. This way you don't have to worry about pointers, you just have to assign the values of the arguments to setWord to word1 and word2 (which I also recommend you make std::string's).
If you want to keep using char arrays change your function body to this:
I dont want to use strings. This is just an exercise in trying to do it as manually as possible...if that makes sense? as for changing the function body to
1 2
word1 = w1;
word2 = w2;
It returns the error: incompatible types in assignment of `char*' to `char[100]'
I just want to clarify this. I assume you mean to use this in the main? If so, it still doesnt work because, I believe, word1 and word2 are not defined in the main but in the class and if I am to use the object 'a', they are still private so there is no direct access?
I also want to avoid the use of the string library if possible.
No, use this in your setWord function. It will copy the contents of w1 and w2 to word1 and word2. Also, it's not in the standard string library, the function was intended to be used with c-style strings, not std::string's.
Answering your question about w1 and w2 being NULL:
When you write a function that takes a pointer to anything, the first thing you must do is make sure that it isn't null. If you put your class in a library, and someone else links in your code and they call your setWord() without sending you a valid pointer, it will either be garbage or NULL and if you don't check it, you will get a very nasty core dump if you are on UNIX/Linux, or one of those "Your program has executed an illegal function ..."-type of message.
ASCII is right: You should use the string library. But it's good to know how to do the "C" side of things because you will definitely run into it!