Function itoa - what is wrong??

Pages: 12
I wrote following function to convert integer to string and handle largest negative number as well.But it is not working.Could someone tell me what I am doing wrong?? (n is number,char s[] is str and maxsize is sizeof(str))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int itoa(int n,char s[],int msize)
{
  int i,sign,digit;
  char p[]="0123456789";

  sign=n;
  i=0;

  while (i < msize-1)
  {
    digit=n%10;
    n/=10;
    s[i]=p[digit];
    i++;
  }

  if (sign < 0 && i < msize-1)
    s[i]='-';
    i++;

  else if (i >= msize-1)
    return -1;

  s[i]='\0';
  reverse(s);
  return 0;
}
Last edited on
Hi,
Why don't you use the standard ::itoa() function?
Cause it is not handling largest negative numbers.What is wrong in my code above?
closed account (48T7M4Gy)
What is wrong in my code above?

Well why don't you help yourself and add back the lines that make it runnable code in the shell, and show some sample input and output.

That way we don't have to break out the crystal balls and call in the mind-reader crew. :)
My problem is while.I want to do following within while:

e.g. n=214 => s[0] = p[2], s[1] = p[1], s[2] = p[4]...
closed account (48T7M4Gy)
So where is the rest of the code?
The code is like thathttp://www.learntosolveit.com/cprogramming/Ex_3.4_itoa-2.html
I just changed itoa from void to int and with 3 parameters instead of 2.maxsize is sizeof(str)
closed account (48T7M4Gy)
Use theirs, it works. No reason to change don't waste your time.
No I don't want that :)
Can someone see the failure in my version of itoa above?
closed account (48T7M4Gy)
msize
Why this is not correct?How should it look like?Idea is: As soon as i < msize-1 is not true return -1;
closed account (48T7M4Gy)
Why this is not correct?
Probably because you line 1 is wrong - well sort of anyway. :)
Last edited on
Oh sorry this was a typo :) I mean beside of that (I corrected it)
closed account (48T7M4Gy)
Well, like i said before you should show you code so it can be run. If you don't want to then that's OK, up to you.
closed account (z05DSL3A)
a few things to get you started...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int itoa(int n,char s[],int maxsize)  // itoa asking for trouble unless you 
{                      //have it in a namespace
  int i,sign,digit;
  char p[]="0123456789";

  sign=n;
  i=0;

  while (i < msize-1)  // msize? maxsize probably
  {
    digit=n%10;
    n/=10;
    s[i]=p[digit];
    i++;
  }

  if (sign < 0 && i < msize-1) // msize? maxsize probably
    s[i]='-';
    i++;
                                  // the else is orphaned need {} round the if path
  else if (i >= msize-1) // msize? maxsize probably
    return -1;

  s[i]='\0';
  reverse(s);
  return 0;
}
Last edited on
Ok. But my question is:

Is this snippet correct:

1
2
3
4
5
6
7
while (i < msize-1)
{
  digit = n %10;
  n /= 10;
  s[i] = p[digit];
  i++;
}
You shall break; when n is 0.
closed account (z05DSL3A)
You would probably want to control the while loop with n rather than the size of the array...but make sure you don't write out of bounds if the array is too small.
Cause it is not handling largest negative numbers


Yet you pass the function an int ......

Are you working in tandem with closed account to troll?
Last edited on
closed account (E0p9LyTq)
Why don't you use the standard ::itoa() function?


http://www.cplusplus.com/reference/cstdlib/itoa/
This function (itoa) is not defined in ANSI-C and is not part of C++, but is supported by some compilers.
Pages: 12