DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Addition of Stings in C++

  1. #1
    Join Date
    Sep 2006
    Posts
    7

    Talking Addition of Stings in C++

    hi i just started learning c++. I have been using turing and java but i thought its always best to start learning as a total newbie :P

    well i was trying to make a program using functions and i had some problem adding strings e.g.

    if string a is "hello"
    string b is "world"

    string c should add a and b so its stores the string "helloworld"

    the + thing doesnt work like it does in java or turing..

    any help?

    im using visual studio .net 2003

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    C++ std::string is not the same as Java's String.

    You need to #include <string>

    then:

    using std::string;
    string a = "hello";
    string b="world";
    string c=a+b;
    Danny Kalev

  3. #3
    Join Date
    Sep 2006
    Posts
    7
    Quote Originally Posted by Danny
    C++ std::string is not the same as Java's String.

    You need to #include <string>

    then:

    using std::string;
    string a = "hello";
    string b="world";
    string c=a+b;
    o wow thanx :D

    its funny how string is common in programs and c++ has a separate header for it

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    Well, that's C++. You need to #include the specific libraries you're program will use. I don't think Java is very different in this respect. You also need to import the right packages in Java if you want to use them, don't you?
    Danny Kalev

  5. #5
    Join Date
    Sep 2006
    Posts
    7
    yea but atleast u dont have to import stuff for the very basic things like strings and stuff

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    Well, C++ isn't Java for bad or good. The best way to learn a new language is to start thinking in that language instead of looking for the former language's features. BTW, if you want strings without header files you can use char *. Not that I would recommend it...
    Danny Kalev

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    you cannot do much in C++ without including the correct headers, lesson #1. The same 10 or 15 headers will unlock almost everything in the language, and you can do a LOT with only 5 or 6 of them.

    The string one makes sense: until the standard string, there were 20 or more compiler/vendor/home-rolled/opensource/etc string libraries out there (MFC still supports their own string mess & has functions that want that object instead of std one). Adding it to the core language would break older programs that used the keyword "string" to mean one of those older libraries. Also, the C and C++ "way of doing stuff" is to include the stuff you need and to leave out what you don't need. Adding one thing to core language but not another would be even more confusing. (Ok we added strings, do we add math? vectors? everything & bloat it up? where are the lines drawn?)

    I suppose one could take an older language that did all the hard work of resolving all these issues, copy it and clean it up, and claim that your new language was "better" because it forced the current popular programming paradigm onto the programmer, forced portability at the cost of performance, or had whatever other "features", but this has been done before and it created as many problems as it solved. What Im trying to say here is that no language is yet the end all be all way to express your ideas, your sort of stuck with new languages that severely limit your options or older ones that have some strange "historical" things but give you the freedom to do your job. Both have a place in todays programming world, and likely will for decades to come... roll with it and soon the reasons things are done the way they are will begin to make sense.

Similar Threads

  1. Java Swing Calculator Panel Addition Probelm
    By hemanthjava in forum Java
    Replies: 0
    Last Post: 10-14-2006, 02:29 PM
  2. How do you add multiple time stings?
    By Code_Writer in forum VB Classic
    Replies: 11
    Last Post: 08-14-2005, 08:50 PM
  3. Windows XP Home Addition
    By John O. in forum .NET
    Replies: 0
    Last Post: 05-16-2002, 08:41 AM
  4. Windows XP Home Addition
    By John O. in forum .NET
    Replies: 0
    Last Post: 05-16-2002, 08:37 AM
  5. Addition to the VB Community
    By Yair Alan Griver [MSFT] in forum .NET
    Replies: 194
    Last Post: 02-09-2002, 12:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center