我試圖掃描字符串不尋常的行爲,它需要有10與scanf函數
struct Person{
char name[MAX];
float score;
date_t dafe;
};
//MAX = 11
我使用的菜單,當輸入的字符串少於10個字符,它工作正常一個最大長度但是當字符串更長時,它會遍歷菜單並將字符串的其餘部分(任何超過10個字符的字符)存儲在數組中下一個學生的名稱值中(創建一個新學生)。我究竟做錯了什麼?這是用scanf限制字符串的正確方法嗎?
while(choice != 6){
printMenu();
scanf("%d", &choice);
switch(choice){
case 1 :
if(Size < MAX){
printf("Adding Person \n");
personList[Size] = addPerson();
Size++;
}else{
printf("Reached maximum size \n");
}
break;
case 2 : //Other Options
Person_t addperson(void){
person_t tempperson;
printf("Enter name> \n");
scanf("%10s", tempPerson.name);
printf("Enter date: day> \n");
scanf("%d", &person.date.day);
//more values
return tempPerson;
}
MAX_NAME_SIZE的值是多少? – chux
它被評論在第一個代碼塊中,11 – XcammanX
所以它是!在'struct student {...'之前建議'#define MAX_NAME_SIZE 11'。代碼比評論更響亮。 – chux