itoa???

This is a discussion on itoa??? within the C++ Programming forums, part of the General Programming Boards category; How would I convert an int to a character string? ex...
int number = 534;
char str[4] = {some funtion ...

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

So, you rewrite the standard library when programming? It is a good excercise, but in projects, I prefer to reuse already tested code.

>Are there any reasons why one shouldn't use my function as
>opposed to the other methods suggested?

Portability: if I would use your function and then use the code on a different platform, I would also have to recompile your functions on that platform and hope that they work as they did on the first platform.

So, you rewrite the standard library when programming? It is a good excercise, but in projects, I prefer to reuse already tested code.

Yeah, not only is it a good exercise, it's fun too And, I'll have you know that I tested my function

>Are there any reasons why one shouldn't use my function as
>opposed to the other methods suggested?

Portability: if I would use your function and then use the code on a different platform, I would also have to recompile your functions on that platform and hope that they work as they did on the first platform.

I don't see why my function wouldn't work on another platform. I didn't need to include anything except <iostream> to run it, and I didn't use any compiler specific code.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

>I prefer to write my own functions.
This is always good when practicing, but for production code it's a waste of your time to recreate that which already exists.

>Are there any reasons why one shouldn't use my function as opposed to the other methods suggested?
They're standard, portable, guaranteed to work everywhere the same way, there's no reason to wonder if the logic is wrong somewhere, there's less code to check if a bug does occur, it's easier to maintain, find documentation on, read, understand... I could go on.

>I don't see why my function wouldn't work on another platform.
Actually, it wouldn't. Your function has a serious bug and lacks crucial functionality.

Originally posted by Prelude >I prefer to write my own functions.
This is always good when practicing, but for production code it's a waste of your time to recreate that which already exists.

Yeah, I know. If I was a professional programmer, I wouldn't waste my time rewriting standard functions.

>I don't see why my function wouldn't work on another platform.
Actually, it wouldn't. Your function has a serious bug and lacks crucial functionality.

Ouch! I know it doesn't work for negative integers. Also, I wasn't sure if I should delete strNum at the end of the function, and if so, where to delete it. Mind telling me how to improve that function? I'm just really curious.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Shoot, egg on my face. I forgot about that whole 0 to n-1 thing. Yeah, I get the point. The thing is, I don't trust standard libraries very much because I don't understand them. I've tried looking at the code for them, but I don't understand much of it. *sigh* I guess I'll give up now and use the boring standard functions

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

question

Not being a master coder myself, I was wondering while on this piece of code if someone could tell me how to return the contents of strNum from the function and still delete the memory? It's probably painfully obvious but I can't figure it out =) Thanks!

>Mind telling me how to improve that function?
Your first goal should be removing the memory allocation. C++ is designed so that you can avoid such low level operations more often than you would expect. Either have the user pass in a buffer or avoid potentially dangerous char arrays and use std::string or another suitable string class that handles memory for you. Next you need to handle negative numbers, the usual solution looks like this:

Which could still be improved, but will work nicely for most general usage.

>I don't trust standard libraries very much because I don't understand them.
That's not a reason to avoid them, instead you should work to understand them. Consider this, if you don't understand the libraries that you are recreating then why do you think you can write them better?