0

To convert an integer to base 10 char*

std::itoa(ConCounter, ID, 10);

ConCounter is an integer, ID is a char*, and 10 is the base

It says that iota is not a member of std and without std it's not declared. I know it's a nonstandard function but I included all the libraries for it and it still doesn't see it.

What is a way to do the above? Any quick one liners? I've tried the following;

std::to_string //it's not declared for me when using mingw, it doesn't exist.
snprintf/sprintf //should work but it gives me the "invalid conversion from 'int' to 'char *'"    error
std::stoi //has same problem as iota
nurtul
  • 274
  • 1
  • 9
  • 19
  • 1
    `to_string` is a) in C++11 and b) fixed in newer versions of MinGW. – chris Nov 17 '13 at 18:33
  • 2
    BTW, `std::iota` is something rather different than `itoa`. – Joe Z Nov 17 '13 at 18:35
  • @Chris which newer version ? [This](http://en.cppreference.com/w/cpp/string/basic_string/to_string) won't compile on MingW g++ (GCC) 4.8.1 – P0W Nov 17 '13 at 18:38
  • @P0W, I seem to recall it being fixed in the main releases, but it definitely works fine with MinGWBuilds 4.8.1. – chris Nov 17 '13 at 18:47
  • @chris http://i.troll.ws/376fe7ab.jpg – P0W Nov 17 '13 at 18:58
  • @P0W, Actually, it might be that MinGWBuilds just applies the patch for you. Here you go: http://stackoverflow.com/a/12975602/962089 – chris Nov 17 '13 at 19:00

3 Answers3

6

Try this:

#include <sstream>

int i = // your number
std::ostringstream digit;
digit<<i;
std::string numberString(digit.str());
Bruce Dean
  • 2,798
  • 2
  • 18
  • 30
  • I added this and I get a "invalid conversion from 'constant char*' to 'char*'" error – nurtul Nov 17 '13 at 18:43
  • It is because I removed the string part and just did ID = digit.str().c_str(); Which is why I was getting the error. – nurtul Nov 17 '13 at 18:53
1

I recommend using roybatty's answer, but I think sprintf should work too. I think when you used it you forgot the format string. It should be:

char buf[16];
std::snprintf(buf, sizeof(buf), "%d", integer);
uk4321
  • 1,028
  • 8
  • 18
0

There is also "strtol" function : http://www.cplusplus.com/reference/cstdlib/strtol/

Denis Gladkiy
  • 2,084
  • 1
  • 26
  • 40