string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • selam
    New Member
    • Nov 2006
    • 16

    string

    Hi,i am currently taking c++ and i'm having a problem with the assignment.The assignment is when a user enter the first name of an employee, the id is created by taking the first character of the first name and followed by number.
    Does anyone have any ideas?
  • sivadhas2006
    New Member
    • Nov 2006
    • 142

    #2
    Hi,

    This may satisfy your need.

    Code:
    #include <iostream.h>
    #include <time.h>
    #include <stdlib.h>
    
    int main()
    {
       char
          szEmpName[30],
          szEmpID[35];
    
       // get the employee name.
       cout << "Enter the employee name :";
       cin >> szEmpName;
    
       /* Seed the random-number generator with current time so that
        * the numbers will be different every time we run.
        */
       srand( (unsigned)time( NULL ) );
    
       // create a employee id.
       sprintf(szEmpID, "%c%1d", szEmpName[0], rand());
    
       // print the employee id.
       cout << "The employee id is :" << szEmpID;
    
       return 0;
    }
    Regards,
    M.Sivadhas.

    Comment

    • selam
      New Member
      • Nov 2006
      • 16

      #3
      Originally posted by sivadhas2006
      Hi,

      This may satisfy your need.

      Code:
      #include <iostream.h>
      #include <time.h>
      #include <stdlib.h>
      
      int main()
      {
         char
            szEmpName[30],
            szEmpID[35];
      
         // get the employee name.
         cout << "Enter the employee name :";
         cin >> szEmpName;
      
         /* Seed the random-number generator with current time so that
          * the numbers will be different every time we run.
          */
         srand( (unsigned)time( NULL ) );
      
         // create a employee id.
         sprintf(szEmpID, "%c%1d", szEmpName[0], rand());
      
         // print the employee id.
         cout << "The employee id is :" << szEmpID;
      
         return 0;
      }
      Regards,
      M.Sivadhas.
      It is really greate code.But there is one problem.How can i write it as a function.What values does it accept from the main function.And the id is not generated randomly the user suppose to enter the id like 001 and like that and also when the user enter the id it checks wheather he is the first one in that alphabet or not.

      Comment

      • sivadhas2006
        New Member
        • Nov 2006
        • 142

        #4
        Hi,

        I think to do that you need to store the user name and id in the file right?

        Regards,
        M.Sivadhas.

        Comment

        Working...