Concat LPSTR in C++

Posted by Cat Man Do on Stack Overflow See other posts from Stack Overflow or by Cat Man Do
Published on 2011-11-20T01:46:13Z Indexed on 2011/11/20 1:51 UTC
Read the original article Hit count: 469

Filed under:
|

Trying to use as basic C++ as I can to build a list of numbers from 1-52 in a random order (deck of cards). Unfortauntely, all my attempts to concat the strings and get a result end in failure. Any suggestions? NOTE: This is not homework it's something I'm using to create a game.

// Locals
    char result[200] = "";  // Result
    int card[52];           // Array of cards
    srand(time(0));         // Initialize seed "randomly"

    // Build
    for (int i=0; i<52; i++) {
        card[i] = i;  // fill the array in order
    }

    // Shuffle cards
    for (int i=0; i<(52-1); i++) {
        int r = i + (rand() % (52-i));
        int temp = card[i]; card[i] = card[r]; card[r] = temp;
    }

    // Build result
    for (int c=0; c<52; c++) {

        // Build
        sprintf(result, "%d", card[c]);

        // Comma?
        if ( c < 51 )
        {
            sprintf(result, "%s", ",");
        }
    }

My end result is always garbled text. Thanks for the help.

© Stack Overflow or respective owner

Related posts about c++

Related posts about concat