Archived

This topic is now archived and is closed to further replies.

Using strings in C++

This topic is 6446 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have Visual C++ 6, and I can''t find a way to use strings, there is a string.h in my include folder but when I use it in an include I can''t figure out how to use strings. I have a string library from another source, but this is just bugging me. Please don''t laugh at me . Thanks.

Share on other sites
string.h simply includes functions like:
strcpy(), strcmp(), ect which work on char *''s

if you want to declare a string like
string str1, str2;

then you have to provide your own implementation....or use CString

"If at first you DO succeed...try not to look astonished!!"

BASSOFeeSH@aol.com

BASSOFeeSH
><>

Share on other sites
  #include #include using namespace std;int main(){ string s; s = "Keebler"; cout << s << endl; return 0;}

Keeb

Edited by - Keebler on June 23, 2000 2:55:42 AM

Share on other sites
you could do this

  #include void main(){char *name;*name = "Keebler";cout<< name;}

+-----------------------------
Come, help me program my 3d game, i just picked up a new copy of Learn C++ in 21 days!
--Unknown Newbie
+-----------------------------

Share on other sites
C doesn''t support strings. Never has. Never will. You CAN have arrarys of char, however.

As for C++, MFC has a class called CString, but you can''t access it through "string.h".

Share on other sites
Aren''t strings arrays of char?

Thanks, Arthur(rockslave)

Share on other sites
C style strings are really arrays of ints (because of type promotion). Anyway, for c style string you should allocate space for a char pointer to a string.

int main()
{
char *str = (char *)calloc(how_many ,sizeof(char));
printf(str);

return 0;
}

For a string class, check up on the STL.

Buster:
What do you think a string is? Many characters together, or an array of chars.

There is a string class for in the STL.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

Edited by - ImmaGNUman on June 23, 2000 8:39:10 PM

Share on other sites
C strings are not arrays of ints. Type promotion applies to function parameters (and maybe a few other places), but definately not to arrays.

Rock

Share on other sites
haha I was gonna add something but.... I don''t wanna confuse Fender any more

Share on other sites
The original poster did not even mention C strings, so I don''t know why people are arguing about C supporting std::string. STL is part of the C++ standard, so you can count on std::string being there like part of the language.

The neat part about C++ is that you can use a class to wrap a string, which is really just an array of characters. That is exactly what the STL string class does for you -- it gives you a nice way to do some common operations on strings (like appending, searching, removing, etc.) using the operators you would expect, like this:

#include <string>#include <iostream>using namespace std;void main(){string my_string = "Hello " + "world!";cout << my_string;}

The output of that program is just what you would expect: "Hello world!". If you are not sure about classes or arrays, then I would suggest getting a good book on C++ and doing some studying. You''ll need to know how to use both classes and arrays before you can understand how STL strings work. And you can always post your questions here!

terminate: Your example should either fail to compile or crash when it is run... Never use the dereferencing operator (*) on a pointer that doesn''t point to anything.

- null_pointer
Sabre Multimedia

Share on other sites
quote:
Original post by ImmaGNUman

C style strings are really arrays of ints (because of type promotion). Anyway, for c style string you should allocate space for a char pointer to a string.

< Magmai takes the bong away from ImmaGNUman, and bogarts it for himself>

Must be some good stuff too

Share on other sites
quote:
Original post by Rock2000

C strings are not arrays of ints. Type promotion applies to function parameters (and maybe a few other places), but definately not to arrays.

Rock

Unless, of course, we are talking about Unicode and wchar_t!

Share on other sites
Well, I''m sure we aren''t talking about wide characters, and type promotion wouldn''t apply there either, which was my main point. But if you want to be technical, they still aren''t arrays of ints. They are short ints.

Rock