fork download
  1. #include <stdio.h>
  2. #include "readline.h"
  3.  
  4. #define NAME_LEN 25
  5. #define MAX_PARTS 100
  6.  
  7. struct part {
  8. int number;
  9. char name[NAME_LEN+1];
  10. int on_hand;
  11. };
  12.  
  13. int find_part(int number, const struct part inv[], int np);
  14. void insert(struct part inv[], int *np);
  15. void search(const struct part inv[], int np);
  16. void update(struct part inv[], int np);
  17. void print(const struct part inv[], int np);
  18.  
  19. /**********************************************************
  20.  * main: Prompts the user to enter an operation code, *
  21.  * then calls a function to perform the requested *
  22.  * action. Repeats until the user enters the *
  23.  * command 'q'. Prints an error message if the user *
  24.  * enters an illegal code. *
  25.  **********************************************************/
  26. int main(void)
  27. {
  28. char code;
  29. struct part inventory[MAX_PARTS];
  30. int num_parts = 0;
  31.  
  32. for (;;) {
  33. printf("Enter operation code: ");
  34. scanf(" %c", &code);
  35. while (getchar() != '\n') /* skips to end of line */
  36. ;
  37. switch (code) {
  38. case 'i': insert(inventory, &num_parts);
  39. break;
  40. case 's': search(inventory, num_parts);
  41. break;
  42. case 'u': update(inventory, num_parts);
  43. break;
  44. case 'p': print(inventory, num_parts);
  45. break;
  46. case 'q': return 0;
  47. default: printf("Illegal code\n");
  48. }
  49. printf("\n");
  50. }
  51. }
  52.  
  53. /**********************************************************
  54.  * find_part: Looks up a part number in the inv array. *
  55.  * Returns the array index if the part number *
  56.  * is found; otherwise, returns -1. *
  57.  **********************************************************/
  58. int find_part(int number, const struct part inv[], int np)
  59. {
  60. int i;
  61.  
  62. for (i = 0; i < np; i++)
  63. if (inv[i].number == number)
  64. return i;
  65. return -1;
  66. }
  67.  
  68. /**********************************************************
  69.  * insert: Prompts the user for information about a new *
  70.  * part and then inserts the part into the inv *
  71.  * array. Prints an error message and returns *
  72.  * prematurely if the part already exists or the *
  73.  * array is full. *
  74.  **********************************************************/
  75. void insert(struct part inv[], int *np)
  76. {
  77. int part_number;
  78.  
  79. if (*np == MAX_PARTS) {
  80. printf("Database is full; can't add more parts.\n");
  81. return;
  82. }
  83.  
  84. printf("Enter part number: ");
  85. scanf("%d", &part_number);
  86. if (find_part(part_number, inv, *np) >= 0) {
  87. printf("Part already exists.\n");
  88. return;
  89. }
  90.  
  91. inv[*np].number = part_number;
  92. printf("Enter part name: ");
  93. read_line(inv[*np].name, NAME_LEN);
  94. printf("Enter quantity on hand: ");
  95. scanf("%d", &inv[*np].on_hand);
  96. (*np)++;
  97. }
  98.  
  99. /**********************************************************
  100.  * search: Prompts the user to enter a part number, then *
  101.  * looks up the part in the inv array. If the *
  102.  * part exists, prints the name and quantity on *
  103.  * hand; if not, prints an error message. *
  104.  **********************************************************/
  105. void search(const struct part inv[], int np)
  106. {
  107. int i, number;
  108.  
  109. printf("Enter part number: ");
  110. scanf("%d", &number);
  111. i = find_part(number, inv, np);
  112. if (i >= 0) {
  113. printf("Part name: %s\n", inv[i].name);
  114. printf("Quantity on hand: %d\n", inv[i].on_hand);
  115. } else
  116. printf("Part not found.\n");
  117. }
  118.  
  119. /**********************************************************
  120.  * update: Prompts the user to enter a part number. *
  121.  * Prints an error message if the part can't be *
  122.  * found in the inv array; otherwise, prompts the *
  123.  * user to enter change in quantity on hand and *
  124.  * updates the array. *
  125.  **********************************************************/
  126. void update(struct part inv[], int np)
  127. {
  128. int i, number, change;
  129.  
  130. printf("Enter part number: ");
  131. scanf("%d", &number);
  132. i = find_part(number, inv, np);
  133. if (i >= 0) {
  134. printf("Enter change in quantity on hand: ");
  135. scanf("%d", &change);
  136. inv[i].on_hand += change;
  137. } else
  138. printf("Part not found.\n");
  139. }
  140.  
  141. /**********************************************************
  142.  * print: Prints a listing of all parts in the inv array, *
  143.  * showing the part number, part name, and *
  144.  * quantity on hand. Parts are printed in the *
  145.  * order in which they were entered into the *
  146.  * array. *
  147.  **********************************************************/
  148. void print(const struct part inv[], int np)
  149. {
  150. int i;
  151.  
  152. printf("Part Number Part Name "
  153. "Quantity on Hand\n");
  154. for (i = 0; i < np; i++)
  155. printf("%7d %-25s%11d\n", inv[i].number,
  156. inv[i].name, inv[i].on_hand);
  157. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:2:22: fatal error: readline.h: No such file or directory
compilation terminated.
stdout
Standard output is empty