打印数组中的项目不会打印项目

时间:2016-11-25 02:06:58

标签: c

#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 20

struct Item
{
int SKU;
char name[MAX_LENGTH+1];

};

int contains(void)
    {
    int compare;
    return 0;
    }

void displayItemWith(struct Item item[], int count)
    {
    int i;

    char alphabet;
    i = 0;
    for(i=0; i<count; i++){
            //char inventory[ ] = {item->SKU};
            printf("%d", item[i].SKU);
    }



int main(void)
{
int compare;
char count;

    //prints the title
    printf("=== TEST ===\n");

//硬编码库存21项 - 还有0个房间

    struct Item inventory[21] =
    {

    // price  sku  txd qty min  name

            {  .SKU =   275, "Royal Gala Apples"   },
            {  .SKU =   386, "Honeydew Melon"      },
            {  .SKU =   240, "Blueberries"         },
            {  .SKU =   916, "Seedless Grapes"     },
            {  .SKU =   385, "Pomegranate"         },
            {  .SKU =   495, "Banana"              },
            {  .SKU =   316, "Kiwifruit"           },
            {  .SKU =   355, "Chicken Alfredo"     },
            {  .SKU =   846, "Veal Parmigiana"     },
            {  .SKU =   359, "Beefsteak Pie"       },
            {  .SKU =   127, "Curry Chicken"       },
            {  .SKU =   238, "Tide Detergent"      },
            {  .SKU =   324, "Tide Liq. Pods"      },
            {  .SKU =   491, "Tide Powder Det."    },
            {  .SKU =   538, "Lays Chips S&V"      },
            {  .SKU =   649, "Joe Org Chips"       },
            {  .SKU =   731, "Allen's Apple Juice" },
            {  .SKU =   984, "Coke 12 Pack"        },
            {  .SKU =   350, "Nestea 12 Pack"      },
            {  .SKU =   835, "7up 12 Pack"         }
    };


    printf("*** NO MATCHES ***\n");
    printf("=== END ===\n");
    return 0;
}

您好,用户,我正在尝试打印出项目列表 以这种格式输出; SKU:275 - 皇家嘎拉苹果 SKU:386 - Honeydew Melon

但唯一可以打印的是=== TEST ===, NO MATCHES ,=== END ===。

有什么理由?

1 个答案:

答案 0 :(得分:0)

因为无论你做什么,都会显示“NO MATCHES”,因为你无条件地打印它。此外,为了打印列表,您需要调用您创建的方法“displayItemWith(struct Item item [],int count)”,并请首先声明结构以获得更好的方法。示例:

  struct Books {
  char  title[50];
  char  author[50];
  char  subject[100];
  int   book_id;
  };