How can I concatenate string + int + string in C++? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How can I concatenate string + int + string in C++? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

How can I concatenate i + name + letter + i?

for(int i = 0; i < 10; ++i){

    //I need a const char* to pass as a parameter to another function
    const char* name  = "mki";

    //The letter is equal to "A" for the first 2, "B" for the second 3,
    //"C" for the following 4 ...
    const char* final_string = ???
}

I already tried using:

std::to_string(i)

But I got an error saying that

to_string is undefined for std

I’m using Visual C++.

Answer

You have an older version of VC++ that does not support the current C++ standard. In that case, you have to do it the old-fashioned way.

#include <sstream>

std::ostringstream o;

o << "mki" << i << "abc";

std::string s=o.str();
We are here to answer your question about How can I concatenate string + int + string in C++? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji