我有一些問題得到我的輸入,它進入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;
}
'count'在'add'中未初始化。另外,'process'中的'count = add(count,inven);'不會更新'main'中的'count'變量。 – aschepler
好的,你建議我做什麼? –
1)'int count;' - >'int count = 0;' – BLUEPIXY