0

All the values seem to be where they should be when I use or display them, but for some reason every time I get to a certain point (flagged in code), the program crashes.

The program is supposed to be adding a struct to an array. This struct contains another struct along with an int denoting quantity. These are also being stored in an array.

Included are the main.c and functions surrounding the crash.

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;
}

FUNCTIONS

//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);
}

Thanks in advance for any and all help.

Deidrei
  • 2,125
  • 1
  • 14
  • 14
Aezur
  • 331
  • 2
  • 8

3 Answers3

3

scanf() takes pointers to the variables it reads the input into.

You do this by taking the address of choice, using an &:

scanf("%d", &choice);
Phillip Kinkade
  • 1,382
  • 12
  • 18
3

You pass choice to scanf but you meant to pass &choice. Your code should read:

scanf("%d", &choice);

What happens is scanf needs an address for it to write the value to. You pass a value rather than the value's address and so scanf treats that value as an address. So, suppose that choice is a variable with the value 0. Passing the value of choice rather than its address leads to scanf attempting to write to the address 0. And that typically leads to segmentation fault.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

fflush(stdin); is undefined behavior, that might be the cause for crash!!!

Also as others pointed out, you have missed & (address of operator) in scanf

Also, don't use fflush(stdin), instead if you want to clear the stdin you can do something like below...

int c;
while ((c = getchar()) != '\n' && c != EOF);
  • 1
    It's undefined in the C specification, but defined in e.g. Linux using glibc as well as Windows. – Some programmer dude Dec 13 '13 at 07:19
  • @JoachimPileborg: That's a new learning I got. Was not knowing this... As per this post, http://stackoverflow.com/a/11604639/1814023 Thanks –  Dec 13 '13 at 07:24