C程序为什么它在我输入后崩溃

问题描述:

我有一些问题得到我的输入,它进入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

你得到一个赛格故障是由于未初始化的变量“计数”被用来访问数组,导致你超出数组的边界。

你假设count有一个确定的值(0?),但实际上它有任何垃圾碰巧是他们当你创建变量。你需要明确地说count = 0;

+0

因此,我提前提出了所有更改,例如初始化main中的声明,然后更改if中的语句,并且在我按下条目ID –

+0

上的输入后仍然崩溃并复制并粘贴您提交的代码。只更改第32行以将计数初始化为0.重新编译并尝试输入。这一次的改变让我能够运行你的程序,所以我不确定自那以后发生了什么变化。 – hulud

+0

所以我错过了第32行,但做了所有其他更改,现在它的工作,但是,当我添加三是最大它不拒绝我,当我去添加第四个,所以现在我有保存问题数组, –

主要功能,计数宣告但尚未初始化,计数具有意义的价值,你必须初始化到计数变量为0

int main (void) 
{ // OPENS MAIN 


// Declarations 

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