Click here to Skip to main content
15,393,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to write a function that get an empty string and an integer from the user and return it as a string, the size of the string is defined.
all I know that I cannot use the 'itoa' function and I need to use a pointer.
for example if the integer is 12 so the function return "'1' '2' ".
thats what i wrote till now but unfortuntlly its not working.
any ideas?

What I have tried:

void ToString(char* str, int number)
	int i;
	int temp = number;
	int size;
	char n;
	while (temp != 0) {
		temp / 10;
	for (i = size - 1; i >= 0; i--) {
		str == number / 10 + '0';

Updated 31-Dec-21 8:42am

So little code, so many errors ...
1) "==" is a comparison, not an assignment.
2) str is a pointer, so you might not want to overwrite it with a value anyway...
3) If you divide a number by ten, does that really extract a single digit?
4) if you don't explicitly modify a variable (hint: number) does it ever change?
5) What value does a variable get if you don't assign a value to it at all?
6) Where do you use n?
7) What use is temp at all?

To be honest, that looks like you threw code together at random in the hope that we might rewrite it into something that works you could hand in: I see nothing there that indicate that you gave any thought to what you had to do before you jumped on the keyboard, and that's never a good way to develop anything.
There are a few things wrong with your logic:

- The variable size is used by never initialized. I would guess it should be initialized to zero and then incremented in the while loop with the ++ operator.

- Also in the while loop, temp should probably be set with the /= operator.

- Another thing is the str pointer appears to be set using the comparison operator instead of the assignment operator which is a single equals sign.

Here is what your logic looks like with these changes :
void ToString( char* str, int number )
    int i;
    int size = 0;
    int temp = number;
    while( temp != 0 )
        temp /= 10;
    for( int i = size - 1; i >= 0; --i )
        *str = ( number / 10 ) + '0';
It's still not right but at least the syntax errors have been fixed. I recommend that you run your program in a debugger so you can see what is going on with it. At the very least, add some output statements so you can see the characters as they are added to the string.
KarstenK 31-Dec-21 4:26am
The string calculation doesnt look sound.
Rick York 31-Dec-21 13:47pm
No, it does not. That's why I was very hesitant to add code. In the end I decided to do it because they can't debug the code until it compiles.
You have some errors in your code,

size is not initialized
you have performed an operation on temp but haven't stored it.
Inside the for loop you have used comparsion operator instead of equals'='.

The following code might help in performing the operation
char *itoa(int num) 
    int i; 
    while(num != 0) 
        str[i++] = num % 10 + '0'; 
        num /= 10; 
    str[i] = '\0'; 
	return str; 
Rick York 31-Dec-21 13:48pm
That's closer but it will build the string backwards. Also - i is not initialized.
Greetings May I inquire how is it guaranteed the destination ptr points to sufficient memory to contain all the digits . It does not seem possible to obtain same as the caller would then need to know the conversion already, unless of course it is allocated to contain the maximum possible length of integer converted string to wit i.e. e.g. std::numeric_limits<int>::max() . May I also inquire is this a school assignment? Also the level/year of schooling to wit i.e. Grammar[1-8]? Secondary[1-4]?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900