i have a problem with assigning the character array tempChar to strMyString.c_str();

does anyone have any ideas?

thanks a lot!!

05-10-2002

ygfperson

you are mixing tmpChar and tempChar, two separate and distinct variables.

05-10-2002

Unregistered

C strings are a bit trickier than STL strings. First of all in the declaration of tmpChar the value in the []s needs to be a constant available at compile time. Since the length of strMyString is unknown at compile time the mechanism you used is in error. Even if it wasn't the length() method of STL strings returns the number of non-null characters in the string. To declare a C string to hold all the non-null char of the STL string you need to add space for the terminating null character in addition to all the non null char so it would be strMyString.length() + 1. AT least you attempted to declare memory for tmpChar, which is more than a lot of times.

To declare just enough memory for tmpChar you would need to use dynamic memory like this;

char * tmpChar = new char[strMyString.length() + 1];

Alternatively, since I suspect you are not familiar with dynamic memory yet, you could declare tmpChar to hold as many char as conceivably necessary using static memory, like this:

char tmpChar[256];

you could replace 256 by 80 or 1000 or whatever you thought was as big as necessary.

Next, you can't assign one C string to another using the = oprerator like you can two STL strings. You need to use the strcpy() (or similar) function found in cstring header file (or string.h header depending on your compiler). It works like this:

char tmpChar[256];
//obtain strMyString like you did
strcpy(tmpChar, strMyString.c_str());

last but not least, tempChar is not declared, though I suspect that is a typo for tmpChar.

I believe you can convert tmpChar to strNewString like this:

string strNewString(tmpChar);

or like this:

strNewString = string(tmpChar);

I also believe that the STL string class has it's own built in toupper() and tolower() functions, although the names may be slightly different, to do all this for you if you wish.

05-10-2002

Unregistered

Thanks a lot for clearing up all these issues for me!
I really appreciate it.....and as someone who has just started with c++, this really helps a lot.