Thread: Help: Outputting to char array with sprintf

  1. #1
    Registered User
    Join Date
    Apr 2009

    Help: Outputting to char array with sprintf

    Hi everyone,

    This is my first post, I'm hoping for a little help, as I'm getting a little burnt out on the following idea:

    I'm trying to create a char array with 312 variables. This will represent 6 packs of cards all shuffled together. (Obviously, I'm working on a card game). I'm trying to get program to a testable point, where the user can enter a number from 0-311 and obtain the values contained in my arrays.

    For example:
    User enters: 0
    Output: Ace of hearts, 1 point.

    So.... I'm using a number of arrays to accomplish this:

    #include <stdio.h>
    #include <stdlib.h>
    int d, e, j;
    d = 0;
    e = 0;
    char Six_deck1[311]; // Controlled by int j - used to store text description
    int Six_deck2[311]; // Controlled by int j  -  used to store card values
    int pointvalue[13] = {1,2,3,4,5,6,7,8,9,10,10,10,10}; //controlled by int d - All face cards worth 10 points.
    char *value[13] = {"Ace", "Deuce", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; //controlled by int d
    char *suits[4] = {"Hearts", "Diamonds", "Clubs", "Spades"}; //controlled by int e
    //m = 0;
    //Setting values to arrays
    for (j=0; j<=311; j++)
        printf("Card %d set with the %s of %s and %d point(s).\n", j, value[d], suits[e], pointvalue[d]);
            if (d>12) {d=0;e++; ;} //Resets the card value to Ace after King has been assigned and advances suit.
            if (e>3) {e=0; ;} // Resets suits to "Hearts" after all "Spades" have been used, total cards (ie:packs) determined by j.
    system ("pause");
    system ("cls"); //Need stdlib.h
    for (j=0; j<=311; j++)
    printf("Array %d has a value of %d\n\n", j, Six_deck2[j]);
    return 0;}

    So far, I can easily assign the points in the pointvalue array to my Six_deck2 array. But, this is where I'm stumped. Now, I need to add 2 outputs to a single array.

    I'm would love to use sprintf in the following manner:

    sprintf (Six_deck1, "%s of %s", value[d], suits[e]);

    But it doesn't work. Any suggestions on this? I figured this would be an 'easy' way to set my char Six_deck1 array , without painstakingly entering every card by hand.

    Thanks for any help....

    Todd V

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    I'm not entirely sure what you are trying to accomplish. First though, you're six_deck is declared as having 311 elements, so you might want to bump that up to 312. Could you possibly try using a struct?
    typedef struct _Card {
      int point_value;
      int value;
      int suit;
    } Card;
    Would that help you keep track of cards better?
    Last edited by neandrake; 04-11-2009 at 03:13 PM. Reason: forgot point value
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    Apr 2009
    Hi, thanks for the response...

    You're right, there should be 312 values in my arrays... thats my mistake. I guess i've started counting at zero as well.

    Ultimately, where i'm trying to get to with this program.... is to have the user enter any number from 0-311 and receive 2 types of feedback from the program.

    1. A text description of the card and
    2. it's point value. (The point value assignment is working fine.)

    It's the text output that's bothering me. Here's an example of what I'm looking for:

    The user could enter 0 and see:
    Ace of Hearts, 1 point

    or 100 and
    Ten of Spades, 10 points.

    or 311 and see
    King of Spades, 10 points.

    Now, I thought entering 312 card descriptions into the char array by hand would be ugly and time consuming, so I thought I'd try and have the values automatically assigned. The first 'for' loop starts a counter rolling, and it outputs the card Value (ace, deuce, etc), the Suit and the Point value to the screen. It then successfully assigns the int array: Six_deck2 with a point value for every number from 0-311.

    But with the text description ... i have no idea how i would import strings from 2 arrays (char 'suits' and char 'value') into one array (char 'Six_deck1').

    Is there a way to do this with pointers, perhaps?

    Or maybe using the output of a printf command to run into the value of a char array?

    Thanks again to anyone who reads this!

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    Your original use of sprintf is correct, look:
    #include <stdio.h>
    #include <string.h>
    int main () {
            char this[]="eeny", that[]="meany", both[64];
            sprintf(both, "%s and %s", this, that);
            return 0;
    } that's not the problem.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. help with array of char and char **
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-20-2002, 02:23 PM