Thread: Concatenating C style Strings Question

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Concatenating C style Strings Question

    Hi,

    I'm trying to figure out how to take the text "Say ", and combine it with the value of a float variable (99.12), so that I end up with a C style string that looks like "Say 99.12".

    Does anyone know how I'd go about doing this? I would use C++ style strings but I'm trying to mod Quake 2, and basically this function accepts a C style string, like this:

    void qbSynchConsole(char *);

    So does anyone know what's wrong with my little example program that I made as an experiment? It crashes but I don't know why. Any help would be highly appreciated.

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
        char *char1 = NULL;
        char *char2 = NULL;
        float a = 99.12;
        int dec,sign;
    
        char1 = "Say ";
        char2 =_fcvt(a,2,&dec,&sign);
    
        strcat ( char1, char2 );
    
        cout << "char1 = " << char1;
    	return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    "Say " is a string literal. You can not (usually) write to string literals.

    You need to do something like
    Code:
    char char1[30] = "Say ";
    That gives you space for 29 characters plus the terminating one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If I'm not mistaken, in C you'd use sprintf or snprintf.

    And naturally you need to have a writable string buffer large enough to hold the result.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Why not something like:

    Code:
    stringstream buf;
    float a = 99.12;
    buf << "Say " << a;
    qbSynchConsole( ( char* )buf.str( ).c_str( ) );
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question on malloc and strings
    By officedog in forum C Programming
    Replies: 20
    Last Post: 11-06-2008, 05:14 PM
  2. Question on strings
    By papagaio in forum C Programming
    Replies: 3
    Last Post: 09-19-2008, 03:15 AM
  3. A question regarding strings
    By babu in forum C Programming
    Replies: 6
    Last Post: 07-31-2007, 05:30 AM
  4. Simple Question C++ Strings
    By bobthebullet990 in forum C++ Programming
    Replies: 7
    Last Post: 05-08-2006, 02:35 AM
  5. Style / Theory question regarding client-server apps
    By Daveg27 in forum C Programming
    Replies: 3
    Last Post: 08-12-2002, 06:12 AM