2013-01-25 124 views
2

我在我的WxDev C++上編譯了這段代碼。理論上它應該輸出用戶的答案和正確的答案。它的確,除了coranswers數組的數組0之外,只顯示一個空格。我試圖在其他PC上編譯它,並且問題不存在。我試過重新安裝WxDev,甚至用代碼塊代替它。似乎問題出在我的電腦上。我該怎麼辦?不同的電腦不同的輸出

#include<stdio.h> 
int main(){ 
char coranswers[20] = {'A', 'B', 'C', 'D', 'E', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A'}; 
char answers[20]; 
int count; 
int grade = 0; 

for(count = 0; count < 20; count++){ 
      printf("No.%d ", count+1); 
      scanf("%s", &answers[count]); 
      } 
for(count = 0; count < 20; count++){ 
      printf("No.%d ", count+1); 
      printf("Your answers : %c\n", answers[count]); 
      if(answers[count] == coranswers[count]){ 
          grade++; 
          } 
      else{ 
       printf("Wrong answer, correct answer is %c\n", coranswers[count]); 
      } 
      } 
      return 0; 
      } 

回答

5

你不想讀字符串轉換成字符數組

scanf("%s", &answers[count]); // the format string should be "%c" 
+0

大概'「%C」'有空格,如果它應該忽略換行符。 –

+0

謝謝,我確實使用了%c並最終搞亂了這個程序,但是在看到Anton的回答之後,我嘗試了使用空白並且工作。 – user2011047

0

你沒有得到一個角色。

改變你的scanf格式說明從%S%C

scanf("%c", &answers[count]) 

對於不同的系統之間輸出的差異,這可能是一個原因。

scanf("%s", &answers[count]) 

由於answers字符數組,所以你不需要&當你得到一個字符串。您可能正在破壞&answers

0

通常我使用%s只爲連字符,但有點不同 - >

char temp[8]; 

scanf("%s", temp); 
answers[count] = temp[0];