Jump to content
  • Advertisement

Archived

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

Wizzy

C++ Equivalent for VB's "String"?

This topic is 5563 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

can''t seem to find it the only character type variable i can find is "Char" which only stores one character... is there a C++ variable type for storing multiple characters (like VB''s string)... i''d like to use it for taking in a person''s name or whatever...

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
you need to use a pointer or an array:
char *cad = "hello";
char cad[10] = "hello";

Share this post


Link to post
Share on other sites
#include <string>
using namespace std;

Then you can declare strings using 'string varName'. If you need to use a c++ string with an old c function you'll have to use stringVar.c_str() to convert it.

Edit: Actually this will help you more than I can. ANSI String Class

[edited by - punx on March 31, 2003 9:01:21 PM]

Share this post


Link to post
Share on other sites
Theres the ANSI string class

#include <string>
using namespace std;

string myString = "This is a string";




or plain olds characters arrays terminated by a null

char string[40];

[edited by - xMcBainx on March 31, 2003 9:00:01 PM]

Share this post


Link to post
Share on other sites
The "traditional" way is to use a character array:


char str[] = "OMG it's a string!";


Unfortunately, this popular method has a lot of little oddities that will mess up a newbie. You have to research this a bit to get it right (i.e. you can't assign such strings directly, and you can't compare them with ==)

The STL comes with its own string class that behaves in a much more civilized manner.


#include <string>

int main()
{
std::string my_string;
cin >> my_string;

cout << "You typed \"" << my_string << "\"\n";
return 0;
}


EDIT: Wow, beaten to it several times over!
Mine's the most thorough, though!


[edited by - micepick on March 31, 2003 9:00:58 PM]

[edited by - micepick on March 31, 2003 9:01:13 PM]

Share this post


Link to post
Share on other sites
There are two (actually four) objects you need to work with.
The first is std::string and the second is std::stringstream

std::string manages a string buffer (an array of characters).
std:stringstream manages the formating of input into a string buffer.


  
#include <string>
#include <sstream>

#include <iostream>

void test()
{
std::stringstream ss;
ss << "Hello";
ss << "" << "World!";
std::string str = ss.str()
std::cout << str.c_str();
}


.str() gets a std::string from the string-stream, and .c_str() get a const char* from std::string that can be sent out the standard out (via cout).

There are also wide versions of all of those objects, VB by default uses UNICODE, which is the wide version of strings. C by default uses ANSI strings. With C++ you pick either or.


  
#include <string>
#include <sstream>
#include <iostream>

void test()
{
std::wstringstream ss;
ss << "Hello";
ss << "" << "World!";
std::wstring& str = ss.str();
std::wcout << str.c_str();
}

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!