Jump to content
  • Advertisement

Archived

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

Fender148

Using strings in C++

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
        
#include <string>

#include <iostream>

using namespace std;

int main()
{
string s;

s = "Keebler";

cout << s << endl;

return 0;
}


If you need more info, search MSDN for basic_string

Keeb



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

Share this post


Link to post
Share on other sites
you could do this

    

#include <iostream>

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!