Results 1 to 12 of 12

Thread: Newbie string addition

  1. #1

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Newbie string addition

    I know this will be easy, just that i am new to the string side of C++ (i can understand the maths parts as it's similar to VB, which i am better at)
    I just am wondering the most efficient way to do the equivalent of this:

    Dim a$, b$
    a$ = "he"
    b$ = "llo"
    a$ = a$ + b$
    Print a$
    sql_lall

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    you concentarte c strings with strcat, the string class should have a overloaded operator +
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169

    Re: Newbie string addition

    Originally posted by sql_lall
    I know this will be easy, just that i am new to the string side of C++ (i can understand the maths parts as it's similar to VB, which i am better at)
    I just am wondering the most efficient way to do the equivalent of this:

    Dim a$, b$
    a$ = "he"
    b$ = "llo"
    a$ = a$ + b$
    Print a$
    Code:
    string a("he");
    string b("llo");
    
    a += b;
    
    cout << a << endl;
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    thanks

    I think i'll use the strcat method. However, i was unaware that you could declare strings like that and use them. What versions of C++ can you do that in??
    sql_lall

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Um, you can do it in C++, C++, or C++.

    strcat is for C-style strings declared as pointers/arrays. C++ is slightly more intelligent about it.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    For the string class you need these two lines at the top of your app:

    #include <string>
    using namespace std;


    The include directive imports the file "string" into your app (which is where the string class is declared). Don't worry about the other line for now, wait until you learn about namespaces. It's not very high priority. Just remember you need it everytime you include any headers from the C++ standard library.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking One slight error

    Ok, i tried the using namespace std; thing, but it didn't work.
    Just a question, should it matter whether the i #included <string> or <string.h>, of for that matter is there any difference between <[headername]> and <[headername].h>??
    sql_lall

  8. #8
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    If there is a .h at the end, and there is another ehader without it, the one without is newer, and should be used.

    Z.

  9. #9

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking OK

    Yeah, i had to use no .h
    It solved the problem, but i couldn't cout<< the strings.
    Any suggestions. ( i just tried the code posted before, with the necessary headings, but the cout line came up with an error)
    sql_lall

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You need <iostream>, not <iostream.h> or it won't work properly.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking OK

    Thanks for your answer. Very helpful.
    BTW: your picture, with which smilies does the animation 'start' ??
    sql_lall

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It starts with the cool guy getting an idea

    It was made by Bloodeye (I think) who I haven't seen around here for ages now...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width