2013-04-02 22 views
1

嗨我對C很新,我想在C中做一個調查程序,允許用戶在不同類型的問題之間作出選擇並將調查存儲在一個文件中。我的代碼不會在while循環中輸入並在它之前終止。有人可以指出錯誤嗎?C調查程序

makesurvey() 
{ 
int tfquestions, mcq, shortq,essayq,rankq ; 
int i=1; 
char *buffer; 
printf("\nEnter the number of True/False Questions, Multiple Choice, Short answer , EssayAnswer and rank the choice questions "); 
scanf("%d",&tfquestions); 
scanf("%d",&mcq); 
scanf("%d",&shortq); 
scanf("%d",&essayq); 
scanf("%d",&rankq); 
FILE *fp; 
fp = fopen("survey.txt","w"); 
    while(i=!tfquestions) 
    { 
      if(fp != NULL) 
      { 
      fprintf(fp,"Enter The True false question"); 
      buffer = (char*)malloc(sizeof(40)); 
      fscanf(fp,"%c",buffer); 
      fclose(fp); 
      } 

      else 
      { 
        printf("Could not open the file"); 
      } 
    i++; 
    } 
+1

'(I =!tfquestions)'表示 「分配'未tfquestions'(0或1)以'i'」 – pmg

+3

'而(I =!tfquestions)'應該使用'!='。因爲你把'i'設置爲'!tfquestions',結果爲0. –

+0

好吧謝謝(Y) – Pradit

回答

4

你需要usem!= not =!

fp = fopen("survey.txt","w"); 
    while(i!=tfquestions) 
    { 
     if(fp != NULL) 
     { 
     fprintf(fp,"Enter The True false question"); 
     buffer = (char*)malloc(sizeof(40)); 
     fscanf(fp,"%c",buffer); 
     fclose(fp); 
     } 

     else 
     { 
       printf("Could not open the file"); 
     } 
i++; 
} 
+0

yea得到它的感謝! – Pradit