Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do you create a string in C++ ?

Posted on 2003-02-20
5
Medium Priority
?
266 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
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

569 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