2015-03-02 47 views
-2
**#include <stdio.h> 
#define SIZE 3 
void scanA(int array[SIZE][SIZE]); // prototyping 
int main() 
{ 
    int myarray[SIZE][SIZE]; 
    int i,j;  

    printf("Please enter the array: \n"); 
    scanArray(myarray); 
    for(i=0; i<SIZE; i++) 
     for(j=0; j<SIZE; j++) 
     { 
      printf("%c",myarray[i][j]);   
     } 
    printf("\n"); 
    return 0; 
} 
void scanA(int array[SIZE][SIZE]) // function defintion 
{ 
    int i; 
    int j; 
    for(i=0; i<SIZE; i++)  // looping to scan 
     for(j=0; j<SIZE; j++) 
     { 
      scanf("%c\n ",&array[i][j]); 
     } 
}** 

// scanA函數中的scanf要求10個字符,雖然它是循環的9倍 我想知道原因和解決方案。爲什麼scanf函數要求額外的輸入?

+1

請不要在此處輸入您的代碼,複製並粘貼從你的編輯器中刪除 – 2015-03-02 21:10:55

+0

刪除'「%c」'後面的空格。http://stackoverflow.com/a/28820181/2410359在正確的軌道上:'「%c」'。 – chux 2015-03-03 01:05:21

+0

「%c」 - 這是行得通的,但是,是什麼原因? – pahan 2015-03-03 03:36:49

回答

1

嘗試在%c之前放一個空格(只有當您讀取一個字符時),原因有時會將scanf讀取爲char字符。 (我不能完全肯定,但我這個解決方案的工作)

對不起,我的英語,這不是我的主要語言:(

+0

它工作。謝謝你的回答。但是,是否有一個特別的原因?我想知道。 – pahan 2015-03-03 03:36:12

+0

你必須添加空格,因爲當你輸入字符並按輸入scanf讀兩個字符:你插入的字符和輸入 – keras 2015-03-03 07:23:59

+0

@PahanMadusha這裏是另一種解釋。http://stackoverflow.net M /問題/ 17079144 /爲什麼-WE-需要到放空間,前-C – keras 2015-03-03 14:14:03

相關問題