MrTNader
MrTNader

Reputation: 37

String inserting C++

I have been working on a program that will output a given playing card. I am using the Ace of Spades just to start out. This isn't the entire program use by no means, but this is just to see if I am on the right track.

For purposes beyond what I am about to show, I need to create a string that has "Ace of HeartsSpades" stored in it from one string that contains "Ace" and one that contains "Spades"

string toString(string myRank, string mySuit)
{
    string halfCard, fullCard;
    halfCard = myRank;                       //Ace 
    fullCard = halfCard.append(mySuit);      //AceSpades
    fullCard.insert(3, " of ");               //Ace of Spades
    return fullCard;
}

There is the method that I have so far. I know that not every card is going to work with a position of 3 in my fullCard.insert line, so is there another way to make this work so that this method becomes universal for all cards in a deck (jokers are not used in the deck).

I am sorry if my explanation of things were not clear.

Upvotes: 0

Views: 73

Answers (2)

Brad Vidal
Brad Vidal

Reputation: 21

I believe you could do something like:

string toString(string myRank, string mySuit)
{
    return myRank + " of " + mySuit;
}

Upvotes: 2

Aesthete
Aesthete

Reputation: 18848

It's easier than that (assuming you're using std::string)

std::string toString(const std::string& myRank, const std::string& mySuit)
{
    return myRank + " of " + mySuit;
}

Note that I've changed your argument to be references, which will avoid unecessary string object copies.

Upvotes: 1

Related Questions