samedi 30 janvier 2016

Char compare in if statements not working properly

I have a couple of if statements comparing a private char of a class with a character (like 'c' or 'd'), but it isn't assigning the right value when I run it, and I don't know what the problem is. Here's the code:

ostream & operator<<(ostream &out, const Card &rhs)
{

    string string_suit;
    string string_rank;

    /*switch(rhs.suit)
    {
        case 'c': string_suit="Clubs"; 

        case 'd': string_suit="Diamonds"; 
                  break;
        case 'h': string_suit="Hearts"; 
                  break;
        case 's': string_suit="Spades"; 
                  break;
        default : string_suit= "error"; 
    }*/

    if(rhs.suit == 'c')
    {
    string_suit="Clubs";
    }

    else if(rhs.suit == 'd')
    {
    string_suit ="Diamonds";
    }

    else
    {
    string_suit = "error";
    }


    if(rhs.rank==1)
    string_rank="Ace";

    else if(rhs.rank==11)
    string_rank="Jack";

    else if(rhs.rank==12)
    string_rank="Queen";

    else if(rhs.rank==13)
    string_rank="King";


    else
   {

     ostringstream convert;
     convert << rhs.rank;
     string_rank=convert.str(); //rhs.rank;
   }   

    out<<string_rank<<" of "<<string_suit ;
    return out;


}

Aucun commentaire:

Enregistrer un commentaire