-4

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++.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sspp
  • 65
  • 1
  • 6
  • 1
    use `std::stringstream` – Ari0nhh Aug 02 '16 at 12:41
  • Check [this answer](http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c/5590404#5590404) on a related (not duplicate) question. A macro version of Sam's answer. – DevSolar Aug 02 '16 at 12:50

2 Answers2

6

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();
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
-6

plain old sprintf

char buf[10000];

sprintf(buf , "%s is %c string!!%d!!%d" , "this , 'a' , 1 ,1);
Carmine Ingaldi
  • 856
  • 9
  • 23
  • 4
    This solution is strongly discouraged. It's a C solution, not a C++ solution; and it is prone to buffer overflow (at the very least you should use `snprintf`). – mindriot Aug 02 '16 at 12:45