Thread: alarm the user when the id already exist in staff system using c programming

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    48

    alarm the user when the id already exist in staff system using c programming

    Code:
    void addStaff()
    {
    
    
    	struct staff staa;
    	char ans;
    	FILE *fp;
    
    
    	fp = fopen("staff.dat", "ab");
    
    
    	printf("Add New staff (Y/N) : ");
    	rewind(stdin);
    	scanf(" %c", &ans);
    	
    
    
    	while (toupper(ans) == 'Y')
    	{
    		printf("Staff Details\n");
    		printf("Staff ID:\n");
    		scanf("%d", &staa.id);
    		rewind(stdin);
    		printf("Staff name:\n");
    		scanf("%s", &staa.name);
    		rewind(stdin);
    		printf("Postion:\n");
    		scanf("%s", &staa.position);
    		rewind(stdin);
    		printf("Salary");
    		scanf("%d", &staa.salary);
    		rewind(stdin);
    
    
    		fwrite(&staa, sizeof(staa), 1, fp);
    		printf("New staff details store successfully\n");
    		printf("Are you want to add new staff details?(Y/N)");
    		rewind(stdin);
    		scanf("%c", &ans);
    	}
    	fclose(fp);
    }
    i like to alert the user if they enter the id that is already exist.
    typedef struct staff {
    
    
    	int id, salary;
    	char name[30], position[30];
    
    
    }staff;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you intend to keep reading the file whenever you want to access the data, or have you considered reading the entire contents of the file to an array, and then work with that array, writing the array to file when necessary? Unless you make use of sophisticated methods to handle the data (or use an embedded database engine like SQLite, though that would be a big change to your program), the former is likely to be slow for accessing the data; the latter is potentially fast, especially if you sort the data, though it may be greatly limited by memory and may be slow to write everything out when you want to save.

    Quote Originally Posted by kkkcj
    i like to alert the user if they enter the id that is already exist.
    Before adding new data, you would search the data to see if the id to be added already exists.

    By the way, get rid of the rewind(stdin) calls. Generally, the standard input stream is not something you can seek on, so whatever you want to achieve should be done using more reliable means.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    i got it..i intent to to keep reading the file whenever you want to access the data
    and
    Code:
    
    
    Code:
    void modifyStaff() {
    	char ans, cont, name[25], position[20];
    	int i = 0, pCount, modiCount = 0, found, id;
    	int salary;
    	staff P[20];
    	FILE*fp;
    	fp = fopen("staff.dat", "rb");
    	while (fread(&P[i], sizeof(staff), 1, fp))
    		i++;
    	pCount = i;
    	fclose(fp);
    	do {
    		printf("\nEnter ID of the Staff to be modified : ");
    
    
    		rewind(stdin);
    		scanf("%d", &id);
    		found = 0;
    
    
    		printf("\nID    NAME   POSITION    SALARY \n");
    		printf("====== ======  ==========  ======= \n");
    		for (i = 0; i < pCount; i++) {
    			if (id == P[i].id)
    			{
    				found = 1;
    				printf("%-18d %-10s %-10s %-10d \n",
    					P[i].id, P[i].name, P[i].position, P[i].salary);
    				printf("\n Updated Name:");
    				rewind(stdin);
    				scanf("%[^\n]", &name);
    				printf("\n Updated Position:");
    				rewind(stdin);
    				scanf("%[^\n]", &position);
    				printf("\n Updated salary:");
    				rewind(stdin);
    				scanf("%d", &salary);
    				printf("Confirm to Modify (Y=yes)? ");
    				rewind(stdin);
    				scanf("%c", &ans);
    				if (toupper(ans) == 'Y') {
    					P[i].id = id;
    					strcpy(P[i].name, name);
    					strcpy(P[i].position, position);
    					P[i].salary = salary;
    					modiCount++;
    				}
    				printf("Updated Staff Records:\n");
    				printf("\nID NAME POSITION SALARY\n");
    				printf("========  ========= =========== ========\n");
    				printf("%-18d %-10s %-10s %-10d", P[i].id, P[i].name, P[i].position, P[i].salary);
    			}
    		}
    		if (!found)
    			printf("NO record founded with this ID\n");
    		printf("Any more record to modify?(Y=yes)?");
    		rewind(stdin);
    		scanf("%c", &cont);
    	} while (toupper(cont) == 'Y');
    	fp = fopen("staff.dat", "wb");
    	for (i = 0; i < pCount; i++)
    		fwrite(&P[i], sizeof(staff), 1, fp);
    	fclose(fp);
    	printf("\n\t%d Record(s) modified.\n\n", modiCount);
    }

    i would like to ask where can i put the parameter, i doing an assigment and the parameter is compulsory to add but i dunno where to add

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kkkcj
    i intent to to keep reading the file whenever you want to access the data
    In that case you should create a few functions to make this task easier. For example:
    Code:
    staff *findStaffById(staff *staffObj, int id) {
        int found = 0;
        FILE *fp = fopen("staff.dat", "rb");
        if (!fp) {
            return NULL;
        }
    
        while (fread(staffObj, sizeof(*staffObj), 1, fp)) {
            if (staffObj.id == id) {
                found = 1;
                break;
            }
        }
    
        fclose(fp);
    
        if (found) {
            return staffObj;
        } else {
            return NULL;
        }
    }
    Now you can call findStaffById in your modifyStaff function.

    Note that your modifyStaff function's approach to reading the staff objects is wrong: you cannot assume that there will be a maximum of 20 staff records. If you want to use a fixed size array, then your loop that reads from file must account for this. But if you are going to use an array, then why not use the other method, i.e., read once into the array instead of reading the file each time?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    which mean i should not use staff P[20]; right?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kkkcj
    which mean i should not use staff P[20]; right?
    Not quite. You can use that, but it means that you must change your loop such that it loops at most 20 times, otherwise your program is vulnerable to buffer overflow. Also, it means that you are assuming that there will be at most 20 staff records. Furthermore, if you are going to read all the records into an array anyway, why not do it once instead of each and every time you want to access the data?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    ohhh...i see and for
    Code:
    staff *findStaffById(staff *staffObj, int id) {
        int found = 0;
        FILE *fp = fopen("staff.dat", "rb");
        if (!fp) {
            return NULL;
        }
    
        while (fread(staffObj, sizeof(*staffObj), 1, fp)) {
            if (staffObj.id == id) {
                found = 1;
                break;
            }
        }
    
        fclose(fp);
    
        if (found) {
            return staffObj;
        } else {
            return NULL;
        }
    }
    compiler show error during this line
    Code:
    if(staffObj.id == id) {
    it say must have struct or union type

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yeah, you should change staffObj.id to staffObj->id because staffObj is a pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    after that it show red underline on == say excepted a field name sry for all the question...i really not good at programming

  10. #10
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    and i should put the findStaffbyid in the beginning of the modify code or last?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kkkcj
    after that it show red underline on == say excepted a field name
    Copy and paste the exact error or warning message. If it is an editor/IDE hint, then perhaps it is the hint that is wrong (e.g., due to an intellisense-type bug, or simply because the system hasn't updated its symbol tables yet)

    Quote Originally Posted by kkkcj
    and i should put the findStaffbyid in the beginning of the modify code or last?
    You would call it when you need to find the staff member by the id provided by the user.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    Severity Code Description Project File Line Suppression StateError C2059 syntax error: '==' Project25 c:\users\yi cian\documents\visual studio 2017\projects\project25\project25\source.c 167
    Error C2059 syntax error: '}' Project25 c:\users\yi cian\documents\visual studio 2017\projects\project25\project25\source.c 181
    Error C2059 syntax error: 'if' Project25 c:\users\yi cian\documents\visual studio 2017\projects\project25\project25\source.c 175
    Error C2059 syntax error: 'else' Project25 c:\users\yi cian\documents\visual studio 2017\projects\project25\project25\source.c 178

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating staff system
    By kkkcj in forum C Programming
    Replies: 18
    Last Post: 12-07-2017, 11:01 AM
  2. C Programming a board w/ LCD alarm clock
    By kingpanther in forum C Programming
    Replies: 1
    Last Post: 07-25-2016, 10:48 AM
  3. WSF-Staff
    By Elkvis in forum Tech Board
    Replies: 0
    Last Post: 06-19-2014, 09:17 AM
  4. Mini Alarm System
    By peckitt99 in forum Windows Programming
    Replies: 4
    Last Post: 04-23-2008, 12:08 AM
  5. Printing System time and 30 minute alarm
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-17-2006, 05:10 AM

Tags for this Thread