Function itoa - what is wrong??

Pages: 12
Aug 8, 2016 at 10:37am
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 Aug 8, 2016 at 12:35pm
Aug 8, 2016 at 10:42am
Hi,
Why don't you use the standard ::itoa() function?
Aug 8, 2016 at 10:52am
Cause it is not handling largest negative numbers.What is wrong in my code above?
Aug 8, 2016 at 10:56am
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. :)
Aug 8, 2016 at 11:17am
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]...
Aug 8, 2016 at 11:40am
closed account (48T7M4Gy)
So where is the rest of the code?
Aug 8, 2016 at 11:51am
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)
Aug 8, 2016 at 12:02pm
closed account (48T7M4Gy)
Use theirs, it works. No reason to change don't waste your time.
Aug 8, 2016 at 12:12pm
No I don't want that :)
Can someone see the failure in my version of itoa above?
Aug 8, 2016 at 12:22pm
closed account (48T7M4Gy)
msize
Aug 8, 2016 at 12:25pm
Why this is not correct?How should it look like?Idea is: As soon as i < msize-1 is not true return -1;
Aug 8, 2016 at 12:32pm
closed account (48T7M4Gy)
Why this is not correct?
Probably because you line 1 is wrong - well sort of anyway. :)
Last edited on Aug 8, 2016 at 12:34pm
Aug 8, 2016 at 12:34pm
Oh sorry this was a typo :) I mean beside of that (I corrected it)
Aug 8, 2016 at 12:36pm
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.
Aug 8, 2016 at 12:37pm
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 Aug 8, 2016 at 12:39pm
Aug 8, 2016 at 12:51pm
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++;
}
Aug 8, 2016 at 12:58pm
You shall break; when n is 0.
Aug 8, 2016 at 1:03pm
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.
Aug 9, 2016 at 3:45am
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 Aug 9, 2016 at 4:54am
Aug 9, 2016 at 4:42am
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