2017-04-18 88 views
-1

我有一些問題得到我的輸入,它進入itemID後崩潰,我完全失去了,我與數組一起工作,如果任何人都可以幫助我會真的很棒。也謝謝,我知道我的編碼是廢話。C程序爲什麼它在我輸入後崩潰

#include <stdio.h> 
#include <stdlib.h> 
#define MAX 3 

//Structed Items 

struct item{ 

char itemname[20]; 
char itemdes[30]; 
int itemID; 
int itemOH; 
double itemUP; 
}; 


// Function Declarations 

int getMenu_Choice(); 
int process (int choice, int count, struct item inven[]); 
    int add (int count, struct item inven[]); 
int showall(int count, struct item inven[]); 


int main (void) 
{ // OPENS MAIN 


// Declarations 

int choice; 
int count; 
struct item inven[MAX]; 


// Statements 

do// 
{ 
choice = getMenu_Choice(); 
process (choice, count, inven); 
} 
while (choice != 0); 


return 0; 


} // CLOSE MAIN 

/*============================getChoice=*/ 

int getMenu_Choice (void) 
{ //OPEN GETCHOICE 


// Declarations 
int choice; 

// Statements 

printf("\n\n**********************************"); 
printf("\n    MENU    "); 
printf("\n\t1.Create A File   "); 
printf("\n\t2.Read A File   "); 
printf("\n\t0.Exit     "); 
printf("\n**********************************"); 
printf("\nPlease Type Your Choice Using 0-2"); 
printf("\nThen Hit Enter: "); 
scanf("%d", &choice); 

return choice; 

} //CLOSES GET CHOICE 




/*============================process=*/ 

int process (int choice, int count, struct item inven[]) 
{// OPEN PROCESS 


// Declarations 


// Statements 
switch(choice) 
    { 
     case 1: count = add(count, inven); 
      break; 
     case 2: showall(count, inven); 
      break; 
     case 0: exit; 
      break; 
     deafult: printf("Sorry Option Not Offered"); 
      break; 

} // switch 

return count; 

} // CLOSE PROCESS 


/*============================add one=*/ 
int add(int count, struct item inven[]) 

{//OPENS CREATE 

// Declarations 

int i; 


i = count; 

if (count != MAX) 
{ 
printf("Enter the Item ID:\n"); 
scanf("%d", &inven[i].itemID); 

printf("Enter the Item Name:\n"); 
scanf("%s", &inven[i].itemname); 
i++; 

} 

else { 
printf("sorry there is no more room for you to add"); 

}; 

return i; 


}; // CLOSE CREATE 



/*============================showall=*/ 

int showall(int count, struct item inven[]) 
{ 
//Declarations 
int i; 

// Statements 




for(i = 0; i < MAX; i++) 
{ 
printf("\nItem ID : %d", inven[i].itemID); 
printf("\nItem Name : %s", inven[i].itemname); 
};  

return 0; 
} 
+1

'count'在'add'中未初始化。另外,'process'中的'count = add(count,inven);'不會更新'main'中的'count'變量。 – aschepler

+0

好的,你建議我做什麼? –

+0

1)'int count;' - >'int count = 0;' – BLUEPIXY

回答

0

你得到一個賽格故障是由於未初始化的變量「計數」被用來訪問數組,導致你超出數組的邊界。

你假設count有一個確定的值(0?),但實際上它有任何垃圾碰巧是他們當你創建變量。你需要明確地說count = 0;

+0

因此,我提前提出了所有更改,例如初始化main中的聲明,然後更改if中的語句,並且在我按下條目ID –

+0

上的輸入後仍然崩潰並複製並粘貼您提交的代碼。只更改第32行以將計數初始化爲0.重新編譯並嘗試輸入。這一次的改變讓我能夠運行你的程序,所以我不確定自那以後發生了什麼變化。 – hulud

+0

所以我錯過了第32行,但做了所有其他更改,現在它的工作,但是,當我添加三是最大它不拒絕我,當我去添加第四個,所以現在我有保存問題數組, –

0

主要功能,計數宣告但尚未初始化,計數具有意義的價值,你必須初始化到計數變量爲0

int main (void) 
{ // OPENS MAIN 


// Declarations 

int choice; 
int count;  // --->>>> int count = 0; 
struct item inven[MAX]; 
//... 
}