Thread: error about strcpy and strcmp

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

    error about strcpy and strcmp

    Code:
    void modifyStaff() {
        char ans, cont, staid[20], name[25], position[20];
        int i = 0, pCount, modiCount = 0,salary,found;
        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("%s", staid);
            found = 0;
    
    
            printf("\nID    NAME   POSITION    SALARY \n");
            printf("============   ===========    ======= \n");
            for (i = 0; i < pCount; i++) {
                if (strcmp(staid, P[i].id) == 0) {
                    found = 1;
                    printf("%-18d %-10s %-10s %-10d \n",
                        P[i].id, P[i].name, P[i].salary, P[i].salary);
                    printf("\n Updated Name:");
                    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 = staid;
                        strcpy(P[i].name, name);
                        strcpy(P[i].position, position);
                        strcpy(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 ID =%d\n", staid);
            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++)
            fwite(&P[i], sizeof(staff), 1, fp);
        fclose(fp);
        printf("\n\t%d Record(s) modified.\n\n", modiCount);
    }
    the compiler show bunch of error during the strcpy and strcmp function and i don't understand why

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Probably because the struct's name, position, salary and id aren't character arrays.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    Code:
    typedef struct staff {
    	
    	int id,salary;
    	char name[30], position[30];
    
    
    }staff;

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Ok then, as you see, id and salary aren't character arrays. Instead of strcmp and strcpy use == and = on them.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    thank u very muchhh....there another question..it will only execute until the line 21...it wont execute the remain row

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Have you converted staid( which is a char array ) to an int? If you haven't, do so, and then compare the converted value with P[i].id

    You can of course scrap "staid" completely and use an int, say "id", with scanf like this:
    Code:
    scanf("%d", &id);
    Also, I said it before and I say it again: Stop using rewind with stdin.
    Last edited by GReaper; 12-17-2017 at 04:36 AM.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    Code:
    void modifyStaff() {
    	char ans, cont, name[25], position[20];
    	int i = 0, pCount, modiCount = 0, found, id[20];
    	int salary[20];
    	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 : ");
    
    
    		scanf("%d",id);
    		found = 0;
    
    
    		printf("\nID    NAME   POSITION    SALARY \n");
    		printf("============   ===========    ======= \n");
    		for (i = 0; i < pCount; i++) {
    			if (id==P[i].id == 0) {
    				found = 1;
    				printf("%-18d %-10s %-10s %-10d \n",
    					P[i].id, P[i].name, P[i].position, P[i].salary);
    				printf("\n Updated Name:");
    				scanf("%[^\n]", name);
    				printf("\n Updated Position:");
    			
    				scanf("%[^\n]", position);
    				printf("\n Updated salary:");
    				
    				scanf("%d",salary);
    				printf("Confirm to Modify (Y=yes)? ");
    				
    				scanf("%c", &ans);
    				if (toupper(ans) == 'Y') {
    					P[i].id =id;
    					strcmp(P[i].name, name);
    					strcmp(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" );
    		printf("Any more record to modify?(Y=yes)?");
    		
    		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);
    }
    it's work but when i modify the updated name and position will display together..and the cannot store the salary correctly....for example i modify the salary to become 9000 but it will show 12347487

  8. #8
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    and it will not change the original data..for example i modify the name kkk to become yyy, it still show the kkk instead of yyy

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by kkkcj View Post
    and it will not change the original data..for example i modify the name kkk to become yyy, it still show the kkk instead of yyy
    You should be using strcpy and not strcmp to update the name and position (lines 40 and 41)

  10. #10
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    which mean i should use =?

  11. #11
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    i misunderstand it...i get it now...is the salary...

  12. #12
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    Code:
    void modifyStaff() {
    	char ans, cont, name[25], position[20];
    	int i = 0, pCount, modiCount = 0, found, id[20];
    	int salary[20];
    	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 == 0) {
    				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);
    					strcpy(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");
    		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 change my code...but it does't work for the salary part...it display error access of violation

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    1. You forgot an &
    2. Bizarrely, salary is an array of ints, not a single int.
    3. All those rewind(stdin) are not doing anything for you.

    > strcpy(P[i].salary, salary);
    It only works with CHAR arrays.
    Making salary an array of int doesn't make it something you can pass to strcpy.
    Use P[i].salary = salary;
    When you've fixed it to not be an array.

    > if (id == P[i].id == 0)
    What are you trying to do here?
    You've written this
    if (id == ( P[i].id == 0 ) )
    The inner ( ) expression has a value of either 0 or 1.
    So this if statement will only be true if id is 0 or 1.
    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.

  14. #14
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    Code:
    typedefstruct staff {
    
    int id, salary;
    char name[30], position[30];
    }staff;
    
    void modifyStaff(){
    char ans, cont, name[25], position[20];
    int i =0, pCount, modiCount =0, found, id[20];
    int salary[20];
    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 ==0){
                found =1;
                printf("%-18d %-10s %-10s %-10d \n",
                    P[i].id, P[i].name, P[i].position, P[i].salary);
                printf("\n Updated Name:");
    
                scanf("%[^\n]",&name);
                printf("\n Updated Position:");
    
                scanf("%[^\n]",&position);
                printf("\n Updated salary:");
    
                scanf("%d",&salary);
                printf("Confirm to Modify (Y=yes)? ");
    
                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");
        printf("Any more record to modify?(Y=yes)?");
    
        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 change the code as below...the salary cannot modify successfully ..for example i modify the salary form 600 become 700, the results will be 1767752

  15. #15
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    Code:
    if(id == P[i].id ==0){
                found =1;
    this is mean if the id user enter same as id store in system. print all the details and another question is the compiler will display the details even though the user user enter the wrong ID
    Last edited by kkkcj; 12-17-2017 at 07:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp and strcpy error
    By behzad_shabani in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2008, 04:15 PM
  2. help with #define, strcmp, and strcpy
    By doc_watson2007 in forum C Programming
    Replies: 8
    Last Post: 12-20-2004, 10:50 PM
  3. strcpy()&strcmp()
    By mr alison in forum C Programming
    Replies: 5
    Last Post: 10-10-2002, 02:26 PM
  4. strcpy & strcmp, what is wrong?
    By Silverdream in forum C Programming
    Replies: 7
    Last Post: 02-13-2002, 03:36 PM

Tags for this Thread