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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you use strings in old C++?

Status
Not open for further replies.

SpeakerForTheDead

Programmer
Jan 31, 2002
28
0
0
US
Okay. I have C++ 4.0 at home and I can't get strings to work. From what I've heard, this because at the time 4.0 came out, string wasn't "standard." At first I thought this had to do with the header files I was using. But does it have something to do with namespaces? Do I have to use a certain "using namespace" file?
 
<string>
using namespace std; Rocco is the BOY!!

M O T I V A T E!
 
If only it was so simple.

<string> does not exist in C++ v4.0.
And using namespace std; is automatically built in so if you try to use it, it'll give you an error message.
 
Are you saying that you can't even use array of caracters ?

Ex: char[17] = &quot;Hello everyone !&quot;;
 
<string> may not exist but how about <string.h>?? You don't need the using namespace declaration now - string has been a standard type for some time now.

I've read old books on VC++ using 4.0 and even earlier and string was a supported type in the tutorials.

If your using MFC in your project you can use CString
You are most likely missing the libraries of code that make up the std lib.

If there was a consortium for C++ you could get the libs there, otherwise check MS site. Rocco is the BOY!!

M O T I V A T E!
 
I haven't tried the char array yet. And <string.h> is missing something. It doesn't recognize strings!
 
char arrays work. But there's a function that I use frequently for loading files: c_str()

How would I use that?
 
Ha! Never mind! I DON'T use it! Simple as that!

What does c_str() do, anyway?

The syntax I use it in is

fin.open(string.c_str())
note: fin is the ifstream command I use
 
I can't tell you exactly what does c_str().
maybe it is like a cast operator.

try this little program,in it you might found what you were looking for previously.

#include <iostream>
using namespace std ;

void main()
{
string str1(&quot;Hello &quot;);
string str2(&quot;everybody !&quot;);

cout << &quot;str1 = &quot; << str1.c_str() << endl;

// append str2 to str1
str1.append(str2);

cout << &quot;str1 = &quot; << str1.c_str() << endl;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top