Stage 1 initial (w/ memory leaks) in test.c

This commit is contained in:
Rory Healy 2020-05-23 23:19:05 +10:00
parent f8227cacb0
commit aacbd56e5c
Signed by: roryhealy
GPG key ID: 0A3CBDE9C2AE672F
2 changed files with 97 additions and 8 deletions

View file

@ -34,6 +34,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
/* constants */ /* constants */
#define STAGE_FORMATTING_STRING "=========================" #define STAGE_FORMATTING_STRING "========================="
@ -77,14 +78,6 @@ int main(int argc, char *argv[]) {
/* holds all the credit cards */ /* holds all the credit cards */
credit_card_t all_cards[MAX_NUM_CARDS]; credit_card_t all_cards[MAX_NUM_CARDS];
/* holds all the transactions */
transaction_t all_transactions[MAX_NUM_CARDS];
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* Dynamic memory allocation required as
* size of all_transactions isn't known
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
/* stage 1: reading one credit card record */ /* stage 1: reading one credit card record */
stage_one(); stage_one();

96
src/test.c Normal file
View file

@ -0,0 +1,96 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define CREDIT_CARD_ID_LEN 8
/* structure typedefs */
typedef struct {
char card_id[CREDIT_CARD_ID_LEN];
int daily_lim;
int trans_lim;
} credit_card_t;
typedef struct {
char* str;
} words_t;
/* function prototypes */
void stage_one(credit_card_t* all_cards, int* num_cards);
void read_one_card(credit_card_t* one_card);
int main(int argc, char *argv[]) {
/* all_cards will store all credit card information */
credit_card_t* all_cards = (credit_card_t*)malloc(sizeof(credit_card_t));
int num_cards = 0;
stage_one(all_cards, &num_cards);
free(all_cards);
all_cards = NULL;
return 0;
}
void stage_one(credit_card_t* all_cards, int* num_cards) {
printf("=========================Stage 1=========================\n");
credit_card_t curr_card;
read_one_card(&curr_card);
printf("Card ID: %s\n", curr_card.card_id);
printf("Daily limit: %d\n", curr_card.daily_lim);
printf("Transaction limit: %d\n", curr_card.trans_lim);
printf("\n");
}
/* read a line of input credit cards into one_card */
void
read_one_card(credit_card_t* one_card) {
int i = 0, c;
/* records the length of the current field */
int index = 1;
char card_id_str[CREDIT_CARD_ID_LEN];
char* daily_lim_str = (char*)malloc(index * sizeof(char) + 1);
char* trans_lim_str = (char*)malloc(index * sizeof(char) + 1);
assert(daily_lim_str != NULL && trans_lim_str != NULL);
/* read the card ID from the input line */
while (((c = getchar()) != EOF) && (c != '\n') && (c != '\r')) {
if (i < CREDIT_CARD_ID_LEN) {
card_id_str[i++] = c;
index += 1;
} else {
break;
}
}
card_id_str[i] = '\0';
/* copy the card id to one_card */
strncpy(one_card->card_id, card_id_str, CREDIT_CARD_ID_LEN);
one_card->card_id[CREDIT_CARD_ID_LEN] = '\0';
/* read the daily limit from the input line */
i = 0;
while (((c = getchar()) != EOF) && (c != '\n') && (c != '\r')) {
if (c != ' ') {
daily_lim_str[i++] = c;
index += 1;
} else {
break;
}
}
daily_lim_str[i] = '\0';
one_card->daily_lim = atoi(daily_lim_str);
/* read the transaction limit from the input line */
i = 0;
while (((c = getchar()) != EOF) && (c != '\n') && (c != '\r')) {
trans_lim_str[i++] = c;
index += 1;
}
trans_lim_str[i] = '\0';
one_card->trans_lim = atoi(trans_lim_str);
free(daily_lim_str);
free(trans_lim_str);
daily_lim_str = trans_lim_str = NULL;
}