0

I have 2 enumerations of values for playing cards, their rank and suit. I want to take a file consisting of data like this:

2C 6D 7h 10s JC

This info determines a playing card.

6c is the 6 of clubs.

JC is the Jack of clubs, and so on. (Case does not matter).

How do I take the inputted characters and use them to make a new playing card? And having a case for every possible string seems like a lot of lines of code. Can that be simplified somehow?

#include "header1card.h"
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream> 
#include <fstream>

using namespace std;

int main() {
  Card card;
}

class Card {
private:
 enum struct ranks {two, three, four, five, six, seven, eight, 
             nine, ten, jack, queen, king, ace };
 enum struct suits {diamonds, clubs, hearts, spades };
 suits su; //do what with these two?
 ranks ra;
    };



int fileparser(vector<Card> & readcards, char * filename) {
 ifstream filereading;
 filereading.open(filename, ios::in);

 if (filereading.is_open()) {
   //while read the file here. save what is read to a string.
  char temp;

  while (!filereading.eof()) {
   //how make a card based off of the inputtedr chars?
   char readchar = filereading.get(temp );

   switch (readchar) {
   case '2' :

    break;
   case '3' :

    break;

//and so on to make every card? Seems like a lot of lines of code.
   }
   readcards.pop_back( /*After making the card, put it in vector*/   );

  } 
    filereading.close();
 }
 else {
  //throw error, cannot find file.
  cout << "Could not find file " + filename << endl; //error on ' + filename'. Says Expression must have integral or unscoped enum type.
  cerr << "COuld not find file " << endl;
     }
    }
alain.janinm
  • 19,035
  • 10
  • 56
  • 103
Eric
  • 11
  • 1
  • 1
  • 6

1 Answers1

2

First, I recommend rewriting your input code to look like this:

Card temp;
while (filereading >> temp) {
    readcards.push_back(temp); // note: push_back, not pop_back
}

See this question for why not to check for eof().

So with that, we just need to write an operator>> for our class:

class Card {
public:
    friend istream& operator>>(istream& is, Card& c) {
        std::string name;
        if (is >> name) {
            if (name.size() == 2) {
                // rank is name[0], suit is name[1]
                // go process
            }
            else {
                // set fail state
                is.setstate(std::ios::failbit);
            }
        }
        return is;
    }
};

So how do we get the rank and suit? This was the main part of your question. For suits, the simplest thing is just a switch:

switch (toupper(name[1])) {
case 'D':
    c.suit = Card::diamonds;
    break;
case 'S':
    // etc.
default:
    // set fail state, maybe log some error too
    is.setstate(std::ios::failbit);
}

For ranks, we can simplify the 2, 3, ..., 9 part with:

if (name[0] >= '2' && name[0] <= '9') {
    int idx = name[0] - '2'; // now it's an int, with value 0 through 7
    c.rank = Card::two + idx; // because enums are ordered
}
else {
    // fallback to switch
    switch (toupper(name[0])) {
    case 'T':
        c.rank = Card::ten;
        break;
    case 'J':
        // etc
    default:
        // set fail state
        is.setstate(std::ios::failbit);
}
Community
  • 1
  • 1
Barry
  • 247,587
  • 26
  • 487
  • 819
  • The real issue for the `operator>>` is error handling. Your code assumes space separators, which is reasonable, but does require verifying that the extracted string is exactly two characters. And of course, `operator>>` should never throw; if there is a format error in the input, it should set `std::ios_base::failbit` in the stream. – James Kanze Jan 25 '15 at 16:04