Search code examples
carraysinitialization

Can't initialize my array counter in C despite trying everything


I've been working on this school assignment which creates a restaurant menu, and I have an array to store everything that a person orders. The array that stores the orders ordered is array OrderedItems [30]. This array is basically a counter. When a person orders order number 1 as an example OrderItems[1] increases by 1.

I've tried to initialize by using OrderedItems [30] = {0}, and using a for loop to initialize every spot individually, However, that didn't work. Please help me initialize this array. I've also tried memset(OrderedItems, 0, 30); and this didn't work too so I really have no clue what to do.

I also want to add that I've tried to globally declare the OrderedItems array since I've heard that all global declarations are automatically initialized to 0, but that also didn't work.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>

    FILE *fPointer1; //for food//
    FILE *fPointer2; //for invoice//
    int count;
    char name[50];
    float price;

void FunctionToPrintFoodItems (void)
{
    fPointer1 = fopen ("Food.txt", "a+");
    float price;

    printf("ITEMCODE\tDESCIPTION\tPRICE (RM)\n");
    while (!feof (fPointer1))
    {
    fscanf(fPointer1, "%d %s %f", &count, name, &price);
    printf("%d\t\t%s\t\t%.2f\n", count, name, price);
    }
    fclose (fPointer1);
}

void clrscr()
{
    system("@cls||clear"); //might not need. Will delete it not needed//
}

void debug (void)
{
    printf("THIS IS PRINTED");
}

#define clear clrscr ();
#define printfood FunctionToPrintFoodItems();
#define de debug();


int main ()
{
    fPointer1 = fopen("Food.txt", "w");
    fprintf(fPointer1, "1 BigM 10.40\n");
    fprintf(fPointer1, "2 Cheeseburger 9.45");
    fclose (fPointer1);

    int i;  
    int MainMenuCode;
    int additems = 0;
    int orderitems;
    int item;
    int ordered;

    int OrderedItems[30] = {0};

    memset(OrderedItems, 0, 30);

    for (i=0 ; i < 30 ; i++)
    {
    OrderedItems[i] = NULL;
    printf("%d: %d\n", i, OrderedItems);
    }
    do
    {
    printf("WELCOME TO RESTOURANT MAC C - Main Menu\n\n");
    printf("[1] Add new food items\n\n");
    printf("[2] Take order\n\n");
    printf("Enter ITEM CODE (0 to Quit) : ");
    scanf("%d", &MainMenuCode);


    if (MainMenuCode  == 1)
    {
        clear;
        additems = 1;
        printf("WELCOME TO RESTAURANT MAC C - Add Menu\n\n");
        printfood;

        printf("\n");

        while ( additems == 1 )

            {
            printf("Enter description (0 to Main Menu) : ");
            scanf("%s", name);

            if (strcmp (name, "0") == 0)
            {
                additems = 0;
                clear;
                break;
            }
            printf("Enter price (RM) : ");
            scanf("%f", &price);

            count ++;

            fPointer1 = fopen ("Food.txt", "a");
            printf("\n%d\t\t%s\t\t%.2f\n\n", count, name, price);
            fprintf(fPointer1, "\n%d %s %.2f", count, name, price);
            fclose (fPointer1);

            }
    }
    else if (MainMenuCode == 2)
    {
        clear;
        orderitems = 1;
        printf("WELCOME TO RESTAURANT MAC C- Take Order\n\n");
        printfood;

    while (orderitems == 1)
        {
            fPointer1 = fopen ("Food.txt", "a+");



            printf("Enter ITEM CODE (0 to Quit, 100 for Main Menu) : ");
            scanf("%d", &item);


            if (item == 100) break;
            else if (item == 0) //final approach//
            {
                fPointer2 = fopen ("Invoice.txt", "a+");

                de;

                fclose (fPointer2);
                return 0;
            }
            else if (item == 900)
            {
                for (i=0 ; i < 30 ; i++)
             printf("%d: %d\n", i, OrderedItems);
            }
            else if (item > count || item < 1)
            {
                printf("\n\nITEM CODE not available. Try again\n\n");
            }
            else 
            {
                OrderedItems[item]++;       
                printf("%d %d", item, OrderedItems);        
            }
            fclose (fPointer1);
        }

    }
    else printf("Please enter a valid Menu Code\n");
    } while (MainMenuCode != 0);
    return 0;
}

Solution

  • -  printf("%d: %d\n", i, OrderedItems);
    +  printf("%d: %d\n", i, OrderedItems[i]);
    

    OrderedItems is an address of the array's first element.

    OrderedItems[0] is a value of the array's first element.

    OrderedItems[i] is a value of the array's i-th element.