Overloading the >> operator problem splitting the input

This is a discussion on Overloading the >> operator problem splitting the input within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to overload the input operator in the following manner: I have a playing card class that ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    7

    Overloading the >> operator problem splitting the input

    Hi,

    I am trying to overload the input operator in the following manner:

    I have a playing card class that that contains two private members: rank stores the value of a card, suit stores the suit of the card.

    The >> operator should accept cards in the following manner:

    examples:
    7S
    KH
    3D

    where 7S represents the 7 of spades
    where KH represents the king of hearts
    where 3D represents the 3 of diamonds

    For the first example, internally, this would occur within an object of the playing card class:

    rank = Seven
    suit = Spades

    I have created two enum types to use:

    Code:
    enum Rank {Two ... Ace};
    enum Suit {Spades ... Hearts};
    So my challenge is I am unsure as how to split that one value from stdin into two usable values

    Code:
    istream& operator>> (istream& in, Card& c)
    {
            in >> c;            //Need help with implementation
            return in;
    }]

  2. #2
    Registered User bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87
    how about something like this, put into a function:
    Code:
      string card;
      cin >> card;
      rank = card[0];
      suit = card[1];
    Then do whatever you want with it, no need to overload the operator.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,011
    In short, you need to read the characters in, convert them in the proper members of card field values, and handle errors in formatting correctly.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operator Overloading problem
    By omGeeK in forum C++ Programming
    Replies: 34
    Last Post: 04-13-2012, 03:18 PM
  2. Operator Overloading problem
    By FMan in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2008, 12:37 PM
  3. Problem with overloading an operator.
    By apacz in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2006, 10:00 AM
  4. operator overloading problem~
    By black in forum C++ Programming
    Replies: 6
    Last Post: 07-20-2004, 09:04 AM
  5. Problem with Operator Overloading
    By bench386 in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 12:39 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21