Thread: Strings in C++?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    18

    Question Strings in C++?

    Hi Everyone,
    I'm new here, so please don't be so harsh on me hehe.

    So basically, I went out and purchased "C++ For Dummies, 5th Edition", and read the variables section. It says for strings, you use it in the following format:

    Code:
    string szMyString;
    However, when I put this in, I get a compiler error. I don't think string is recognised as a variable type because it doesn't get highlighted.

    So I was wondering, what's going on? Does C++ even support strings? Is there some sort of different header apart from the iostream I need to include?

    Oh, happy new year everyone, hope you've all had an excellent Christmas.

    Thanks .

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to #include <string>. Also, unless you have a using directive like using namespace std; or a using declaration like using std::string;, you should fully qualify the name as std::string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    18
    Quote Originally Posted by laserlight View Post
    You need to #include <string>. Also, unless you have a using directive like using namespace std; or a using declaration like using std::string;, you should fully qualify the name as std::string.
    Thanks a lot!
    Have an excellent new year.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    The cool thing about strings are that you can even access individual characters like char string arrays, like so: szString[i];

    If you need to convert to char array: szString.c_str();
    And if you need to cut up sections, use .substr()!

    good luck!
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's not cool, that's the biggest headache in trying to produce a std::string-compliant proper unicode-based string implementation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    18
    Nice Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

Tags for this Thread