2013-03-06 29 views
-1

我想在C中填充2d數組一切正常,但分配給數組的值不打印。輸入驗證分配和顯示,如果二維字符數組

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

main() 
{ 
    char parkingspace[25][4]; 
    char CarReg[7], validreg[7]; 
    int row, position; 

    printf("Enter the car Registration number \n"); 
    fgets(CarReg, sizeof(CarReg), stdin); 
    if(isdigit(CarReg[0])&& isdigit(CarReg[1]) && (CarReg[2]=='H') && (CarReg[3]=='I') && (CarReg[4]=='R') && (CarReg[5]=='E')) 
    { 
     puts("Valid Registration \n"); 
     printf("==================================================\n\n\n"); 
    } 
    else 
    { 
     puts("Invalid registration .\n Please put a value of two digits followed by the word HIRE! in caps"); 
    } 
    printf("You entered: %s\n", CarReg); 
    if(isdigit(CarReg[0])&& isdigit(CarReg[1]) && (CarReg[2]=='H') && (CarReg[3]=='I') && (CarReg[4]=='R') && (CarReg[5]=='E')) 
    { 
     strcpy(validreg, CarReg); 
     printf("Accepted Car Reg is : %s\n\n\n\n", validreg); 
     printf("==================================================\n\n\n"); 
    } 
    for (row=1; row<26; row++) 
    { 
     for (position=1;position<5; position++) 
     { 
      parkingspace[row][position]=validreg; 
      printf("parkingspace \t row[%d] position[%d] =[ %c ]\n", row,position,parkingspace[row][position]); 
     } 
    } 
} 
+0

你忘了添加代碼 – 2013-03-06 11:38:33

+0

您忘記了密碼... – 2013-03-06 11:38:33

+0

你還指望它來打印,它有什麼打印 – 2013-03-06 11:41:16

回答

0

您在39行犯了一個錯誤

parkingspace[row][position]=validreg; 

應該是你必須是陣列從零到大小的陣列減去一個索引

parkingspace[row][position]=validreg[position]; 
1

的一個問題。因此row的有效索引是024,而不是125像你一樣。


另一個問題是,你嘗試的字符數組分配到單個字符:

parkingspace[row][position]=validreg; 

如果你只想在parkingspace[row]validreg前四個字符,然後做例如這樣的內部循環:

for (position=0; position < 4; position++) 
{ 
    parkingspace[row][position] = validreg[position]; 
    printf("parkingspace \t row[%d] position[%d] =[ %c ]\n", row, position, parkingspace[row][position]); 
} 
0
for (row=1; row<26; row++) 
{ 
    for (position=1;position<5; position++) 

這兩行應修改爲

for (row = 0; row < 24; row ++) 
{ 
    for (position =0; position < 3; poisition++) 

這是因爲u必須聲明爲字符parkingspace [25] [4]; 陣列開始在索引「0」和去錢櫃(大小 - 1)