1

I have the following c++ function and I would like to know how to convert an int to a string for the else clause of the if statement.

string afficherValeurNominal(int val)
{
    string valAffiche = "";
    if (val == 11) // carte j
    {
        valAffiche = "V";
    }
    else if (val == 12) // carte Q
    {
        valAffiche = "D";
    }
    else if (val == 13) // carte k
    {
        valAffiche = "R";
    }
    else
    {
        valAffiche = val;
    }
    return valAffiche;
}
1
  • 1
    Why don't you switch (val) instead of repeated else if? Commented Mar 11, 2016 at 22:56

3 Answers 3

6

Use to_string.

valAffiche = std::to_string(val);
Sign up to request clarification or add additional context in comments.

Comments

4
int a = 22;
stringstream ss;
ss << a;
string str = ss.str();

Comments

0

Use boost library.

int i = 42;
std::string str = boost::lexical_cast<std::string>(i);

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.