Hi guys could someone suggest to me as to why it generates a duplicate customer ID number? For example at the moment it prints
c0001
c0002
c0003
c0002
It generates a number but not a unique one
Code:
/****************************************************************************
* Menu option #1: Add Customer
* Allows the user to add a new customer record to the linked list.
****************************************************************************/
void addCustomer(TennisStoreType* ts)
{
/* declaration of variables*/
char tmpSurname[SURNAME_MAX + 1];
char tmpFirstName[FIRSTNAME_MAX + 1];
char tmpAddress[ADDRESS_MAX + 1];
char tmpSuburb[SUBURB_MAX + 1];
char tmpPostCodePtr[10];
char tmpPhoneNumPtr[10];
char custID[CUSTID_LEN];
int PostCode[POSTCODE_LEN + 1];
int PhoneNum[PHONENUM_LEN + 1];
int finished = FAILURE;
int countCust;
CustomerNodeType* curCust = ts -> headCust;
CustomerNodeType* newCust;
CustomerNodeType* prevCust;
do
{
printf("Surname: (1-12 characters)\n");
fgets(tmpSurname, SURNAME_MAX + 2, stdin);
/* check the range of surname */
if(tmpSurname[strlen(tmpSurname) -1] != '\n');
{
printf("Surname too long less that 12\n\n");
}
/* fails if empty line is pressed*/
if(tmpSurname[0] == '\n')
{
printf("\t\n*****back to main menu*****");
finished = FAILURE;
}
}while(tmpSurname[strlen(tmpSurname)-1] != '\n');
tmpSurname[strlen(tmpSurname)-1] = '\0';
while(validCustSurname(ts, tmpSurname) == FAILURE)
{
printf("Customer exists already. Try again!\n\n");
do
{
printf("Enter a customer surname (1-12)");
fgets(tmpSurname, SURNAME_MAX + 2, stdin);
/* check if surname is longer then the constant variable*/
if(tmpSurname[strlen(tmpSurname)-1] != '\n')
{
printf("Customer surname too long! Has to be less than 12\n\n");
readRestOfLine();
}
/* stop processing if empty line is entered*/
if(tmpSurname[0] == '\n')
{
printf("***Returning to the main menu***");
finished = FAILURE;
}
}while(tmpSurname[strlen(tmpSurname)-1] != '\n');
tmpSurname[strlen(tmpSurname)-1] = '\0';
}
/********** input for first name**********/
do
{
printf("First Name: (1-12 characters)\n");
fgets(tmpFirstName, FIRSTNAME_MAX + 2, stdin);
/* check the range of surname */
if(tmpFirstName[strlen(tmpFirstName) -1] != '\n');
{
printf("First Name too long less that 12\n\n");
}
/* fails if empty line is pressed*/
if(tmpFirstName[0] == '\n')
{
printf("\t\n*****back to main menu*****");
finished = FAILURE;
}
}while(tmpFirstName[strlen(tmpFirstName)-1] != '\n');
tmpFirstName[strlen(tmpFirstName)-1] = '\0';
/********* enter Address *********/
do
{
printf("Address: (1-20 characters)\n");
fgets(tmpAddress, ADDRESS_MAX + 2, stdin);
/* check the range of surname */
if(tmpAddress[strlen(tmpAddress) -1] != '\n');
{
printf("Address too long less that 20\n\n");
}
/* fails if empty line is pressed*/
if(tmpAddress[0] == '\n')
{
printf("\t\n*****back to main menu*****");
finished = FAILURE;
}
}while(tmpAddress[strlen(tmpAddress)-1] != '\n');
tmpAddress[strlen(tmpAddress)-1] = '\0';
/************ suburb ************/
do
{
printf("Suburb: (1-12 characters)\n");
fgets(tmpSuburb, SUBURB_MAX + 2, stdin);
/* check the range of suburb */
if(tmpSuburb[strlen(tmpSuburb) -1] != '\n');
{
printf("Suburb too long less that 12\n\n");
}
/* fails if empty line is pressed*/
if(tmpSuburb[0] == '\n')
{
printf("\t\n*****back to main menu*****");
finished = FAILURE;
}
}while(tmpSuburb[strlen(tmpSuburb)-1] != '\n');
tmpSuburb[strlen(tmpSuburb)-1] = '\0';
/*********PostCode*********/
do
{
printf("PostCode: (4 - digits)\n");
fgets(tmpPostCodePtr, POSTCODE_LEN + 2, stdin);
/* fails if empty line is pressed*/
if(tmpPostCodePtr[0] == '\n')
{
printf("\t\n*****back to main menu*****");
finished = FAILURE;
}
/* check the range of postcode */
if(tmpPostCodePtr[strlen(tmpPostCodePtr) -1] != '\n')
{
printf("PostCode too long 4 digits\n\n");
readRestOfLine();
}
else
{
tmpPostCodePtr[strlen(tmpPostCodePtr) -1] = '\0';
finished = SUCCESS;
}
PostCode[POSTCODE_LEN] = atoi(tmpPostCodePtr);
}while(finished == FAILURE);
/*********this is for the phone number**********/
do
{
printf("PhoneNumber: (8 - digits)\n");
fgets(tmpPhoneNumPtr, PHONENUM_LEN + 2, stdin);
/* fails if empty line is pressed*/
if(tmpPhoneNumPtr[0] == '\n')
{
printf("\t\n*****back to main menu*****");
finished = FAILURE;
}
/* check the range of postcode */
if(tmpPhoneNumPtr[strlen(tmpPhoneNumPtr) -1] != '\n')
{
printf("Failed Enter 8 digits for phone number!\n\n");
readRestOfLine();
}
else
{
tmpPhoneNumPtr[strlen(tmpPhoneNumPtr) -1] = '\0';
finished = SUCCESS;
}
/* casting the temporary char* to an int PhoneNum variable*/
PhoneNum[PHONENUM_LEN] = atoi(tmpPhoneNumPtr);
}while(finished == FAILURE);
/* clear input buffer*/
readRestOfLine();
/******* allocating memory for customer*******/
if((newCust = malloc(sizeof(CustomerNodeType)))==NULL)
{
fprintf(stderr, "Not enough memory or currupt!");
finished = FAILURE;
}
/* This copies the temp variables to the proper variables*/
strcpy(newCust -> surname, tmpSurname);
strcpy(newCust -> firstName, tmpFirstName);
strcpy(newCust -> address, tmpAddress);
strcpy(newCust -> suburb, tmpSuburb);
newCust -> postCode = PostCode[POSTCODE_LEN];
newCust -> phoneNum = PhoneNum[PHONENUM_LEN];
/* the next customer is set to NULL*/
newCust -> nextCust = NULL;
/* adding the customers details to the linked list*/
curCust = ts -> headCust;
prevCust = NULL;
/* sort the linked list by type customer and by surname field*/
while(curCust != NULL && strcmp(curCust -> surname, newCust ->
surname) <MIN_INPUT)
{
prevCust = curCust;
curCust = curCust -> nextCust;
}
/* count the customers*/
ts -> customerCount++;
printf("Total Details: %d",ts -> customerCount);
if(prevCust == NULL)
{
ts -> headCust = newCust;
}
else
{
prevCust -> nextCust = newCust;
}
/*** generating the customer ID ***/
countCust = ts -> stockCount ++;
if(countCust >0 && countCust < 10)
{
sprintf(custID,"C000%u",countCust);
}
else if(countCust > 9 && countCust < 100)
{
sprintf(custID,"C00%u",countCust);
}
else if(countCust > 99 && countCust < 1000)
{
sprintf(custID,"C0%u",countCust);
}
else if(countCust > 999 && countCust < 10000)
{
sprintf(custID,"C%u",countCust);
}
else
{
printf("\nRecords are full for Stock input :)\n");
return;
}
strcpy(newCust -> custID, custID);
finished = SUCCESS;
}