fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6. #define CUSHION 50 /* for array sizes*/
  7. #define RECLMT 20 /* amount of records */
  8.  
  9. struct book
  10. {
  11. char title[CUSHION];
  12. char firstname[CUSHION];
  13. char lastname[CUSHION];
  14. char isbn[13]; /*ISBN Number consists of 13 numbers*/
  15. char publisher[CUSHION];
  16. char dop[4]; /*only the year needed*/
  17. };
  18.  
  19. struct book Book[RECLMT]; /*20 records*/
  20.  
  21. int populate(void);
  22.  
  23. void welcome_menu(void); /* Options menu */
  24.  
  25. int choice(char);
  26.  
  27. /* MAIN STARTS HERE */
  28. int main()
  29. {
  30. welcome_menu();
  31.  
  32. return 0;
  33. }
  34.  
  35. /* MAIN ENDS HERE */
  36.  
  37. /* start menu */
  38. void welcome_menu(void)
  39. {
  40. char p;
  41. printf(" ******************************************\n");
  42. printf(" * Welcome to the Mile End Library System *\n");
  43. printf(" ******************************************\n\n");
  44. printf("Type the number corresponding\n to your desired action:\n\n");
  45. printf("1. ADD A BOOK\n2. UPDATE AN EXISTING ENTRY\n3. BOOK SEARCH\n4. PRINT ALL FILE RECORDS\n");
  46. printf("\n5. (FOR ADMIN USE ONLY) POPULATE FILE\n\n");/*options keys*/
  47.  
  48. p=getchar();
  49. choice(p);
  50. }
  51.  
  52.  
  53.  
  54. int populate(void)
  55. {
  56. printf("All entries will be saved in the file, books.dat.\n\n" );
  57.  
  58. int c=0, x=0;
  59.  
  60. /* struct book Book[RECLMT]; */
  61.  
  62. char s;
  63.  
  64. for(;c<=RECLMT;c++){
  65.  
  66. printf("Title?\t\t");
  67.  
  68. while ( ( s = getchar() ) !='\n' ){
  69.  
  70. Book[c].title[ x++ ] = s;
  71. }
  72.  
  73. Book[c].title[x]='\0';
  74.  
  75. printf("Author's first name?\t");
  76.  
  77. while ( ( s = getchar() ) !='\n' ){
  78.  
  79. Book[c].firstname[ x++ ] = s;
  80. }
  81.  
  82. Book[c].firstname[x]='\0';
  83.  
  84. printf("Author's last name?\t");
  85.  
  86. while ( ( s = getchar() ) !='\n' ){
  87.  
  88. Book[c].lastname[ x++ ] = s;
  89. }
  90.  
  91. Book[c].lastname[x]='\0';
  92.  
  93. printf("ISBN#: ");
  94.  
  95. while ( ( s = getchar() ) !='\n' ){
  96.  
  97. Book[c].isbn[ x++ ] = s;
  98. }
  99.  
  100. Book[c].isbn[x]='\0';
  101.  
  102.  
  103. printf("Year of publication?\t");
  104.  
  105. while ( ( s = getchar() ) !='\n' ){
  106.  
  107. Book[c].dop[ x++ ] = s;
  108. }
  109.  
  110. Book[c].dop[x]='\0';
  111.  
  112. printf("Next book -->\n");
  113. }
  114.  
  115. struct book *books;
  116. *books = Book ;
  117. FILE *bibPtr; /* file handle */
  118. bibPtr=fopen( "books.dat","w" );
  119. if( bibPtr == NULL )
  120. { printf( "No file found.\n" );}
  121. else
  122. {
  123. fwrite(books,sizeof(*Book),RECLMT,bibPtr);
  124. }
  125. fclose(bibPtr);
  126. }
  127. int choice(char p)
  128. {
  129. switch(p)
  130. {
  131. case '1':
  132. break;
  133. case '2':
  134. break;
  135. case '3':
  136. break;
  137. case '4':
  138. break;
  139. case '5':
  140. populate();
  141. break;
  142. default:
  143. welcome_menu;
  144. }
  145. }
  146.  
  147. /* A function to remedy the repeated requests in populate() */
  148.  
  149. /*
  150. *entry(char[],int)
  151. {
  152. while ( ( s = getchar() ) !='\n' ){
  153.  
  154. Book[c].title[ x++ ] = s;
  155. }
  156.  
  157. Book[c].title[x]='\0';
  158.  
  159. }
  160. */
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty