From e6e218f3c442f366f5588a3bd4557d36b58c6bac Mon Sep 17 00:00:00 2001 From: Win Date: Sun, 1 Sep 2024 18:37:50 +0700 Subject: [PATCH] added comments and todos --- vendorInfo.c | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/vendorInfo.c b/vendorInfo.c index 54333aa..0e30775 100644 --- a/vendorInfo.c +++ b/vendorInfo.c @@ -47,10 +47,12 @@ int main() while ((fgets(file_buffer, 50, fp) != NULL) && (index <= MAX_VENDORS)) { + /* TO-DO: VALIDATE FILE DATA */ char *token = strtok(file_buffer, " "); strncpy(vendors[index].vendor_name, token, strlen(token) + 1); token = strtok(NULL, " "); + /* TO-DO: VALIDATE MERCHANDISE BY PUTTING EVERYTHING AS LOWERCASE*/ strncpy(vendors[index].merchandise, token, strlen(token) + 1); token = strtok(NULL, " "); @@ -82,26 +84,31 @@ int main() printf("%-50s %s\n", vendor_info, vendor_stock); } - - char user_purchase[50]; - char query[100]; - read_input(query, 100); + char user_query[100]; + read_input(user_query, 100); - int query_length = strlen(query); - - for (int i = 0; i < query_length; i++) + // convert string to lowercase - each character one by one + int user_query_length = strlen(user_query); + for (int i = 0; i < user_query_length; i++) { - query[i] = tolower(query[i]); + user_query[i] = tolower(user_query[i]); }; - search(query, vendors, total_vendors); + search(user_query, vendors, total_vendors); fclose(fp); free(file_buffer); return 0; }; +/* + custom read input function + str = string itself + n = max amount of characters that the function is able to read into buffer + + reads one character at a time into buffer and adds a null terminator when finish reading +*/ int read_input(char str[], int n) { int ch, i = 0; @@ -118,6 +125,14 @@ int read_input(char str[], int n) return i; }; +/* + Search function + + Parameters: + - query: search query + - vendors: the vendor table + - total_vendors: the amount of vendors +*/ int search(char query[], struct vendor_item vendors[MAX_VENDORS], int total_vendors) { int i; @@ -142,7 +157,15 @@ int search(char query[], struct vendor_item vendors[MAX_VENDORS], int total_vend return 0; }; -int purchase(struct vendor_item vendors[MAX_VENDORS], char str[], int amount) +/* + Purchase function + + Parameters: + - item: The item that the user is purchasing + - amount: How many items that the user is purchasing + - vendors: vendor table +*/ +int purchase(char item[], int amount, struct vendor_item vendors[MAX_VENDORS]) { };