Results 1 to 9 of 9

Thread: Question!

  1. #1

    Thread Starter
    Hyperactive Member AAG's Avatar
    Join Date
    Aug 2000
    Location
    United States
    Posts
    411

    Exclamation Question!

    Got another quick question. In vb6, to combine more than 1 string of data into a single string, you would do the following

    string = "Statement1" & AnotherString & "Statement2"

    how would this be accomplished in c++ ?? I've tried the following

    Code:
    char * string
    
    string "Statement1", string2, "Statement2";
    the above simply outputs "Statement1", and nothing else.

    then i tried

    Code:
    string "Statement1" string2 "Statement2";
    The above gets an error.

    How do i do it then ?

    Thanx for any input.

  2. #2
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    #include <string>
    using namespace std;
    string s1 = "hello ";
    string s2 = "world";
    strcat(s1, s2);
    Alcohol & calculus don't mix.
    Never drink & derive.

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Not quite, the strcat() function is used for standard char* strings, not strings made with the string class. With the string class the + operator is overloaded and works just like the & operator in VB.

    You can concatenate strings using either the strcat() function, or the sprintf() function. sprintf() is possibly easier to use and more flexible. Look them up in the help files, they're quite simple to use, so it's probably not worth me explaing them both here.
    Harry.

    "From one thing, know ten thousand things."

  4. #4

    Thread Starter
    Hyperactive Member AAG's Avatar
    Join Date
    Aug 2000
    Location
    United States
    Posts
    411

    is sprintf function used in win32 programming ?

    I've only seen it in console programming and not win32. I was sure thier was a function such as the & in vb. I've seen it before, but don't remeber how to do it, its been about 5 months since i've done any c++ programming and i've been programming in vb6, but now want to program the media player i've created thru pure API in vb6 to c++. the API part isn't hard, its just this little problem. I looked up the sprintf function, and didn't really understand its use the way you wanted me to use it. sorry, please help.

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    You're thinking of printf(), not sprintf(). 'printf' is short for 'print formatted', and the plain printf() function does just that, prints a formatted string to the console, but as you say it isn't used in Windows programming since there is not console. sprintf(), however, prints to a string (that's what the s stands for). You can concatenate two strings using either of these lines of code:
    Code:
    // using strcat()
    strcat(charstring1, charstring2);
    
    // or using sprintf()
    sprintf(charstring1, "%s%s", charstring1, charstring2);
    sprintf() looks more complicated, and it is in this very simple case, but it comes in very handy when you want to put together different variables and different datatypes into one formatted string.

    I was sure thier was a function such as the & in vb.
    I don't know what you mean by this really, since & isn't a function, it's an operator. There is no operator to do this in C++ because a char* string is a basic datatype, and doesn't have an operator overloaded to concatenate it. Char strings are just pointers (to arrays) in C++. If you use a string classs such as the STL string class, or the MFC CString class (I think that's an MFC class but not certain), then they have operators overloaded to do different things, and one of them is concatenation. As I said previously, the STL string class uses the + operator to do this. So this works:
    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::string str1, str2, str3;
        str1 = "A string... ";
        str2 = "Another string.";
        str3 = str1 + str2;
        std::cout << str3 << std::endl;
        return 0;
    }
    That's code for a console app, obviously, but the class works the same way in any kind of app.
    Harry.

    "From one thing, know ten thousand things."

  6. #6

    Thread Starter
    Hyperactive Member AAG's Avatar
    Join Date
    Aug 2000
    Location
    United States
    Posts
    411

    Thanx but....

    Yo thanx, but the strcat function only joins to strings into one, i need to join quite a few into one. for example, in vb6, i would need to do this

    Code:
    string = "Hello " & string1 & "Hello1 " & string 2 & "Hello3 "
    i need to do this is c++, of course its not gonna be hello's LOL.

    and theres more than 4 strings that i need to join. the data in the strings is different everytime, otherwise i wouldn't need to join them. and as for sprintf, i'm getting illegal operations when the sprintf function is ran. it compiles fine, but errors out. is there any way to join more than 2 at a time? Sorry for the trouble.

  7. #7

    Thread Starter
    Hyperactive Member AAG's Avatar
    Join Date
    Aug 2000
    Location
    United States
    Posts
    411

    OOPS

    ok, i made an error in how i was using strcat. now i'm doing it like this

    char * string

    strcat(string, "Hello1 ");
    strcat(string, string2);
    strcat(string, "Hello2 ");
    strcat(string, string3);
    strcat(string, "Hello3 ");

    the only problem the strings i try and combine with strcat don't appear in when i get the data from 'string'

    what am i doing wrong ?

  8. #8
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    That's exactly why I suggested sprintf(), because it can simplify joining lots of variables together into a string.

    I haven't seen your code obviously so I can only guess at why you're getting errors. I expect it's because you haven't allocated a proper buffer for the string, or it's not large enough to take the string you want. You need to declare the string you're going to print to properly as an array, or if you want it as a pointer then you must allocate enough space for your string. Generally you can guess a sensible maximum for the length of the string you're gonna get:
    Code:
    char buffer[200]; // allocate a buffer for 200 characters
    sprintf(buffer, "(your format string here)", YourVariablesHere); // print the string to the buffer
    If you can't fix the errors, post your code.
    Harry.

    "From one thing, know ten thousand things."

  9. #9
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    I posted that before I saw your previous post.

    You need to allocate a proper buffer for strcat() too.
    Harry.

    "From one thing, know ten thousand things."

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