I don't know what kind of homework this is, but most probably the teacher doesn't want an answer where you just call a "magical" existing function (even though that's the recommended way to do it), but he wants to see if you can implement this by your own.
Back in the days, my teacher used to say something like "I want to see if you can program by yourself, not if you can find it in the system." Well, how wrong he was ;) ..
Anyway, if your teacher is the same, here is the hard way to do it..
std::string LongToString(long value){ std::string output; std::string sign; if(value < 0) { sign +"-"; value = -value; } while(output.empty() || (value > 0)) { output.push_front(value % 10 +'0') value /= 10; } return sign + output;}
You could argue that using std::string
is not "the hard way", but I guess what counts in the actual agorithm.