Advertisement
yanni_yagami

sys

Apr 29th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MOBILE_LENGTH 11
  5. typedef struct {
  6.     char name[256], mobile_number[MOBILE_LENGTH], book_name[256], author_name[256];
  7.     int cost;
  8. }data;
  9.  
  10. typedef enum {
  11.     false,
  12.     true
  13. }bool;
  14.  
  15. int press(void);
  16. void save_data(data client);
  17. void flush(void);
  18. void get_data(data *client);
  19. bool continue_or_not(void);
  20. void print_data(void);
  21.  
  22. int main(void) {
  23.     data client;
  24.     FILE *p;
  25.     char file_name[MOBILE_LENGTH + 4];
  26.     int numb;
  27.  
  28.     while(true) {
  29.         numb = press();
  30.  
  31.         switch(numb) {
  32.             case 1:
  33.             get_data(&client);
  34.             save_data(client);
  35.             break;
  36.  
  37.             case 2:
  38.             print_data();
  39.         }
  40.  
  41.         if(continue_or_not())
  42.             continue;
  43.         else
  44.             break;
  45.     }
  46.  
  47.     return 0;
  48. }
  49.  
  50. int press(void) {
  51.     int temp;
  52.  
  53.     printf("press 1 for sign-up.\n");
  54.     printf("press 2 to enter books.");
  55.  
  56.     while(true) {
  57.         printf("\n> ");
  58.         scanf("%d", &temp);
  59.  
  60.         if(temp == 1)
  61.             return 1;
  62.  
  63.         else if(temp == 2)
  64.             return 2;
  65.     }
  66. }
  67.  
  68. void save_data(data client) {
  69.     FILE *temp;
  70.     char file_name[MOBILE_LENGTH + 4];
  71.  
  72.     strcpy(file_name, client.mobile_number);
  73.     strcat(file_name, ".txt");
  74.  
  75.     temp = fopen(file_name, "a+");
  76.  
  77.     if(temp == NULL) {
  78.         fprintf(stderr, "error epenning the file.");
  79.         exit(EXIT_FAILURE);
  80.     }
  81.  
  82.     fprintf(temp, "%s%s\n%s%s%d\n", client.name, client.mobile_number, client.book_name, client.author_name, client.cost);
  83.     fclose(temp);
  84.     return;
  85. }
  86.  
  87. void flush(void) {
  88.     int flush = 0;
  89.  
  90.     while((flush = getchar()) != '\n' && flush != EOF);
  91.  
  92.     return;
  93. }
  94.  
  95. void get_data(data *client) {
  96.     flush();
  97.     printf("customer name     : ");
  98.     fgets(client->name, 256, stdin);
  99.  
  100.     printf("mobile number     : ");
  101.     fgets(client->mobile_number, MOBILE_LENGTH, stdin);
  102.     flush();
  103.  
  104.     printf("enter a book name : ");
  105.     fgets(client->book_name, 256, stdin);
  106.  
  107.     printf("book author       : ");
  108.     fgets(client->author_name, 256, stdin);
  109.  
  110.     printf("cost of the book  : ");
  111.     scanf("%d", &client->cost);
  112.     return;
  113. }
  114.  
  115. bool continue_or_not(void) {
  116.     char answer;
  117.     while(true) {
  118.         printf("would you like to continue ? (y/n) ");
  119.         scanf(" %c", &answer);
  120.         if(answer == 'y' || answer == 'Y')
  121.             return true;
  122.  
  123.         else if(answer == 'n' || answer == 'N')
  124.             return false;
  125.     }
  126. }
  127.  
  128. void print_data(void) {
  129.     char file_name[MOBILE_LENGTH + 4];
  130.     FILE *temp;
  131.     data client;
  132.  
  133.     flush();
  134.     printf("enter a mobile number : ");
  135.     fgets(file_name, MOBILE_LENGTH, stdin);
  136.     strcat(file_name, ".txt");
  137.  
  138.     temp = fopen(file_name, "r");
  139.     if(temp == NULL) {
  140.         printf("\nthis number isn't registered.\n");
  141.         return;
  142.     }
  143.  
  144.     fscanf(temp, "%s", client.name);
  145.     fscanf(temp, "%s", client.mobile_number);
  146.     fscanf(temp, "%s", client.book_name);
  147.     fscanf(temp, "%s", client.author_name);
  148.     fscanf(temp ,"%d", &client.cost);
  149.  
  150.     fclose(temp);
  151.  
  152.     printf("--------------------------------------\n");
  153.     printf("name   : %s\n", client.name);
  154.     printf("mobile : %s\n", client.mobile_number);
  155.     printf("--------------------------------------\n");
  156.     printf("book   : %s\n", client.book_name);
  157.     printf("author : %s\n", client.author_name);
  158.     printf("cost   : %d\n", client.cost);
  159.     printf("--------------------------------------\n");
  160.     return;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement