I'm sorry, I'm really slow..
lets say I have a class "DAD" and I want to use the pointer to the DAD class, then I want to send another char array to the function to assign the word to a char array within the class.. How can I do that? I kind of simplified my code, to get to the meat of the thing I am trying to do.. does it make sense? I know I may have other syntax wrong as well, could anyone help me?
I want to be able to send a char array to the function, then assign that word to the char array that is in the class..
class DAD
{
public:
char wordInClass[80];
void assignWord(char[]);
private:
};
int main()
{
DAD *words;
words new DAD();
char wordInMain[80] = "My name is John";
words->assignWord(char wordInMain[]);
}
void getWord(char[])
{
strcpy(wordInClass, wordInMain);
}
#include <iostream>// for use of cin, cout
usingnamespace std;
class DAD
{
public:
char wordInClass[80];
void assignWord(char* pch);
};
void DAD::assignWord(char* pch)
{
strcpy_s(wordInClass, pch);
return;
}
int main()
{
DAD *words;
words = new DAD;
char wordInMain[80] = "My name is John";
words->assignWord( wordInMain );
// test output
cout << wordInMain << endl;
cout << words->wordInClass << endl;
delete words;// don't forget this
system("PAUSE");
return 0;
}
Am I getting carried away? Sorry.
This solution allocates array space for the wordInClass dynamically on creation of a DAD object through a constructor, or through the function assignWord(). The dynamically allocated character array in the class is deleted with the DAD object using a custom destructor.
Dude, fun2code! You Rock! I forgot about adding the '::' scope solution operator? thing.. anyways.. will be trying to finish my assignment later today.. (unlikely to finish today..) but I appreciate your help so much.!
Hi Fun2Code, So far, having a progress with your help! thanks so much!
Now that I've made some progress, looking to make it more efficient..
I initially was trying to pass the argument (agrv[1]) to the class, but had failed, so i just created a new char, strcpy 'ed it then sent it to the class.. anyway I can send the **argv ? how can i do that? a sample is below of what I have..
1 2 3 4 5 6 7 8 9 10 11 12
int main(int argc, char **argv)
{
cout << "Received " << argc << " arguments...\n";
for (int i=0; i < argc; i++)
cout << "argument " << i << ": " << argv[i] << endl;
char word[80];
strcpy (word, argv[1]);
cout << word << endl;
words->assignWord (word);
}