1

This may be a vaugue question but im getting this error

Simulator.cpp: In member function `void Simulator::generatePassengers()': 
Simulator.cpp:60: error: `itoa' undeclared (first use this function) 

This is the code im having an issue with

itoa(i,buf,10);

What can i use to fix this issue because on one compiler i get this issue on another i dont. So im stumped here has to work on both.

    char buf[3];
    if(i<10)
    {
        key1.append("0");
        key2.append("0");
        key3.append("0");
    }
    itoa(i,buf,10);
    key1.append(buf);
    key2.append(buf);
    key3.append(buf);
soniccool
  • 5,790
  • 22
  • 60
  • 98
  • 6
    From what I remember, `itoa` is nonstandard. – chris Dec 29 '12 at 04:42
  • As above, the following may be useful http://stackoverflow.com/questions/6920554/source-code-in-embedded-c-for-unsigned-integer-to-string depending on your exact requirements – PeterJ Dec 29 '12 at 04:45
  • 2
    What's wrong with `to_string`, `stringstream`,or `boost::lexical_cast`? – Pubby Dec 29 '12 at 04:45
  • @pubby: inefficiency and non-predictability. – Cheers and hth. - Alf Dec 29 '12 at 04:52
  • @Cheersandhth.-Alf `boost::lexical_cast` is 100% predictable and faster than most other formatting options for just about any conversion. http://www.boost.org/doc/libs/1_52_0/doc/html/boost_lexical_cast/performance.html – David Stone Dec 29 '12 at 05:12
  • 1
    @DavidStone: that's a bit of bullshit. e.g. for simple conversion string to `int` i get a factor of 8 to 9 faster than `lexical_cast`, with just an off-the-cuff implementation (no special optimization of that code). **[try the source code yourself](https://gist.github.com/4404935)**. – Cheers and hth. - Alf Dec 29 '12 at 06:23
  • I guess I was wrong on that. I fiddled with the code a little bit, and `boost::lexical_cast` was still safer for large values, but when I corrected your code to check for that (and accepted the input number as a command-line argument rather than a compile-time constant), my times were Do it yourself: 2.21 seconds -- boost: 5.98 seconds. That was for the greatest `int` value. Now, my changes have the slight disadvantage of rejecting the least `int` value (which I guess is still better than undefined behavior), but I'm sure I could add that in without tripling execution time. – David Stone Dec 29 '12 at 19:18
  • I tried compiling with BOOST_LEXICAL_CAST_ASSUME_C_LOCALE, and it did speed it up to 4.31 seconds instead of 5.98, but that's still twice as slow as the manual version. Just as a note, my system that I tested is gcc 4.7.2 with boost 1.48.0. I will have to check back with the latest Boost to see if they have fixed any of these problems. – David Stone Dec 29 '12 at 19:21

2 Answers2

3

Your platform doesn't have itoa. It's not specified by any standard. You can use any replacement you like, including printf or just writing your own.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
3

Does this work?

#include <string>
std::string buf = std::to_string(i);
key1.append(buf);
key2.append(buf);
key3.append(buf);
soniccool
  • 5,790
  • 22
  • 60
  • 98