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

What do you mean by "stops running"? Also what function? I'm guessing add_customer_file(). Try using printf's where you think it "stops runing" If you don't see your message on the screen when you run it, the error is higher up. If you see your message, than the error is lower. Good luck.

it crashs. The just in time debugger of visual studio comes up and says:
"An unhandled win32 exception occurred in Loyalty.exe [11112]"

and yes it is the function add_customer_file(),

and I changed the fprintf to a printf but still get the same error but if I remove the function it all works fine... Really confused

and I changed the fprintf to a printf but still get the same error but if I remove the function it all works fine... Really confused

I'll look over your program more closely latter. I didn't say replace fprintf with printf :) I said use printf to debug your program. Look at this example (untested):
You can use printf() to figure out where the error is located pretty easily. Like:

#include <stdio.h>

int main(void) {

  printf("Value 1: %d\n", (5 * 5));
  printf("debug\n");  /* This is the debugging line. */
  fflush(stdout);
  printf("Value 2: %d\b", (0/0));

  printf("Press any key to continue...");
  fflush(stdout);
  return 0;
}

would print out something like:
Value 1: 25
debug
error: divide by zero
and:

#include <stdio.h>

int main(void) {

  printf("Value 1: %d\n", (5 * 5));
  printf("Value 2: %d\b", (0/0));
  printf("debug\n");  /* This is the debugging line. */
  fflush(stdout);

  printf("Press any key to continue...");
  fflush(stdout);
  return 0;
}

would print out:
Value 1: 25
error: divide by zero

So you know the line: printf("Value 2: %d\b", (0/0)); is the problem. It's one of my favourite way's to debug!

This article has been dead for over six months. Start a new discussion instead.