Thread: plz,need some help

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    3

    plz,need some help

    hey,i need help here....i need codes on how to auto generate id ...

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include "staffDetails.h"
    
    using namespace std;
    
    
    void staffDetails::getStaffInfo()
    {
        char yes;
    	
    	
    		cout<<"Please enter staff's name:";
    		cin>>name>>endl;
    		cout<<"Please enter staff's address:";
    		cin>>address>>endl;
    		cout<<"Please enter staff's ID number:";
    		cin>>SID>>endl;
    	
    		cout<<"Please enter staff's c number:";
    		cin>>c_no>>endl;
    
    	save();
    
    }
    
    void staffDetails::save()
    {
    		fstream instaff;
    	instaff.open("staff.dat",ios::in|ios::app);
    	if(instaff.fail())
    	{
    		cerr<<"File opening error!";
    		exit(-1);
    	}
    
    	  instaff>>name>>address>>SID>>c_no;
    	instaff.close();
    }
    >

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    staff member id = number_of_staff_members + 1

    something like that?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    What do you want the ID to look like? All numbers, a mix of letters and numbers? Do you do want their realname to be involved somehow (ie contains their initials)?

    Look into <ctime> and the rand() function, that should put you on the right track.
    Sent from my iPadŽ

  4. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    3

    thanx

    yea..maybe a mix of characters and numbers would be good
    for example,
    id=csd001

    i should use a for loop rite?

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    With the letters you're going to have to use rand() to get their ascii values then convert them to characters. Go to http://www.lookuptables.com they have an ascii chart. If you don't want it random, consider that the letters could be the employees initials.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    3

    hey

    what if i want the id to be in numbers only?

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Again, what numbers do you want? Random numbers? If so then use the rand() function. If you want it based on other data, such as the number of employees you have then you can use that.
    Sent from my iPadŽ

  9. #9
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    numbers 0-9 = ascii 48-57

    so use:

    Code:
    int blaat = rand() % (57 - 48) +48;
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    No, no. You don't need ascii values for numbers. Because rand returns an integer.

    Code:
    #include <cstdlib>
    #include <ctime>
    /* cstdlib for srand() and rand() and ctime to seed srand() */
    using namespace std;
    
    int main() {
       srand(time(NULL));
    
       int randomNum = rand() % 9 + 1;   // A random number between 1 and 9.
       
       return 0;
    }
    Last edited by SlyMaelstrom; 02-23-2006 at 03:12 PM.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed