Thread: Modify structure array help?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    31

    Modify structure array help?

    First...this is the question.
    Modify the program in Q6 to allow a user to edit the employee detail based on the Employee ID field. Display the result before and after edit.
    [Note: You should not allow the user to change the Employee ID.]
    __________________________________________________ _________
    My coding forQ6 below....

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
        typedef struct Employee {
        char ID[4];
        char name[20];
        char department[5];
        } A;
    
        int main(void){
    
            int i, found;
            char req_name[20];
    
            A employee[4] = {
                {"E01","Alice Chin", "R&D"},
                {"E02","John", "IT" },
                {"E03","Vivian", "HR" },
                {"E04","Alice Chin", "IT" }
            };
    
            printf("\nName of employee? ");
            gets(req_name);
    
            found = 0;
    
            for(i=0;i<20;i++){
    
                if(strcmp(employee[i].name, req_name) == 0) {
                    // we found it
                    found = 1;
                    break;
                }
            }
    
            if(found == 1)
            {
                printf("\nI found what you wanted!\n");
                printf("ID: %s\nName: %s\nDepartment: %s",employee[i].ID, employee[i].name, employee[i].department);
            }
    
            else
            {
                printf("Sorry, I couldn't find it.\n");
            }
    
            return 0;
    
        }
    __________________________________________________ ___

    Please teach me how to done this part? Very thanks you.

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Ok, I try to done this question, and below is my result. Did my coding correct?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
        typedef struct Employee {
        char ID[4];
        char name[20];
        char department[5];
        } A;
    
        int main(void){
    
            int i, found;
            char req_name[20];
            char input;
    
            A employee[4] = {
                {"E01","Alice Chin", "R&D"},
                {"E02","John", "IT" },
                {"E03","Vivian", "HR" },
                {"E04","Alice Chin", "IT" }
            };
    
            printf("Name of employee? ");
            gets(req_name);
    
            found = 0;
    
            for(i=0;i<20;i++){
    
                if(strcmp(employee[i].name, req_name) == 0) {
                    // we found it
                    found = 1;
                    break;
                }
            }
    
            if(found == 1)
            {
                printf("\nI found what you wanted!\n");
                printf("\nID: %s\nName: %s\nDepartment: %s",employee[i].ID, employee[i].name, employee[i].department);
                printf("\nDo you want to edit this emplooyee details?(Y/N): ");
                scanf("%c", &input);
    
                if(input == 'Y'){
    
                printf("Please edit employee name: ");
                scanf("%s", employee[i].name);
    
                printf("Please edit employee department: ");
                scanf("%s", employee[i].department);
    
                printf("\nID: %s\nNew Name: %s\nNew Department: %s",employee[i].ID, employee[i].name, employee[i].department);
    
                }
    
                else {
    
                    exit(0);
    
                }
            }
    
            else
            {
                printf("Sorry, I couldn't find it.\n");
            }
    
            return 0;
    
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. modify function pass it array
    By a.mlw.walker in forum C Programming
    Replies: 12
    Last Post: 08-01-2011, 04:03 AM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  4. array of structure
    By ashb in forum C++ Programming
    Replies: 11
    Last Post: 03-31-2005, 10:59 AM
  5. structure array
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-20-2002, 08:30 AM