SHARE
TWEET

uh

a guest May 24th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*********************************
  2. * Class: MAGSHIMIM C2            *
  3. * Week:                          *
  4. * Name:                          *
  5. * Credits:                       *
  6. **********************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. #define ADD_PRO_REASON 1
  13. #define ADD_CON_REASON 2
  14. #define PROS_LIST_NAME "PRO"
  15. #define CONS_LIST_NAME "CON"
  16. #define PRINT_REASONS 3
  17. #define OPTIONS_MIN 1
  18. #define OPTIONS_MAX 4
  19. #define STR_LEN 75
  20. #define EXIT 4
  21.  
  22. typedef struct listOfReasons
  23. {
  24.     char* listName;
  25.     char** reasons;
  26.     int numberOfReasons;
  27. } listOfReasons;
  28.  
  29. void printReasons(listOfReasons reasons1, listOfReasons reasons2);
  30. void printSingleList(listOfReasons list);
  31. void myFgets(char str[], int n);
  32. void showOptions();
  33.  
  34.  
  35. int main(void)
  36. {
  37.     char* dillema = (char*)malloc(STR_LEN * sizeof(char)); //sizeof(char) is not needed but you understand it better
  38.     listOfReasons pros = { PROS_LIST_NAME, 0, 0 };
  39.     listOfReasons cons = { CONS_LIST_NAME, 0, 0 };
  40.     int option = 0;
  41.  
  42.     printf("What's your dillema?\n");
  43.     myFgets(dillema, STR_LEN);
  44.     printf("\n");
  45.     while (EXIT != option)
  46.     {
  47.         printf("Choose option:\n");
  48.         showOptions();
  49.         scanf("%d", &option);
  50.         getchar();
  51.         while (!(OPTIONS_MIN <= option) || !(OPTIONS_MAX >= option))
  52.         {
  53.             printf("Choose again:\n");
  54.             scanf("%d", &option);
  55.             getchar();
  56.         }
  57.         switch (option) //options from 1 to 4
  58.         {
  59.             case ADD_PRO_REASON:               
  60.                 printf("Enter a reason to add to list %s:\n", pros.listName);
  61.                 printf("Reason: ");
  62.                 pros.reasons[pros.numberOfReasons] = (char*)malloc(STR_LEN * sizeof(char));
  63.                 myFgets(pros.reasons[pros.numberOfReasons], STR_LEN);
  64.                 pros.reasons[pros.numberOfReasons] = realloc(pros.reasons[pros.numberOfReasons], strlen(pros.reasons[pros.numberOfReasons]));
  65.                 pros.reasons = realloc(pros.reasons, pros.numberOfReasons * sizeof(char*));
  66.                 pros.numberOfReasons++;
  67.                 break;
  68.             case ADD_CON_REASON:
  69.                 printf("Enter a reason to add to list %s:\n", cons.listName);
  70.                 printf("Reason: ");
  71.                 cons.reasons[cons.numberOfReasons] = (char*)malloc(STR_LEN * sizeof(char));
  72.                 myFgets(cons.reasons[cons.numberOfReasons], STR_LEN);
  73.                 cons.reasons[cons.numberOfReasons] = realloc(cons.reasons[cons.numberOfReasons], strlen(cons.reasons[cons.numberOfReasons]));
  74.                 cons.reasons = realloc(cons.reasons, cons.numberOfReasons * sizeof(char*));
  75.                 cons.numberOfReasons++;
  76.                 break;
  77.             case PRINT_REASONS:
  78.                 printReasons(pros, cons);
  79.                 break;
  80.             default: //will only be 4, no other options are allowed
  81.                 break;
  82.         }
  83.     }
  84.  
  85.     free(pros.reasons);
  86.     free(cons.reasons);
  87.     getchar();
  88.     return 0;
  89. }
  90.  
  91.  
  92. /*
  93. Function will perform the fgets command and also remove the newline
  94. that might be at the end of the string - a known issue with fgets.
  95. input: the buffer to read into, the number of chars to read
  96. */
  97. void myFgets(char str[], int n)
  98. {
  99.     fgets(str, n, stdin);
  100.     str[strcspn(str, "\n")] = 0;
  101. }
  102.  
  103.  
  104. /*
  105. Function will print the options to the user
  106. no i/o
  107. */
  108. void showOptions()
  109. {
  110.     printf("1 - Add PRO reason\n");
  111.     printf("2 - Add CON reason\n");
  112.     printf("3 - Print reasons\n");
  113.     printf("4 - Exit\n");
  114. }
  115.  
  116.  
  117. /*
  118. This function will print all the reasons in both lists
  119. input: struct pros, struct cons
  120. output: none
  121. */
  122. void printReasons(listOfReasons reasons1, listOfReasons reasons2) //paramenters are general
  123. {
  124.     printSingleList(reasons1);
  125.     printSingleList(reasons2);
  126. }
  127.  
  128.  
  129. /*
  130. Will print a single reason list using the struct
  131. */
  132. void printSingleList(listOfReasons list)
  133. {
  134.     int i = 0;
  135.     printf("List %s\n", list.listName);
  136.     for (i = 0; i < (int)(strlen("list") + strlen(list.listName) + 1); i++)
  137.     {//             this is to stop the warnings
  138.         printf("-");
  139.     }
  140.     printf("\n");
  141.     for (i = 0; i < list.numberOfReasons; i++)
  142.     {
  143.         puts(list.reasons[i]);
  144.     }
  145. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top