jeudi 2 juin 2016

How can I scanf a string and push_back to a vector until input is finished using c++

The end goal is to have a vector whit the numbers that are equivalent to the inputs that are mapped. A sample input while be something like this: KS QS TH 8H 4H AC QC TC 5C KD QD JD 8D

edit: After entering the sample imput and an EOF using ^Z the only thing I get is a vector of only 0's

char s[3];
map <string, int> cards;
vector <char> hand;

// I have map of cards in this format
cards["D2"] = 0;
cards["D3"] = 1;
...
cards["SK"] = 50;
cards["SA"] = 51;

while (scanf("%s", &s) == 1){

    hand.push_back(cards[s]);
}

for (int i = 0; i < hand.size(); i++){
    cout << hand[i] << " ";
}

Aucun commentaire:

Enregistrer un commentaire