2012-12-16 87 views
1

我正在使用Microsoft Visual Studio學習C,我寫了一些代碼,它工作的很好。但是當我嘗試用xcode進行調試時,它不起作用。我在xcode中遇到了一些問題

this is my error in xcode

這裏是我的代碼轉換的數以羅馬數字:

#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]); 
     } 

} 
+1

'itoa()'是不是一部分的C標準,它告訴你這一點。谷歌搜索將刪除更多的細節,以及這個SO問題:http://stackoverflow.com/questions/2225868/how-to-convert-an-integer-to-a-string-portably?rq=1 –

回答

1

如前所述,itoa不是C99標準的一部分。相反,使用sprintf(或snprintf避免緩衝區溢出):

sprintf(target_string, "%d", int_value); 
+0

謝謝你你的答案。 – user1907096

1

你可以使用:

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

避免緩衝區溢出(當你將數量不適合的大小您串)。

相關問題