【发布时间】:2015-01-25 15:32:41
【问题描述】:
我有 2 个扑克牌值的枚举,它们的等级和花色。我想获取一个包含如下数据的文件:
2C 6D 7h 10s JC
这个信息决定了一张扑克牌。
6c 是俱乐部的 6。
JC 是俱乐部的杰克,等等。 (大小写无关紧要)。
如何获取输入的字符并使用它们制作新的扑克牌?每个可能的字符串都有一个案例似乎有很多代码行。可以以某种方式简化吗?
#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;
}
}
【问题讨论】:
标签: c++ file stream playing-cards