string manipulation problem

This is a discussion on string manipulation problem within the C Programming forums, part of the General Programming Boards category; Hi guys could someone suggest to me as to why it generates a duplicate customer ID number? For example at ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    string manipulation problem

    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;
    
    }

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    I don't understand your problem, the numbers you show are unique. Also, you might want to do something like:

    Code:
    sprintf(custID, "C%5.5u", countCust);
    can't remember exactly the way to do it off the top of my head, but do that instead of using all those if statements to format your numbers correctly.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    1. fgets(tmpSurname, SURNAME_MAX + 2, stdin);
    Stop lying about your buffer sizes - the rest of the program is broken.

    2. if(tmpSurname[strlen(tmpSurname) -1] != '\n');
    I wonder what that ; at the end of the line means.

    3. if(countCust >0 && countCust < 10)
    You can replace the next dozen lines or so with say
    sprintf(custID,"C%04u",countCust);

    4. Your functions are bloated rambles, try making several smaller functions which do specific tasks. Putting everything into a single function just leads to problems.
    For example, a function to prompt for input and return a buffer without a \n would be invaluable - that's about 20 lines of your code repeated several times over which could be extracted.

    > PostCode[POSTCODE_LEN] = atoi(tmpPostCodePtr);
    Huh?
    Why is this even an array of integers?

    > strcmp(curCust -> surname, newCust -> surname) <MIN_INPUT)
    What's MIN_INPUT?
    Is it zero?
    If it isn't, the code is wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    * hands some coffee to Salem *

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. string manipulation problem
    By csj561 in forum C++ Programming
    Replies: 3
    Last Post: 03-18-2005, 10:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21