Results 1 to 15 of 15

Thread: Combining strings for output

  1. #1

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335

    Combining strings for output

    OK im reading a book called

    C++ Weekend Crash Course

    I know in c++ console to combine a string or seperate value's
    you use
    VB Code:
    1. cout << nVal1 << " Is the same as "<< nVal2

    But how do i do this in c++ window? Is it the same?
    Also is it possible to make like a dos window? More specifically
    is it possible to make a console sort of like a window? ill post a pic in this post to explain.


    Thats the EDIT window or program in dos.
    Attached Images Attached Images  
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    cout << nVal1 << " Is the same as "<< nVal2;

    That's a common misconception. You're not concatenating strings here. You outputting several strings and other values sequentially. They simply show up next to each other in the console window, but that's not really a necessity.

    You can concatenate the string class using the + operator:
    Code:
    sting s1("Hello"), s2("World!"), s3;
    s3 = s1 + ", " + s2;
    cout << s3 << endl;
    You can also build a string from strings and other data using the stringstream classes.
    Code:
    #include <sstream>
    using namespace std;
    
    ostringstream oss;
    oss << "Hello, you're the " << nNum << "th visitor.";
    cout << oss.str();
    As for the edit app, you'll notice that "graphic" elements are really characters from the IBM charset (known as OEM charset in windows). So it's just good placement of characters (don't know how to color them though).
    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.

  3. #3

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    I cant get the below to work
    VB Code:
    1. sting s1("Hello"), s2("World!"), s3;
    2. s3 = s1 + ", " + s2;
    3. cout << s3 << endl;
    i though you just made a typo on sting = string but still no good
    VB Code:
    1. string s1("Hello"), s2("World!"), s3;
    2. s3 = s1 + ", " + s2;
    3. cout << s3 << endl;

    And the other one i tried but nothings working what did i do now?
    VB Code:
    1. #include <stdio.h>
    2. #include <iostream.h>
    3. #include <sstream>
    4. using namespace std;
    5.  
    6.  
    7.  
    8. int main (int nArgs, char* pszArgs[])
    9. {
    10.  
    11.  
    12. int nNum = 2;
    13.  
    14. ostringstream oss;
    15. oss << "Hello, you're the " << nNum << "th visitor.";
    16. cout << oss.str();
    17.  
    18.  
    19. return 0;
    20. }

    thanks and also how will this work in VC++. Win32 Application (window) or MFC
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    You will also need to include the <string> header.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Also, you must use <iostream> and <cstdio>. Actually, <cstdio> has no place in a C++ program, don't use 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

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    ok i tried but no good.
    VB Code:
    1. #include <stdio.h>
    2. #include <iostream.h>
    3. #include <sstream>
    4. #include <string>
    5.  
    6. using namespace std;
    7.  
    8. int main (int nArgs, char* pszArgs[])
    9. {
    10.  
    11. string s1("Hello"), s2("World!"), s3;
    12. s3 = s1 + ", " + s2;
    13. cout << s3 << endl;
    14.  
    15.  
    16. return 0;
    17. }
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    <iostream>, not <iostream.h>.

    <iostream.h> is outdated.
    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.

  8. #8

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    Great thanks alot
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  9. #9

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    ok how do i convert a
    string <- to a -> char ?
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you need it, you can get a const pointer using .c_str() on the string. If you want to actually get a copy, then you'll need to do:
    Code:
    char buf = new char[str.length() + 1];
    strcpy(buf, str.c_str());
    buf[str.length()] = 0; // null termination
    
    // use buf
    
    delete[] buf;
    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
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    ok i got confused in that peice of code where would i put my string?

    (edited)
    OK sorry i didnt study that to hard

    const char *ptr1 = 0;
    ptr1= vIni.data ( );

    i used the above and got it thank you so much you people are great
    Last edited by JasonLpz; Jan 27th, 2003 at 02:40 PM.
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    parksie: strcpy automatically 0-terminates the string for you.
    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.

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Does it? Force of habit I think
    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

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The CRT string functions will happily create access violation due to buffer overflows, but they manage the NUL really well.
    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.

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I think it was about 5 years ago I got into the habit of scattering null terminators around my code just in case Haven't had a problem yet

    (I really oughta start trusting the libraries...)
    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