2013-01-31 28 views
0

當我在數組中插入字符值時,我的程序退出。如果我插入字符值,我想繼續沒有退出程序的循環,並取下一個整數值。 我的代碼有什麼問題。如何驗證c中的數值?

我的代碼:

#include<stdio.h> 
#include<ctype.h> 

int main() 
{ 
    int row1, col1, row2, col2, i, j, k, sum = 0; 
    int first[10][10], second[10][10], multiply[10][10]; 
    clrscr(); 
    printf("Enter the no of rows and columns of first matrix\n"); 
    scanf("%d%d",&row1,&col1); 
    printf("Enter the Element of first matrix"); 
    for(i=0;i<row1;i++){ 
     for(j=0;j<col1;j++){ 
      printf("\nEnter %d element %d row",j+1,i+1); 
      scanf("%d",&first[i][j]); 
      check = first[i][j]; 
      printf("%d",check); 
      if(check>0 || check<0) 
      { 
       printf("this is digit \n"); 
      } 
      else 
      { 
       printf("this is not digit\n"); 
       printf("\nEnter %d element %d row",j+1,i+1); 
       scanf("%d",&first[i][j]); 
      } 
     } 
    } 
    printf("Enter the no of rows and columns of second matrix\n"); 
    scanf("%d%d",&row2,&col2); 

    if(col1 == row2){ 
     printf("Enter the Element of second matrix"); 
     for(i=0;i<row2;i++){ 
      for(j=0;j<col2;j++){ 
       printf("\nEnter %d element %d row",j+1,i+1); 
       scanf("%d",&second[i][j]); 
      } 
     } 

     for(i=0;i<row1;i++){ 
      for(j=0;j<col2;j++){ 
       for(k=0;k<row2;k++){ 
        multiply[i][j]=sum; 
        sum=0; 
       } 
      } 
     } 
     printf("Multiplication of matrices=\n"); 
     for(i=0;i<row1;i++){ 
      for(j=0;j<col2;j++){ 
       multiply[i][j]=0; 
       for(k=0;k<row1;k++){ 
        multiply[i][j] += first[i][k] * second[k][j]; 
       } 
       printf("%d\t",multiply[i][j]); 
      } 
      printf("\n"); 
     } 
    } 
    else{ 
     printf("You are incorrect. According to the requirement of matrix multiplication, the number of column of first matrix should equal to the number of rows of second matrix."); 
    } 
    getch(); 
    return 0; 
} 

感謝你

回答

2

當你執行

scanf("%d",&first[i][j]) 

這意味着你的scanf只接受整數而已。如果你輸入一個字母,然後scanf函數會讀什麼,然後返回0作爲值first[i][j]

scanf("%d");將無法​​得到你的輸入(即使它們是數字),因爲標準輸入不是從字母尚未清理字符。

替換此

for(i=0;i<row1;i++){ 
     for(j=0;j<col1;j++){ 
      printf("\nEnter %d element %d row",j+1,i+1); 
      scanf("%d",&first[i][j]); 

通過

for(i=0;i<row1;i++){ 
     for(j=0;j<col1;j++){ 
      printf("\nEnter %d element %d row",j+1,i+1); 
      while (scanf("%d",&first[i][j]))<=0) { 
       printf("this is not digit\n"); 
       printf("\nEnter %d element %d row",j+1,i+1); 
       scanf("%*[^\n]"); // this clean your input buffer in case yhe input is not integer 
      } 

並刪除該塊:

  check = first[i][j]; 
      printf("%d",check); 
      if(check>0 || check<0) 
      { 
       printf("this is digit \n"); 
      } 
      else 
      { 
       printf("this is not digit\n"); 
       printf("\nEnter %d element %d row",j+1,i+1); 
       scanf("%d",&first[i][j]); 
      } 
+0

感謝名單穆罕默德。這個建議對我有幫助,並且通過描述來提高知識。 –

2

scanf回報您已成功讀取的條目數。當您嘗試讀取一個數字,但提供了非數字值時,scanf返回零。您需要重複提示用戶輸入,直到scanf返回1。下面是修改代碼以做這件事:

for(;;) { 
    printf("\nEnter %d element %d row",j+1,i+1); 
    if (scanf("%d",&first[i][j]) == 1) break; 
    // Read and ignore the invalid input. 's' means "string", '*' means "ignore". 
    scanf("%*s"); 
    printf("\nThe data you entered was incorrect."); 
}