   holding chars
  I''m trying to make a program where it holds someones name. I tried this but I know its wrong, dont know what to do.......   #include <iostream>using namespace std;class nameHold{public:nameHold();~nameHold() {};char setName(char name) { itsName = name; }char getName() const { return itsName; }private:char itsName;};int main(){nameHold personName;char enterName[50];cout << "Enter your name: ";cin >> enterName;personName.setName(enterName);cout << "Your name is: " << personName.getName() << "\n";return 0;}    Signature===================================================Me: So do you know any computer languages?Him: Ummmm.......yeah, I used to know l337 talk.Me: lol, um okkkkkkkkk. 
quote:Original post by TheOne1  I''m trying to make a program where it holds someones name. I tried this but I know its wrong, dont know what to do....... #include <iostream>using namespace std;class nameHold{public:nameHold();~nameHold() {};char setName(char name) { itsName = name; }char getName() const { return itsName; }private:char itsName;};int main(){nameHold personName;char enterName[50];cout << "Enter your name: ";cin >> enterName;personName.setName(enterName);cout << "Your name is: " << personName.getName() << "\n";return 0;} Signature===================================================Me: So do you know any computer languages?Him: Ummmm.......yeah, I used to know l337 talk.Me: lol, um okkkkkkkkk.   ]char setName(char name)should bechar setName(char name[50])andchar itsNameshould bechar itsName[50]and use strcpy() here : itsName = name should be strcpy(itsName,name); ;  He''s correct, but it would be a lot easier if you used the standard string class instead of old C character arrays. You won''t have to deal with the strcpy function (standard strings use plain old = for copying/assignment) or use hacks like limiting the name to 50 characters.You also don''t have to deal with the confusing (for beginners anyway) issue of arrays vs pointers.And finally your setName function should be declared with a return type of void, since it returns nothing. ;  ?!?!?!?!?! theres a string class??? no more null chars to enter? and all that crap?? wow... (btw...all ive taken b4 was just like comp science 110 so ima noob) ;quote:Original post by Ademan  ?!?!?!?!?! theres a string class??? no more null chars to enter? and all that crap?? wow... (btw...all ive taken b4 was just like comp science 110 so ima noob)   I recommend learning C-strings before you start using the STL string class. By learning regular C-strings you learn a lot about manipulating memory and how arrays work.In your actual projects though, definitely use the string class. It will save you a LOT of debugging and coding time. ;  I''ve seen the standard string class in the book C++ in 21 days...or a more limited version.....anyways I was confused!is this it?: #include <iostream>#include <string.h>using namespace std;class String{public:String();String(const char * const);String(const String &);~String();char & operator[](unsigned short offset);char operator[](unsigned short offset) const;String operator+(const String &);void operator+=(const String &);String & operator=(const String &);unsigned short GetLen() const { return itsLen; }const char * GetString() const { return itsString; }private:String (unsigned short);char * itsString;unsigned short itsLen;};//default constructor creates string of 0 bytesString::String(){itsString = new char[1];itsString[0] = ''\0'';itsLen = 0;}//private (helper) constructor, used only by//class methods for creating a new string of//required size. Null filled.String::String(unsigned short len){itsString = new char[len+1];for (unsigned short i = 0; i <= len; i++)itsString[i] = ''\0'';itsLen = len;}//converts a character array to a stringString::String(const char * const cString){itsLen = strlen(cString);itsString = new char[itsLen+1];for (unsigned short i = 0; i < itsLen; i++)itsString[i] = cString[i];itsString[itsLen] = ''\0'';}//copy constructorString::String(const String & rhs){itsLen = rhs.GetLen();itsString = new char[itsLen+1];for (unsigned short i = 0; i < itsLen; i++)itsString[i] = rhs[i];itsString[itsLen] = ''\0'';}//destructor, frees allocated memoryString::~String(){delete [] itsString;itsLen = 0;}//operator equals, frees existing memory//then copies string and sizeString& String::operator =(const String & rhs){if (this == &rhs)return *this;delete [] itsString;itsLen = rhs.GetLen();itsString = new char[itsLen+1];for (unsigned short i = 0; i < itsLen; i++)itsString[i] = rhs[i];itsString[itsLen] = ''\0'';return * this;}//nonconstant offset operator, returns//reference to charcter so it can be changed!char & String::operator[](unsigned short offset){if (offset > itsLen)return itsString[itsLen - 1];elsereturn itsString[offset];}//constant offset operator for use//on const objects (see copy constructor)char String::operator [](unsigned short offset) const{if (offset > itsLen)return itsString[itsLen-1];elsereturn itsString[offset];}//creates a new string by adding current//string to rhsString String::operator +(const String& rhs){unsigned short totalLen = itsLen + rhs.GetLen();String temp(totalLen);unsigned short i;for ( i = 0; i < itsLen; i++ )temp[i] = itsString[i];for (unsigned short j = 0; j < rhs.GetLen(); j++, i++)temp[i] = rhs[j];temp[totalLen] = ''\0'';return temp;}//changes current string, return nothingvoid String::operator +=(const String& rhs){unsigned short rhsLen = rhs.GetLen();unsigned short totalLen = itsLen + rhsLen;String temp(totalLen);unsigned short i;for( i = 0; i < itsLen; i++ )temp[i] = itsString[i];for (unsigned short j = 0; j < rhs.GetLen(); j++, i++)temp[i] = rhs[i-itsLen];temp[totalLen] = ''\0'';*this = temp;}int main(){String s1("initial test");cout << "S1:\t" << s1.GetString() << endl;char * temp = "Hello World";s1 = temp;cout << "S1:\t " << s1.GetString() << endl;char tempTwo[20];strcpy(tempTwo,"; nice to be here!");s1 += tempTwo;cout << "tempTwo:\t" << tempTwo << endl;cout << "S1:\t " << s1.GetString() << endl;cout << "S1[4]:\t" << s1[4] << endl;s1[4] = ''x'';cout << "S1:\t" << s1.GetString() << endl;cout << "S1[999]:\t" << s1.GetString() << endl;String s2(" Another string");String s3;s3 = s1+s2;cout << "S3:\t" << s3.GetString() << endl;String s4;s4 = "Why does this work?";cout << "S4:\t" << s4.GetString() << endl;return 0;}[\source];quote:Original post by TheOne1  is this it?:(snip)Not quite; that''s a custom string class. The standard one is in the <string> header. 