Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************
- * Class: MAGSHIMIM C2 *
- * Week: *
- * Name: *
- * Credits: *
- **********************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define ADD_PRO_REASON 1
- #define ADD_CON_REASON 2
- #define PROS_LIST_NAME "PRO"
- #define CONS_LIST_NAME "CON"
- #define PRINT_REASONS 3
- #define OPTIONS_MIN 1
- #define OPTIONS_MAX 4
- #define STR_LEN 75
- #define EXIT 4
- typedef struct listOfReasons
- {
- char* listName;
- char** reasons;
- int numberOfReasons;
- } listOfReasons;
- void printReasons(listOfReasons reasons1, listOfReasons reasons2);
- void printSingleList(listOfReasons list);
- void myFgets(char str[], int n);
- void showOptions();
- int main(void)
- {
- char* dillema = (char*)malloc(STR_LEN * sizeof(char)); //sizeof(char) is not needed but you understand it better
- listOfReasons pros = { PROS_LIST_NAME, 0, 0 };
- listOfReasons cons = { CONS_LIST_NAME, 0, 0 };
- int option = 0;
- printf("What's your dillema?\n");
- myFgets(dillema, STR_LEN);
- printf("\n");
- while (EXIT != option)
- {
- printf("Choose option:\n");
- showOptions();
- scanf("%d", &option);
- getchar();
- while (!(OPTIONS_MIN <= option) || !(OPTIONS_MAX >= option))
- {
- printf("Choose again:\n");
- scanf("%d", &option);
- getchar();
- }
- switch (option) //options from 1 to 4
- {
- case ADD_PRO_REASON:
- printf("Enter a reason to add to list %s:\n", pros.listName);
- printf("Reason: ");
- pros.reasons[pros.numberOfReasons] = (char*)malloc(STR_LEN * sizeof(char));
- myFgets(pros.reasons[pros.numberOfReasons], STR_LEN);
- pros.reasons[pros.numberOfReasons] = realloc(pros.reasons[pros.numberOfReasons], strlen(pros.reasons[pros.numberOfReasons]));
- pros.reasons = realloc(pros.reasons, pros.numberOfReasons * sizeof(char*));
- pros.numberOfReasons++;
- break;
- case ADD_CON_REASON:
- printf("Enter a reason to add to list %s:\n", cons.listName);
- printf("Reason: ");
- cons.reasons[cons.numberOfReasons] = (char*)malloc(STR_LEN * sizeof(char));
- myFgets(cons.reasons[cons.numberOfReasons], STR_LEN);
- cons.reasons[cons.numberOfReasons] = realloc(cons.reasons[cons.numberOfReasons], strlen(cons.reasons[cons.numberOfReasons]));
- cons.reasons = realloc(cons.reasons, cons.numberOfReasons * sizeof(char*));
- cons.numberOfReasons++;
- break;
- case PRINT_REASONS:
- printReasons(pros, cons);
- break;
- default: //will only be 4, no other options are allowed
- break;
- }
- }
- free(pros.reasons);
- free(cons.reasons);
- getchar();
- return 0;
- }
- /*
- Function will perform the fgets command and also remove the newline
- that might be at the end of the string - a known issue with fgets.
- input: the buffer to read into, the number of chars to read
- */
- void myFgets(char str[], int n)
- {
- fgets(str, n, stdin);
- str[strcspn(str, "\n")] = 0;
- }
- /*
- Function will print the options to the user
- no i/o
- */
- void showOptions()
- {
- printf("1 - Add PRO reason\n");
- printf("2 - Add CON reason\n");
- printf("3 - Print reasons\n");
- printf("4 - Exit\n");
- }
- /*
- This function will print all the reasons in both lists
- input: struct pros, struct cons
- output: none
- */
- void printReasons(listOfReasons reasons1, listOfReasons reasons2) //paramenters are general
- {
- printSingleList(reasons1);
- printSingleList(reasons2);
- }
- /*
- Will print a single reason list using the struct
- */
- void printSingleList(listOfReasons list)
- {
- int i = 0;
- printf("List %s\n", list.listName);
- for (i = 0; i < (int)(strlen("list") + strlen(list.listName) + 1); i++)
- {// this is to stop the warnings
- printf("-");
- }
- printf("\n");
- for (i = 0; i < list.numberOfReasons; i++)
- {
- puts(list.reasons[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement