Ok I need help with this structure its now been split into header and main, I have added a function which runs after adding a new customer which is for writing it to a txt file, the program builds fine but once it gets to the part where it needs to add to the file it stops running,
Header:
#ifndef unique_symbol
#define unique_symbol
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 80 ///< Maximum lenght of a name buffer
typedef struct
{
char name[MAX_NAME_LEN + 1];
int age;
char gender;
int storeID;
int loyaltyCardPoints;
} reg;
/// Pointer to global array of customers
reg *g_a_customers;
// Used to track number of records in the array
int g_num_customers;
/// Prototypes to go into header
void add_customer(reg *cust, FILE *);
void add_customer_file(reg *cust, FILE *);
void display_customer(reg *cust, FILE *);
void add_customer(reg *cust)
{
char gender[2];
char first[MAX_NAME_LEN + 1];
char last[MAX_NAME_LEN + 1];
int len;
printf("\nAdd New Customer:\n");
printf("First Name: ");
scanf("%80s", first);
len = strlen(first);
printf("Last Name: ");
scanf("%80s", last);
if ( len < MAX_NAME_LEN )
{
first[len] = ' ';
len++;
first[len] = 0;
}
strncpy(cust->name, first, len);
strncat(cust->name, last, MAX_NAME_LEN - len);
cust->name[MAX_NAME_LEN] = 0;
printf("Age: ");
scanf("%d", &cust->age);
if ( cust->age < 0 )
cust->age = 0;
printf("Gender: ");
scanf("%1s", &gender); ///< Take first letter to see if it is M or F
if ( gender[0] == 'm' || gender[0] == 'M' )
cust->gender = 'M';
else
cust->gender = 'F';
printf("Store ID: ");
scanf("%d", &cust->storeID);
if ( cust->storeID < 0 )
cust->storeID = 0;
printf("Loyalty Card Points: ");
scanf("%d", &cust->loyaltyCardPoints);
if ( cust->loyaltyCardPoints < 0 )
cust->loyaltyCardPoints = 0;
return;
}
void add_customer_file(reg *cust, FILE* customersDoc)
{
fprintf(customersDoc,"%80s\n %d\n %1s\n %d\n %d\n",
cust->name,
cust->age,
cust->gender,
cust->storeID,
cust->loyaltyCardPoints);
}
void display_customer(reg *cust, FILE* customersDoc)
{
printf(
"\nDetail for Customer: %.*s\n"
"--------------------\n"
" Age: %d\n"
" Gender: %s\n"
" Store ID: %d\n"
" Loyalty Pts: %d\n",
MAX_NAME_LEN, cust->name,
cust->age,
(cust->gender == 'M' ? "Male" : "Female"),
cust->storeID,
cust->loyaltyCardPoints);
return;
}
#endif
Main:
#include "Qheader.h"
int main()
{
FILE *customersDoc;
int count=0;
int i;
customersDoc = fopen("Customers.txt", "w");
/// Set the array to 0
memset(&g_a_customers, 0, sizeof(g_a_customers));
printf("How many customers to enter: ");
scanf("%d", &count);
g_a_customers = (reg*)calloc(count, sizeof(reg));
if (count < 1)
{
count = 0;
printf("Error, invalid number of customers!\n");
}
for ( i = 0; i < count; i++ )
add_customer(&g_a_customers[i]);
add_customer_file(&g_a_customers[i], customersDoc);
display_customer(&g_a_customers[i], customersDoc);
fclose(customersDoc);
return 0;
}
Eventually I want it to read the file and to recall the information from there rather then from the original input... any tips on this would also be greatly appreciated.
Thanks