尽管尝试了一切,但仍无法在C中初始化我的数组计数器

时间:2018-09-14 08:34:59

标签: c arrays initialization

我一直在做这项学校作业,以创建餐厅菜单,并且我有一个数组来存储一个人订购的所有东西。存储订购的订单的数组是数组OrderedItems [30]。该数组基本上是一个计数器。以某人订购的订单号1为例,OrderItems [1]增加1。

我尝试通过使用OrderedItems [30] = {0}进行初始化,并使用for循环分别初始化每个位置,但是,这没有用。请帮我初始化这个数组。我也尝试过memset(OrderedItems,0,30);而且这也不起作用,所以我真的不知道该怎么办。

我还想补充一点,因为我听说所有全局声明都自动初始化为0,所以我尝试全局声明OrderedItems数组,但这也行不通。

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

1 个答案:

答案 0 :(得分:1)

memset的调用应指定要设置的个字节。您需要告诉它要初始化的类型的大小。

memset(OrderedItems, 0, 30 * sizeof(int))