Thread: String shrinking and Autonumber

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    87

    String shrinking and Autonumber

    In a program I am currently creating, users are saved into a file. Is it possible to set it so that a user is automaticly given a membership number in the format XXX, but there has to be three numbers. So user 1 would be, 001.

    Also can you shrink a string. So if I have declared a string as string[30], but the user puts in a filename, such as file.txt, can I remove the .txt extension from the string.
    **********************
    *==================*
    * Many Can. One Must... *
    *==================*
    **********************

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953

    Re: String shrinking and Autonumber

    Originally posted by Necrodeemer
    In a program I am currently creating, users are saved into a file. Is it possible to set it so that a user is automaticly given a membership number in the format XXX, but there has to be three numbers. So user 1 would be, 001.

    Also can you shrink a string. So if I have declared a string as string[30], but the user puts in a filename, such as file.txt, can I remove the .txt extension from the string.
    For the second part you can make a new pointer where you store the name withought the .txt and then you delete the first pointer that has the name and the .txt

    This is what I understood for the first part, do you have anything about the second part?
    none...

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    keep track of last user number issued. Increment by one for each new one issued. Store user number as a string using itoa() or sprintf() or strstream or whatever. Determine string length. If length 1 append two zero's. If length is two append 1 zero.

    Code:
    int lastUserNumber = 0;
    char continue = 'y';
    int currentUserNumber;
    char sUserNumber[4];
    int length;
    char twoZeros[4] = "00";
    char oneZero[4] = "0";
    char tempStr[4];
    
    while(continue)
    {
       //new user number
       currentUserNumber = ++lastUserNumber;
       sprintf(sUserNumber, "%d", currentUserNumber);//or whatever
       length = strlen(sUserNumber);
       if(length == 1)
       {
          strcpy(tempStr, twoZeros);
          strcat(tempStr, sUserNumber);
          strcpy(sUserNumber, tempStr);
       }
       else if (length == 2)
         //similar for length == 2
    
       //then do something with sUserNumber
       cout << "this user number is" << sUserNumber << endl;
    
       cout << "to stop enter any key but lower case y" << endl;
       cin >> continue;
    }
    There are many ways to shrink a string. One way is to copy the contents of the original string into a second one, char by char, until you find the char you want to use as a terminating char.

    Code:
    char filename[20];
    char input[20];
    cout << "enter a filename with extension:  xxxxxx.xxx" 
    cin >> input;
    //copy input into filename until you find a period.
    int i;
    for(i = 0; i < strlen(input); ++i)
    {
      if(input[i] = '.')
      {
         filename[i] = '\0';
         break;
       }
      else
         filename[i] = input[i];
    }

Popular pages Recent additions subscribe to a feed