Thread: Setting strings

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,666
    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,666
    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.

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