1
0

book.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //* book.c -- one-book inventory */
  2. #include <stdio.h>
  3. #include <string.h>
  4. char * s_gets(char * st, int n);
  5. #define MAXTITL 41 /* maximum length of title + 1 */
  6. #define MAXAUTL 31 /* maximum length of author's name + 1 */
  7. struct book { /* structure template: tag is book */
  8. char title[MAXTITL];
  9. char author[MAXAUTL];
  10. float value;
  11. }; /* end of structure template */
  12. int main(void)
  13. {
  14. struct book library; /* declare library as a book variable */
  15. printf("Please enter the book title.\n");
  16. s_gets(library.title, MAXTITL); /* access to the title portion */
  17. printf("Now enter the author.\n");
  18. s_gets(library.author, MAXAUTL);
  19. printf("Now enter the value.\n");
  20. scanf("%f", &library.value);
  21. printf("%s by %s: $%.2f\n",library.title,
  22. library.author, library.value);
  23. printf("%s: \"%s\" ($%.2f)\n", library.author,
  24. library.title, library.value);
  25. printf("Done.\n");
  26. return 0;
  27. }
  28. char * s_gets(char * st, int n)
  29. {
  30. char * ret_val;
  31. char * find;
  32. ret_val = fgets(st, n, stdin);
  33. if (ret_val)
  34. {
  35. find = strchr(st, '\n'); // look for newline
  36. if (find) // if the address is not NULL,
  37. *find = '\0'; // place a null character there
  38. else
  39. while (getchar() != '\n')
  40. continue; // dispose of rest of line
  41. }
  42. return ret_val;
  43. }