Thread: Setting strings

  1. #1
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63

    Setting strings

    Im new to programming(started 2 weeks ago) and am trying to make a poker program for a first project. I am using a function rplc() aka replace to change a an inputed ID to the cards name. I was wonder how I could set an array to equal a string. I understand how it is input, but how to you set it? I have been trying to do things like [array] = [string], and with quotes and such, but its just printing out e instead of the value I put, so I know it isnt working. Would I have to do each letter seperatly set? If so, Im gonna have to find a better way, because that would take me over 700 lines just for that

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How about

    Code:
    char *suits[] = {
      "clubs",
      "diamonds",
      "hearts",
      "spades"
    };
    Then you can either do
    Code:
    char card_suit[10];
    strcpy( card_suit, cards[2] );
    Code:
    char *card_suit;
    card_suit = cards[2];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Maybe I should be more specific...

    Heres an example of the code:
    Code:
    if(cardID = 0)
    {
      [coding that changes id '0' to 'Two of Hearts']
    }
    Im having problem of what to put in there, Ive been thinking of just using an array that this part sets, and then after all 52 of these blocks(one for each card) put either return [array];(if thats proper syntax ) or just have the array global and have it print out from the array directly.

    btw: Im aware that that is 100+ lines, but copy paste works wonders for that. each letter seperate though is just way too...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If card_id is 0..51 say, then use
    card_id % 13
    to get the face value, and
    card / 13
    to get the suit value
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    I see what you mean, but it still doesnt seem to fit my coding... Heres what Im thinking about, using a global variable:
    Code:
    if(cardID = 0)
    {
      cardNAME = Two of Hearts;
    }
    Basically, I need to find a way to make that legal. Is there a way to auto set a char array to have that value, or am I gonna have to go looking more into the string header?

  6. #6
    uninteresting
    Join Date
    Jun 2002
    Posts
    66
    strings are nice, but if you don't want to use them, strcpy() is the way to go.

    Code:
    strcpy (cardNAME, "Two of Hearts");
    Should do it, assuming you have the array big enough to hold all the characters. I don't think allocating an array each time is worth it.
    *** TITANIC has quit (Excess Flood)

  7. #7
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Thanks, I used that, but now it seems to have an error of not running again. Will that function overwrite itself, or do I have to clear out the array before it will copy ontop of it again? If so, is there a compiler that will allow you to put the null value(the noob knows what it is ^^), because it seems whenever I try to, it just puts '?' instead of it in the editor window, and my compiler changes it to a '?' when it loads it, so I cant set it. I have tried setting it to NULL, but it just runs an error and doesnt work.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Assume cardNum is an int, with valid values of 1 to 52. There's no reason you couldn't use the following approach.

    if(cardNum == 1)
    cout << "Ace of spades" << endl;
    else if (cardNum == 2)
    cout << "two of spades" << endl;
    //same for 3 to 50
    else if(cardNum == 51)
    cout << "Queen of Clubs" << endl;
    else
    cout << "King of Clubs" << endl;

    It might not be the most sophisticated approach, but it allows you to convert a number representing a card to a correlated string representing a card, and the more sophisticated approaches will come later. To do it in a more sophisticated fashion I'd learn about enumerated constants, switch statements, structs/classes, overloading operators, friend functions, STL maps, etc.

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    what about something like this?
    Code:
    #include <iostream>
    
    enum { DIAMONDS, HEARTS, CLUBS, SPADES } ;
    char *strsuit[] = {
      "diamonds",
      "hearts",
      "clubs",
      "spades"
    };
    
    
    
    
    int main()
    {
      unsigned int cardsuit;
      std::cin>>cardsuit;
      if (cardsuit<=3)
        std::cout<<strsuit[cardsuit]<<std::endl;
    }
    In that case 0 is diamonds, 1 is hearts, and so on. It wouldn't be hard to allow strings to be inputted (then setting cardsuit to the appropriate number)

    You could also do something similar for the number of the card, ie
    Code:
    enum { ONE, TWO, /*...*/, JACK, QUEEN, KING, ACE }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    No need 4 arrays! How 'bout this?
    Code:
    srand(time(NULL));
    int cardnumber = rand() % 13 + 1;
    int suit = rand() % 4 + 1;
    if(cardnumber == 1)
    cout<<"Ace of ";
    else if(cardnumber == 11)
    cout<<"Jack of ";
    else if(cardnumber == 12)
    cout<<"Queen of ";
    else if(cardnumber == 13)
    cout<<"King of ";
    else
    cout<<cardnumber<<" of ";
    if(suit == 1)
    cout<<"Spades"<<endl;
    else if(suit == 2)
    cout<<"Hearts"<<endl;
    else if(suit == 3)
    cout<<"Clubs"<<endl;
    else
    cout<<"Diamonds"<<endl;
    Do that 4 more times with different variables and then u'll have the hand. In order to check if the hand is worth points the && operator is useful. Like
    Code:
    if(card1 == card2 - 1 && card2 == card3 - 1 && card3 == card4 - 1 && card4 == card5 - 1 && suit1 == suit2 && suit1 == suit3 && suit1 == suit4 && suit1 == suit5){//You got a straight with all of the cards the same suit}
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>How 'bout this?
    Are you sure that it is correct? Can you verify that it is as easily as an array based solution? A simple boundary check and array index is provably easier to get right than if..else if..else chains or a long logical expression.

  12. #12
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Hmmm... Ill try one of those if my way is completely pointless, but Ill post a short version of the code:
    Code:
    //rplc function
    //Replaces a card number id with the cards name
    //(ie. 14=Three of Diamonds)
    void rplc(int cardID)
    {
      if (cardID = 0)
      {
        strcpy (cardNAME, "Two of Hearts");
      }
      else if (cardID = 1)
      {
        strcpy (cardNAME, "Three of Hearts");
      }
      else if (cardID = 2)
      {
        strcpy (cardNAME, "Four of Hearts");
      }
      else if (cardID = 3)
      {
        strcpy (cardNAME, "Five of Hearts");
      }
    .
    .
    .
      else if (cardID = 48)
      {
        strcpy (cardNAME, "Jack of Clubs");
      }
      else if (cardID = 49)
      {
        strcpy (cardNAME, "Queen of Clubs");
      }
      else if (cardID = 50)
      {
        strcpy (cardNAME, "King of Clubs");
      }
      else if (cardID = 51)
      {
        strcpy (cardNAME, "Ace of Clubs");
      }
      else
      {
        cerr<<"Error: card ID unknown";
      }
    }
    Basically, at first when they were all if's alone, it kept doing the same thing over and over(ace of clubs, aka 51) but now with else in there, it always does Three of Hearts, aka 2. Is there a problem with the if's coding, because im garenteed thats why it isnt working right... I know it isnt having a problem reading cardID, because i havent been having the cerr go off...

    And incase you ask, cardNAME is global.

  13. #13
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Your method of assigning a numeric value to every card in the deck (ranging from 1 to 52) isn't a bad idea.. however, why not just break the deck down into 4 suits.. and 13 face values.. (ranging from 2 to ace)

    Maybe.. assign a two dimensional array: int deck[3][12]

    (or.. you can use this same idea with a two dimensional array of pointers to an enumerated list of strings...)

    where the first dimension will represent the 4 suits ([0] to [3]).. and the 13 ([0] to [12]) will represent the possible face values per suit ranging from 2 to ace.

    I would also write a couple of functions.. that will randomly return a suit.. and a face value from deck[][]

    Hope this gives you a different perspective.. if you need any help with specific code.. i could give you a couple of ideas.

    Last edited by The Brain; 07-30-2004 at 03:07 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  14. #14
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by LloydUzari
    Basically, at first when they were all if's alone, it kept doing the same thing over and over(ace of clubs, aka 51) but now with else in there, it always does Three of Hearts, aka 2. Is there a problem with the if's coding, because im garenteed thats why it isnt working right... I know it isnt having a problem reading cardID, because i havent been having the cerr go off...

    And incase you ask, cardNAME is global.
    Your problem is that you are using = instead of == to compare the ints. Change if (cardID = 0) to if (cardID == 0) and do it for every single else if also.

  15. #15
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    my Idea,
    Code:
     
    srand(time(NULL));
    int cardnumber = rand() % 13 + 1;
    int suit = rand() % 4 + 1;
    if(cardnumber == 1)
    cout<<"Ace of ";
    else if(cardnumber == 11)
    cout<<"Jack of ";
    else if(cardnumber == 12)
    cout<<"Queen of ";
    else if(cardnumber == 13)
    cout<<"King of ";
    else
    cout<<cardnumber<<" of ";
    if(suit == 1)
    cout<<"Spades"<<endl;
    else if(suit == 2)
    cout<<"Hearts"<<endl;
    else if(suit == 3)
    cout<<"Clubs"<<endl;
    else
    cout<<"Diamonds"<<endl;
    , is...err...way more easier than
    Code:
    if(cardid==0)
    strcpy(Cardname, "ace of hearts"
    and 51 more elseifs.
    LloydUzari was orriginally doing 52 else if statements for every card and returning an array at the end of the function. you could strcpy the first part of the string of mine and then append the second part of the string and cout the strcpied variable.
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d strings and setting to null
    By Fox101 in forum C Programming
    Replies: 4
    Last Post: 02-06-2008, 12:45 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. setting msvc 6 to use unicode strings
    By reanimated in forum Windows Programming
    Replies: 6
    Last Post: 01-03-2004, 10:08 PM