C程序为什么输入后会崩溃

C程序为什么输入后会崩溃,c,C,我在输入数据时遇到了一些问题,在我输入itemID后它就崩溃了,我完全迷路了,如果有人能帮我的话,我会使用数组,这真是太棒了。也谢谢,我知道我的编码是垃圾 #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

我在输入数据时遇到了一些问题,在我输入itemID后它就崩溃了,我完全迷路了,如果有人能帮我的话,我会使用数组,这真是太棒了。也谢谢,我知道我的编码是垃圾

#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;
}
#包括
#包括
#定义最大值3
//结构化项目
结构项{
char itemname[20];
charitemdes[30];
int itemID;
int itemOH;
双倍递增;
};
//函数声明
int getmenuu选项();
int进程(int选择、int计数、结构项inven[]);
int add(int count,结构项inven[]);
int showall(int count,结构项inven[]);
内部主(空)
{//打开主
//声明
智力选择;
整数计数;
结构项库存【最大值】;
//声明
做//
{   
choice=getmenuu choice();
过程(选择、计数、输入);
}
while(选项!=0);
返回0;
}//关闭主管道
/*==============================================getChoice=*/
int getMenu_选项(无效)
{//opengetchoice
//声明
智力选择;
//声明
printf(“\n\n****************************************”);
printf(“\n菜单”);
printf(“\n\t1.Create A File”);
printf(“\n\t2.读取文件”);
printf(“\n\t0.Exit”);
printf(“\n*************************************”);
printf(“\n请使用0-2键入您的选择”);
printf(“\n然后点击回车:”);
scanf(“%d”,选择(&C);
回报选择;
}//关闭获取选择
/*========================================处理=*/
int进程(int选项、int计数、结构项inven[])
{//开放进程
//声明
//声明
开关(选择)
{
案例1:count=add(count,inven);
打破
案例2:showall(count,inven);
打破
案例0:退出;
打破
deafult:printf(“对不起,未提供期权”);
打破
}//开关
返回计数;
}//关闭进程
/*===========================================添加一个=*/
整数添加(整数计数,结构项库存[])
{//打开创建
//声明
int i;
i=计数;
如果(计数!=最大值)
{
printf(“输入项目ID:\n”);
scanf(“%d”,&inven[i].itemID);
printf(“输入项目名称:\n”);
scanf(“%s”,&inven[i].itemname);
i++;
}
否则{
printf(“对不起,没有更多的空间供您添加”);
};
返回i;
}; // 关闭创建
/*====================================================showall=*/
int showall(int计数,结构项库存[])
{
//声明
int i;
//声明
对于(i=0;i
由于使用未初始化的变量“count”访问数组,导致您出现seg错误,从而超出了数组的界限

您假设count有一个特定的值(0?),但事实上,当您创建变量时,它有任何垃圾。您需要明确地说count=0

main函数中,count声明但未初始化,因此count具有无意义的值,必须初始化为count变量为0


count
add
中未初始化使用。另外,
count=add(count,inven)process
中的code>不会更新
main
中的
count
变量。好的,那么您建议我怎么做?1)
int count-->
int count=02)
过程(选择、计数、inven)-->
count=进程(选择、计数、inven)
3)
i
-->
i
所以我做了前面建议的所有更改,比如在main中初始化声明,然后在add中更改if语句,当我在IDcopy项上按enter键并粘贴您提交的代码后,它仍然崩溃。仅将第32行更改为将计数初始化为0。重新编译并尝试您的输入。这一个更改使我可以很好地运行您的程序,因此我不确定从那时起发生了什么更改。因此我错过了第32行,但做了所有其他更改,现在它工作了,但是,当我添加三个最大值时,它不会拒绝我添加第四个,因此现在我在保存到数组时遇到问题,您可以在add函数中增加I,并返回其值。此返回值在流程函数开关结构中捕获,然后返回。但从main调用进程的行并没有捕获计数的返回。因此,主作用域中count的版本永远不会更改。有很多方法可以解决这个问题,最简单的方法是更新代码的第32行,使count设置为等于进程函数调用的返回值。我该怎么做?整数计数=进程
int main (void)
{ // OPENS MAIN


//  Declarations

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