Thread: Help with creating a string from other strings/integers

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    8

    Help with creating a string from other strings/integers

    I am writing a program that will assign a unique code to a given requestor. The format will be *issuer initials (3 characters)* - *random 4-digit integer*-*date (format is last digit of year + 3-digit day of year (i.e.: 5 January 2020 = 00005))

    I can create this to output to my screen using snprintf, but I need to assign it to a structure it (char).

    Here is my code so far:

    Code:
    printf("Please enter the technician intials: ");
    fgets(techInit, sizeof(techInit), stdin);
    removeNL(techInit); // this is a function to remove the new line from the input
    
    ppr = rand() % 10000;
    
    snprintf(buf, 50, "%s-%d-%d", techInit, ppr, Julian);
    printf("|%s|\n", buf); // this is just a test printf to check if it combined everything properly
    I'm trying to assign the "buf" to a struct item (char) called "new->pprNum"

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What do you mean "assign to a char"?

    A char isn't going to hold a string.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    8
    I have created a struct that has a record defined as "char pprNum"

    This is meant to hold the approval code for the application request (I'm creating a request form). The information inside of it will be a combination of [3 letters "dash" 4 integers "dash" 4 integers]. Would there be some other variable type that would be more appropriate (still learning)?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you need to create structure member called
    char pprNum[50];

    Then you can simply do
    snprintf(new->pprNum, 50, "%s-%d-%d", techInit, ppr, Julian);

    > The format will be *issuer initials (3 characters)* - *random 4-digit integer*-*date (format is last digit of year + 3-digit day of year (i.e.: 5 January 2020 = 00005))
    If you want exactly 4 and 5 digit strings with leading zeros, then use "%04d" and "%05d" as the formats.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2020
    Posts
    8
    That worked! I guess I was just staring at the screen too long to try and look at it from a different perspective.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-13-2017, 01:29 AM
  2. Hot to convert Strings to Integers
    By brian_90 in forum C Programming
    Replies: 4
    Last Post: 04-08-2011, 07:32 PM
  3. Strings as integers
    By unknown_ in forum C Programming
    Replies: 3
    Last Post: 03-19-2010, 09:40 PM
  4. Converting Integers to Strings
    By momo97 in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2009, 01:36 AM
  5. Replies: 9
    Last Post: 03-17-2006, 12:44 PM

Tags for this Thread