2013-09-25 98 views
1

請回復雙倍情況3中的問題?測試案例3其給予運行時間錯誤.. 其他案件正在工作正常 我無法弄清楚scanf中存在什麼問題,它將用戶輸入作爲雙重輸入。如何使用字符取雙倍數值..使用字符數組

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

int main() 
{ 
char x[30]={0}, ch_type[30]={1}; 
int ch=1,idx=0,cond_var2=0,temp=0, choice=0; 
int curr_arr_size=0, arr_size=sizeof(x); 

while(ch) 
{  
    printf("\n\n1.Create a character \n2.create a number \n3.create a double \n4.current status \n0.exit\n"); 
    printf("\nenter the choice:\t");  
    scanf("%d",&choice); 
    switch(choice) 
    { 
    case 0: 
      exit(0); 
      break; 

    case 1: 
     if ((arr_size-curr_arr_size)>=sizeof(char)) 
     { 
      ch_type[idx++]=sizeof(char); 
      printf("\n enter the character:\t"); 
      fflush(stdin); 
      scanf("%c",(x+curr_arr_size)); 
      curr_arr_size=curr_arr_size+sizeof(char); 
     } 
     else 
      printf("no space for any characters.\n"); 
     break; 

    case 2: 
     if ((arr_size-curr_arr_size)>=sizeof(int)) 
     { 
      ch_type[idx++]=sizeof(int); 
      printf("\n enter the integer:\t"); 
      scanf("%d",(x+curr_arr_size)); 
      curr_arr_size=curr_arr_size+sizeof(int); 
     } 
     else 
      printf("no space for any integer.\n"); 
     break; 

    case 3: 
     if ((arr_size-curr_arr_size)>=sizeof(double)) 
     { 
      ch_type[idx++]=sizeof(double); 
      printf("\n enter the double:\t"); 
      scanf("%lf",(x+curr_arr_size));    
      curr_arr_size=curr_arr_size+sizeof(double); 
     } 
     else 
      printf("no space for any doubles.\n"); 
     break; 

    case 4 :    
     while(cond_var2<idx) 
     { 
      if(ch_type[cond_var2]==sizeof(char)) 
       printf("\n%d value is %c",cond_var2+1,*(x+temp)); 
      if(ch_type[cond_var2]==sizeof(int)) 
       printf("\n%d value is %d",cond_var2+1,*(x+temp)); 
      if(ch_type[cond_var2]==sizeof(double)) 
       printf("\n%lf value is %c",cond_var2+1,*(x+temp)); 

      temp=temp+ch_type[cond_var2]; 
      cond_var2++; 
     } 
     printf("\nnumber of the bytes free:\t%d",arr_size-curr_arr_size); 
     break; 
    default : 
      ch=0; 
      printf("\nWrong choice\n"); 
      break; 
    } 
    printf("arsize:%d\ncur_size:%d\n\n",arr_size,curr_arr_size); 
} 
return 0; 
} 
+0

你真的要仔細檢查你的類型演員... – PurpleAlien

+0

是否有錯誤m消息或者是打印錯誤輸出的程序? –

回答

2

編譯器(至少GCC)會告訴你所有你必須知道:

x.c:66:17: warning: format ‘%lf’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat] 

printf("\n%lf value is %c",cond_var2+1,*(x+temp)); 

您切換參數,你需要轉換:

printf("\n%d value is %lf",cond_var2+1,*(double*)(x+temp)); 

注意:是更多的警告,修復這些以及...

相關問題