?
Solved

How do you create a string in C++ ?

Posted on 2003-02-20
5
Medium Priority
?
262 Views
Last Modified: 2010-04-01
I've tried using char *name = new char, which caused an error when deallocating the memory so I was advised to declare it as a pointer to an array as in

char *name = new char[100]

and then delete using

delete [] name

which is fine but what I really want is for the variable name to have no upper limit on it's number of characters so does anyone know how to do this?

Thanks
0
Comment
Question by:TomG_london
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Accepted Solution

by:
fastawdtsi earned 60 total points
ID: 7986836
There are a few ways of doing this, but this is the easiest.

Use the std::string object.  Do this by including the string hearder file in your code (#include <string>) and then creating an instance of a string (std::string name;) then just treat 'name' as a variable (std::cin >> name).

hope this helped!

good luck,
Ernie
0
 

Expert Comment

by:cranbo
ID: 7986849
Use the STL (standard template Library).  It contains a class called string which does all this for you.  In the case of Microsoft Visual Studio, it comes with its own version of the STL.  All you have to do is:

#include <string>        // Note: no .h extension

using namespace std;

...

   string  sMyString = "Hello";
   cout << sMyString.c_str();

...

I'm not sure about other compilers, although the original STL (and documentation) can be downloaded from:

http://www.sgi.com/tech/stl/download.html

Chris
0
 
LVL 6

Expert Comment

by:gj62
ID: 7986851
Tom,

You are using character arrays from C in the example above.

You should be using the ANSI string class provided as part of C++.  See
 http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm

for a good overview.  Generally, in this class you don't have to worry about managing the string size yourself - the class takes care of that.  I believe it allows for strings up to an unsigned int in length - perhaps someone else can confirm that.  
0
 

Expert Comment

by:cranbo
ID: 7986860
Use the STL (standard template Library).  It contains a class called string which does all this for you.  In the case of Microsoft Visual Studio, it comes with its own version of the STL.  All you have to do is:

#include <string>        // Note: no .h extension

using namespace std;

...

   string  sMyString = "Hello";
   cout << sMyString.c_str();

...

I'm not sure about other compilers, although the original STL (and documentation) can be downloaded from:

http://www.sgi.com/tech/stl/download.html

Chris
0
 

Author Comment

by:TomG_london
ID: 7986890
thanks very much, this explains a lot

all you comments are greatly appreciated but I'm giving the points to ernie simply because his answer was first!

Thanks again,

Tom
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question