0

I am using use Microsoft Visual Studio to learn C, I wrote some code, it's work well. But when I tried to debug with xcode, it doesn't work.

this is my error in xcode

Here is my code for convert a number to roman numeral:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int dec,length,indice,irom,itab=0;
    char rom[4][5]={'\0'};
    char dconvert[10];
    char convert[2];
    char tab[7]={'I','V','X','L','C','D','M'};
    float dec2;

    puts("give a number");
    scanf("%d",&dec);

    itoa(dec,dconvert,10);
    length=strlen(dconvert);
    strrev(dconvert);

    for (indice=0; indice < length;indice ++)
    {   
        convert[0]=dconvert[indice];
        dec=atoi(convert);
        if (dec<=3)
        {
            for (irom=0; irom<dec;irom++)
                {
                    rom[indice][irom]=tab[itab];
                }
        }
        if (dec>3 && dec<9)
        {   
            irom=0;
            dec2=dec-5;
            if (dec2<0)
            {
                rom[indice][irom]=tab[itab];
                rom[indice][irom+1]=tab[itab+1];
            }
            else 
            {
                rom[indice][irom]=tab[itab+1];
                for (irom=1;irom<=dec2;irom++)
                {
                    rom[indice][irom]=tab[itab];
                }
            }
        }
        if (dec==9)
        {   
            irom=0;
            rom[indice][irom]=tab[itab];
            rom[indice][irom+1]=tab[itab+2];
        }


        itab=itab+2;
    }
    for (indice=length; indice>=0;indice--)
        {
            printf("%s",rom[indice]);
        }

}
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
user1907096
  • 19
  • 1
  • 2
  • 1
    `itoa()` isn't part of the C standard and it's telling you that. Googling will shed more details, as well as this SO question: http://stackoverflow.com/questions/2225868/how-to-convert-an-integer-to-a-string-portably?rq=1 – Brian Roach Dec 16 '12 at 00:35

3 Answers3

1

As mentioned, itoa is not part of the C99 standard. Instead, use sprintf (or snprintf to avoid buffer overflow):

sprintf(target_string, "%d", int_value);
Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
1

You could use:

snprintf(str, sizeof(str), "%d", num);

to avoid buffer overflow (when number you're converting doesn't fit the size of your string).

Zi0P4tch0
  • 63
  • 1
  • 5
1

Neither itoa nor strrev are standard.

Other StackOverflow questions:

Where is the itoa function in Linux?

Is the strrev() function not available in Linux?

Fixing those two problems may fix the middle one, but if not, read http://forums.bignerdranch.com/viewtopic.php?f=77&t=1743

Community
  • 1
  • 1