Thread: char trouble

  1. #1
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69

    char trouble

    I don't know whats wrong. I'm trying to use a vairable of type char and I can't assign anything to it. Heres my code:
    Code:
    struct SUIT_ID
    {
           char number[20];
           char suit[20];
    };
    
    SUIT_ID deal(int card_pos)
    {
        SUIT_ID respond = {"", ""};
        int card = number_deck[card_pos];
        int suit = suit_deck[card_pos];
        if(card == 2)        {respond.number = "2";}
        else if(card == 3)   {respond.number = "3";}
        else if(card == 4)   {respond.number = "4";}
        else if(card == 5)   {respond.number = "5";}
        else if(card == 6)   {respond.number = "6";}
        else if(card == 7)   {respond.number = "7";}
        else if(card == 8)   {respond.number = "8";}
        else if(card == 9)   {respond.number = "9";}
        else if(card == 10)  {respond.number = "10";}
        else if(card == 11)  {respond.number = "Jack";}
        else if(card == 12)  {respond.number = "Queen";}
        else if(card == 13)  {respond.number = "King";}
        else if(card == 14)  {respond.number = "Ace";}
        
        if(suit == 0)        {respond.suit = "Diamonds";}
        else if(suit == 1)   {respond.suit = "Clovers";}
        else if(suit == 2)   {respond.suit = "Hearts";}
        else if(suit == 3)   {respond.suit = "Spades";}
        return respond;
    }
    If anyone could help it would be great!!!

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    i suggest using a enum to allocate numbers to user defined types

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I don't know whats wrong. I'm trying to use a vairable of type char and I can't assign anything to it.
    In short, you need to do this:
    Code:
    char number[20];
    strcpy(number, "9");
    In C++, array names are pointers. Pointers are variables that can be assigned addresses. Yes, it's true that if you do this:

    char number[] = "King";
    cout<<number;

    number will display as a string and not an address, but that is because the << operator is programmed NOT to ouput the address of the type: pointer to char. Instead, the <<operator automatically dereferences a pointer to type char, which obtains the value at that address, and displays the text. That makes it difficult to know that the name of a char array is a pointer. For arrays of other types, the << operator displays an address when you output the array name:

    int numbers[] = {1, 2, 3};
    cout<<numbers; //006BFDEC

    which makes it easy to know the array name is a pointer.

    Since, something like "9" or "King" is not an address, it can't be assigned to number. If you want to assign some text to a char array, you can use the <cstring> function strcpy():
    Code:
    char number[20];
    strcpy(number, "9");
    cout<<number<<endl;
    Last edited by 7stud; 04-09-2005 at 02:13 PM.

  4. #4
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    Thanks a lot, I had a lot of trouble with that before too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  4. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM