JBTALKS.CC

标题: 清空array 的問題!(C 語言) [打印本页]

作者: 狂天使    时间: 2012-3-13 06:33 PM
标题: 清空array 的問題!(C 語言)
各位大大,我現在的問題是當我要delete 資料的時候我要將struct 裡面的name 刪除!但是一直出現184  incompatible types in assignment ! 請問怎樣解決

這一行 ‘ inventory.name='\0';

  1. #include <stdio.h>
  2. #include "readline.h"

  3. #define NAME_LEN 25
  4. #define MAX_PARTS 100

  5. struct part {
  6.   int number;
  7.   char name[NAME_LEN+1];
  8.   int on_hand;
  9. } inventory[MAX_PARTS];

  10. int num_parts = 0;   /* number of parts currently stored */

  11. int find_part(int number);
  12. void insert(void);
  13. void search(void);
  14. void update(void);
  15. void delete1(void);

  16. void print(void);

  17. /**********************************************************
  18. * main: Prompts the user to enter an operation code,     *
  19. *       then calls a function to perform the requested   *
  20. *       action. Repeats until the user enters the        *
  21. *       command 'q'. Prints an error message if the user *
  22. *       enters an illegal code.                          *
  23. **********************************************************/
  24. int main(void)
  25. {
  26.   char code;

  27.   for (;;) {
  28.     printf("Enter operation code: ");
  29.     scanf(" %c", &code);
  30.     while (getchar() != '\n')   /* skips to end of line */
  31.       ;
  32.     switch (code) {
  33.       case 'i': insert();
  34.                 break;
  35.       case 's': search();
  36.                 break;
  37.       case 'u': update();
  38.                 break;
  39.       case 'p': print();
  40.                 break;
  41.       case 'd': delete();
  42.                 break;
  43.       case 'q': return 0;
  44.       default:  printf("Illegal code\n");
  45.     }
  46.     printf("\n");
  47.   }
  48. }

  49. /**********************************************************
  50. * find_part: Looks up a part number in the inventory     *
  51. *            array. Returns the array index if the part  *
  52. *            number is found; otherwise, returns -1.     *
  53. **********************************************************/
  54. int find_part(int number)
  55. {
  56.   int i;

  57.   for (i = 0; i < num_parts; i++)
  58.     if (inventory[i].number == number)
  59.       return i;
  60.   return -1;
  61. }

  62. /**********************************************************
  63. * insert: Prompts the user for information about a new   *
  64. *         part and then inserts the part into the        *
  65. *         database. Prints an error message and returns  *
  66. *         prematurely if the part already exists or the  *
  67. *         database is full.                              *
  68. **********************************************************/
  69. void insert(void)
  70. {
  71.   int part_number;

  72.   if (num_parts == MAX_PARTS) {
  73.     printf("Database is full; can't add more parts.\n");
  74.     return;
  75.   }

  76.   printf("Enter part number: ");
  77.   scanf("%d", &part_number);
  78.   if (find_part(part_number) >= 0) {
  79.     printf("Part already exists.\n");
  80.     return;
  81.   }

  82.   inventory[num_parts].number = part_number;
  83.   printf("Enter part name: ");
  84.   read_line(inventory[num_parts].name, NAME_LEN);
  85.   printf("Enter quantity on hand: ");
  86.   scanf("%d", &inventory[num_parts].on_hand);
  87.   num_parts++;
  88. }

  89. /**********************************************************
  90. * search: Prompts the user to enter a part number, then  *
  91. *         looks up the part in the database. If the part *
  92. *         exists, prints the name and quantity on hand;  *
  93. *         if not, prints an error message.               *
  94. **********************************************************/
  95. void search(void)
  96. {
  97.   int i, number;

  98.   printf("Enter part number: ");
  99.   scanf("%d", &number);
  100.   i = find_part(number);
  101.   if (i >= 0) {
  102.     printf("Part name: %s\n", inventory[i].name);
  103.     printf("Quantity on hand: %d\n", inventory[i].on_hand);
  104.   } else
  105.     printf("Part not found.\n");
  106. }

  107. /**********************************************************
  108. * update: Prompts the user to enter a part number.       *
  109. *         Prints an error message if the part doesn't    *
  110. *         exist; otherwise, prompts the user to enter    *
  111. *         change in quantity on hand and updates the     *
  112. *         database.                                      *
  113. **********************************************************/
  114. void update(void)
  115. {
  116.   int i, number, change;

  117.   printf("Enter part number: ");
  118.   scanf("%d", &number);
  119.   i = find_part(number);
  120.   if (i >= 0) {
  121.     printf("Enter change in quantity on hand: ");
  122.     scanf("%d", &change);
  123.     inventory[i].on_hand += change;
  124.   } else
  125.     printf("Part not found.\n");
  126. }

  127. /**********************************************************
  128. * print: Prints a listing of all parts in the database,  *
  129. *        showing the part number, part name, and         *
  130. *        quantity on hand. Parts are printed in the      *
  131. *        order in which they were entered into the       *
  132. *        database.                                       *
  133. **********************************************************/
  134. void print(void)
  135. {
  136.   int i;

  137.   printf("Part Number   Part Name                  "
  138.          "Quantity on Hand\n");
  139.   for (i = 0; i < num_parts; i++)
  140.     printf("%7d       %-25s%11d\n", inventory[i].number,inventory[i].name, inventory[i].on_hand);
  141. }

  142. void delete1(void)
  143. {
  144.      int i, number, change;
  145.      printf("Enter part number: ");
  146.      scanf("%d", &number);
  147.      i = find_part(number);
  148.      
  149.      if (i >= 0)
  150.      {

  151.         inventory[i].name='\0';
  152.         inventory[i].number=0;
  153.         inventory[i].on_hand=0;
  154.         num_parts--;
  155.      }
  156.     else
  157.       printf("Part not found.\n");
  158. }
复制代码

作者: Super-Tomato    时间: 2012-3-15 07:11 PM
各位大大,我現在的問題是當我要delete 資料的時候我要將struct 裡面的name 刪除!但是一直出現184  incomp ...
狂天使 发表于 2012-3-13 06:33 PM



在 c 需要使用 memset




欢迎光临 JBTALKS.CC (https://jbtalks.my/) Powered by Discuz! X2.5