【发布时间】:2013-12-13 07:14:44
【问题描述】:
当我使用或显示它们时,所有值似乎都在它们应该在的位置,但由于某种原因,每次我到达某个点(在代码中标记)时,程序就会崩溃。
该程序应该将结构添加到数组中。此结构包含另一个 struct 以及一个表示数量的 int。这些也被存储在一个数组中。
包括 main.c 和崩溃相关的函数。
int main()
{
int choice;
int item_num;
int item_amount;
printf("*****MEAL PLANNER*****");
while(choice != 0)
{
prepopulate();
printf("\n\nTo view items, enter 1. \nTo view current menu, enter 2. \nTo add an item to the menu, enter 3. \nEnter 0 to exit.\n\n");
scanf("%d", &choice);
if(choice > 0 && choice < 5)
{
switch(choice)
{
case 1 : printf("\nView Items\n\n");
displayAllFoodStuff();
break;
case 2 :printf("\nView Current Menu\n\n");
display_meal();
break;
case 3 :printf("\nAdd Item to Menu\n\n");
printf("Enter item number.\n");
scanf("%d", &item_num);
printf("Enter item amount.\n");
scanf("%d", &item_amount);
if(item_num <= fs_count)
{
printf("Enter 1 to confirm.\n");
scanf("%d", choice);
fflush(stdin);
////////CRASHING HERE!!!////////
if(choice == 1)
{
choose_food_item(food_list[item_num], item_amount);
}
break;
}
else
{
printf("\nThat is not a valid entry.\n");
}
break;
default: choice = 0;
break;
}
}
}
return 0;
}
函数
//Define 'add_food_plan_item' function.
void add_food_plan_item(food_plan_item fs)
{
meal[item_count] = fs;
item_count++;
}
//Define 'choose_food_item' function.
void choose_food_item(food_stuff fs, int qty)
{
food_plan_item fpi;
fpi.fs = fs;
fpi.qty = qty;
add_food_plan_item(fpi);
}
提前感谢所有帮助。
【问题讨论】:
-
你能推荐一个好的免费的吗? (这在 cmets 中是允许的,对吧?)