Advertisement

Using strings in C++

Started by June 23, 2000 01:03 AM
11 comments, last by Fender148 24 years, 5 months ago
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.
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
><>
-- What would Sweetness do?
Advertisement
        #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
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
+-----------------------------
Those who dance are considered insane by those who cannot hear the music.
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".
Aren''t strings arrays of char?

Thanks, Arthur(rockslave)
import money.*;#include "cas.h"uses bucks;
Advertisement
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
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
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
haha I was gonna add something but.... I don''t wanna confuse Fender any more
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 &ltstring>
#include &ltiostream>

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

This topic is closed to new replies.

Advertisement