Thread: Auto generated ID

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    Auto generated ID

    How to code a program segment that can auto generate a list of ID from S0001 until S0010?

  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
    Code:
    int nextid ( ) {
      static int last = 0;
      return ++last;
    }
    Each time you call it, you get the next number.

    Formatting as "Snnnn" is an easy exercise for the reader.
    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
    Jun 2012
    Posts
    127
    How to add zero between S and integer?

    So I can get the same result as below:

    S001
    S002
    S003
    S004
    S005
    S006
    S007
    S008
    S009
    S010

    Besides, take out 1 zero when reach 10.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Use printf with a format specifer that says to format the number with a certain number of digits. Refer to the documentation for printf.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. generated assembly
    By KIBO in forum C++ Programming
    Replies: 3
    Last Post: 10-05-2010, 01:21 PM
  2. [VS 2005] Auto generated skeleton code
    By cboard_member in forum Tech Board
    Replies: 4
    Last Post: 03-06-2006, 12:45 PM
  3. i want my output to be generated
    By coolshyam in forum C Programming
    Replies: 3
    Last Post: 03-19-2005, 12:53 AM
  4. Auto generated
    By Daniel decosta in forum C Programming
    Replies: 11
    Last Post: 09-28-2002, 05:35 PM
  5. auto <cringe> generated <cringe> code <cringe>
    By RobS in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-22-2001, 04:46 AM