fork download
  1. #define NUM_BOOKS 2
  2. #include <stdio.h> //#define _CRTMSECURE_NO_WARNINGS #include <string.h>
  3.  
  4. typedef struct
  5. {
  6. char title[20];
  7. char author[20];
  8. int pages;
  9. } Book;
  10.  
  11. void insertBooks(Book list[], int N)
  12. {
  13. int i;
  14. for (i = 0; i < N; i++)
  15. {
  16. printf("title? "); scanf_s("%s", list.title);
  17. printf("author? "); scanf_s("%s", list.author);
  18. printf("pages? "); scanf_s("%d", &list.pages);
  19. }
  20. }
  21.  
  22. void printBooks(Book list[], int N)
  23. {
  24. int i;
  25. for (i = 0; i < N; i++)
  26. {
  27. printf("title: %s ", list.title);
  28. printf("author: %s ", list.author);
  29. printf("pages: %d \n", list.pages);
  30. }
  31. }
  32.  
  33. void saveAsFile(Book list[], int N) // save as "output.txt"
  34. {
  35. FILE* fp = fopen("output.txt", "wt");
  36. int i;
  37. for (i = 0; i < N; i++)
  38. fprintf(fp, "%s %s %d", list.title, list.author, list.pages);
  39. fclose(fp);
  40. }
  41.  
  42. int main(void)
  43. {
  44. Book list[NUM_BOOKS];
  45. insertBooks(list, NUM_BOOKS);
  46. printBooks(list, NUM_BOOKS);
  47. saveAsFile(list, NUM_BOOKS);
  48. return 0;
  49. }
  50.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘void insertBooks(Book*, int)’:
prog.cpp:16:41: error: request for member ‘title’ in ‘list’, which is of pointer type ‘Book*’ (maybe you meant to use ‘->’ ?)
   printf("title? "); scanf_s("%s", list.title);
                                         ^~~~~
prog.cpp:16:46: error: ‘scanf_s’ was not declared in this scope
   printf("title? "); scanf_s("%s", list.title);
                                              ^
prog.cpp:17:42: error: request for member ‘author’ in ‘list’, which is of pointer type ‘Book*’ (maybe you meant to use ‘->’ ?)
   printf("author? "); scanf_s("%s", list.author);
                                          ^~~~~~
prog.cpp:18:42: error: request for member ‘pages’ in ‘list’, which is of pointer type ‘Book*’ (maybe you meant to use ‘->’ ?)
   printf("pages? "); scanf_s("%d", &list.pages);
                                          ^~~~~
prog.cpp: In function ‘void printBooks(Book*, int)’:
prog.cpp:27:29: error: request for member ‘title’ in ‘list’, which is of pointer type ‘Book*’ (maybe you meant to use ‘->’ ?)
   printf("title: %s ", list.title);
                             ^~~~~
prog.cpp:28:33: error: request for member ‘author’ in ‘list’, which is of pointer type ‘Book*’ (maybe you meant to use ‘->’ ?)
      printf("author: %s ", list.author);
                                 ^~~~~~
prog.cpp:29:31: error: request for member ‘pages’ in ‘list’, which is of pointer type ‘Book*’ (maybe you meant to use ‘->’ ?)
   printf("pages: %d \n", list.pages);
                               ^~~~~
prog.cpp: In function ‘void saveAsFile(Book*, int)’:
prog.cpp:38:32: error: request for member ‘title’ in ‘list’, which is of pointer type ‘Book*’ (maybe you meant to use ‘->’ ?)
   fprintf(fp, "%s %s %d", list.title, list.author, list.pages);
                                ^~~~~
prog.cpp:38:44: error: request for member ‘author’ in ‘list’, which is of pointer type ‘Book*’ (maybe you meant to use ‘->’ ?)
   fprintf(fp, "%s %s %d", list.title, list.author, list.pages);
                                            ^~~~~~
prog.cpp:38:57: error: request for member ‘pages’ in ‘list’, which is of pointer type ‘Book*’ (maybe you meant to use ‘->’ ?)
   fprintf(fp, "%s %s %d", list.title, list.author, list.pages);
                                                         ^~~~~
stdout
Standard output is empty