Thread: Trouble with structures,file input, output and prewritten string functions

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    20

    Trouble with structures,file input, output and prewritten string functions

    Need some direction on this project, I have this so far but dont know where to go from here, heres the input file im reading from.

    3Abdul Rachel Lopez Charlie Schofield Theo I will need to ask the user for the first and last names of the new employee.

    Then, you will need to check how many employees are on the old roster. For each employee, you will need to check to see if the NEW employee’s name should be printed before it. If so, print the new employee’s name, followed by the old employee. Otherwise, simply print the old employee’s name. This should repeat until all the names (new and old) have been printed to the new roster.The output file should contain each employee name (FIRST LAST) on a single line, sorted in alphabetical order.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    struct record{
        char fname[20];
        char lname[20];
    };
    
    
    int namecmp (struct record eA, struct record eB);
    
    
    int main() {
        FILE *ifp = fopen("input.txt", "r");
        FILE *ofp = fopen("roster.txt", "w");
        int num_people, i, flag=0;
        struct record new_employee, temp_employee;
    
    
        //INSERT CODE HERE
        //-Ask the user for the first and last names of the new employee
    
    
        //-Get the number of employees on the roster from the input file
    
    
        //-Set up a loop for the number of employees
        //-Each time you should do the following:
        //--Read in a name from the input file
        //--Use the provided namecmp function to see if the
        //      new employee should be printed first
        //--Print the old employee to the output file
    
    
        //-If the new employee was never printed, print at the end of the file
    
    
        fclose(ifp);
        fclose(ofp);
    
    
        return 0;
    }
    
    
    /* Pre-Conditions: This function takes in two structures of type record.
     * These are labeled eA and eB for employee A and employee B, respectively.
     * Post-Conditions: This function compares the names of the two
     * employee to see which should come first lexicographically.
     * The function returns -1 if eA comes first, -1 if eB comes first,
     * and 0 if the employees have the same name.
     */
    int namecmp (struct record eA, struct record eB) {
        if (strcmp(eA.lname, eB.lname) < 0)
            return -1;
        else if (strcmp(eA.lname, eB.lname) > 0)
            return 1;
        else {
            if (strcmp(eA.fname, eB.fname) < 0)
                return -1;
            else if (strcmp(eA.fname, eB.fname) > 0)
                return 1;
            else
                return 0;
        }
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Start with the easy parts first for instance look at the comments"//-Ask the user for the first and last names of the new employee//-Get the number of employees on the roster from the input file" These are simple scanf and fscanf functions.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    okay here is my new code, i left out something on the first tweo scanf because i dont know how i would save the new employee's name.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    struct record{
        char fname[20];
        char lname[20];
    };
    
    
    int namecmp (struct record eA, struct record eB);
    
    
    int main() {
        FILE *ifp = fopen("input.txt", "r");
        FILE *ofp = fopen("roster.txt", "w");
        int num_people, i, flag=0;
        struct record new_employee, temp_employee;
    
    
        printf("What is the first name of the new employee?\n");
        scanf("%s", );
       
        printf("What is the last name of the new employee?\n");
        scanf("%s", );
        
        fscanf("%d", num_people);
    
    
        //-Set up a loop for the number of employees
        //-Each time you should do the following:
        //--Read in a name from the input file
        //--Use the provided namecmp function to see if the
        //      new employee should be printed first
        //--Print the old employee to the output file
    
    
        //-If the new employee was never printed, print at the end of the file
    
    
        fclose(ifp);
        fclose(ofp);
    
    
        return 0;
    }
    
    
    /* Pre-Conditions: This function takes in two structures of type record.
     * These are labeled eA and eB for employee A and employee B, respectively.
     * Post-Conditions: This function compares the names of the two
     * employee to see which should come first lexicographically.
     * The function returns -1 if eA comes first, -1 if eB comes first,
     * and 0 if the employees have the same name.
     */
    int namecmp (struct record eA, struct record eB) {
        if (strcmp(eA.lname, eB.lname) < 0)
            return -1;
        else if (strcmp(eA.lname, eB.lname) > 0)
            return 1;
        else {
            if (strcmp(eA.fname, eB.fname) < 0)
                return -1;
            else if (strcmp(eA.fname, eB.fname) > 0)
                return 1;
            else
                return 0;
        }
    }

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    When you are referencing a struct member you use the dot operator. For example: new_employee.fname will access first name in new_employee struct.
    Look also at your fscanf call, you are missing the first argument, which is the FILE pointer.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    I believe its this way, hopefully i got it right??

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    struct record{
        char fname[20];
        char lname[20];
    };
    
    
    int namecmp (struct record eA, struct record eB);
    
    
    int main() {
        FILE *ifp = fopen("input.txt", "r");
        FILE *ofp = fopen("roster.txt", "w");
        int num_people, i, flag=0;
        struct record new_employee, temp_employee;
    
    
        printf("What is the first name of the new employee?\n");
        scanf("%s", .fname[20]);
        
        printf("What is the last name of the new employee?\n");
        scanf("%s", lname[20]);
        
        fscanf("%d", num_people);
    
    
        //-Set up a loop for the number of employees
        //-Each time you should do the following:
        //--Read in a name from the input file
        //--Use the provided namecmp function to see if the
        //      new employee should be printed first
        //--Print the old employee to the output file
    
    
        //-If the new employee was never printed, print at the end of the file
    
    
        fclose(ifp);
        fclose(ofp);
    
    
        return 0;
    }
    
    
    /* Pre-Conditions: This function takes in two structures of type record.
     * These are labeled eA and eB for employee A and employee B, respectively.
     * Post-Conditions: This function compares the names of the two
     * employee to see which should come first lexicographically.
     * The function returns -1 if eA comes first, -1 if eB comes first,
     * and 0 if the employees have the same name.
     */
    int namecmp (struct record eA, struct record eB) {
        if (strcmp(eA.lname, eB.lname) < 0)
            return -1;
        else if (strcmp(eA.lname, eB.lname) > 0)
            return 1;
        else {
            if (strcmp(eA.fname, eB.fname) < 0)
                return -1;
            else if (strcmp(eA.fname, eB.fname) > 0)
                return 1;
            else
                return 0;
        }
    }

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    i forgot a period .lname

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You also have to specify what struct you want. Do you want to scan in to new_employee or temp_employee?

    Ex: scanf("%s",new_employee.fname);

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    Ok i understand, I believe now that part should be okay.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    struct record{
        char fname[20];
        char lname[20];
    };
    
    
    int namecmp (struct record eA, struct record eB);
    
    
    int main() {
        FILE *ifp = fopen("input.txt", "r");
        FILE *ofp = fopen("roster.txt", "w");
        int num_people, i, flag=0;
        struct record new_employee, temp_employee;
    
    
        printf("What is the first name of the new employee?\n");
        scanf("%s", new_employee.fname[20]);
        
        printf("What is the last name of the new employee?\n");
        scanf("%s", new_employee.lname[20]);
        
        fscanf("%d", &num_people);
    
    
        //-Set up a loop for the number of employees
        //-Each time you should do the following:
        //--Read in a name from the input file
        //--Use the provided namecmp function to see if the
        //      new employee should be printed first
        //--Print the old employee to the output file
    
    
        //-If the new employee was never printed, print at the end of the file
    
    
        fclose(ifp);
        fclose(ofp);
    
    
        return 0;
    }
    
    
    /* Pre-Conditions: This function takes in two structures of type record.
     * These are labeled eA and eB for employee A and employee B, respectively.
     * Post-Conditions: This function compares the names of the two
     * employee to see which should come first lexicographically.
     * The function returns -1 if eA comes first, -1 if eB comes first,
     * and 0 if the employees have the same name.
     */
    int namecmp (struct record eA, struct record eB) {
        if (strcmp(eA.lname, eB.lname) < 0)
            return -1;
        else if (strcmp(eA.lname, eB.lname) > 0)
            return 1;
        else {
            if (strcmp(eA.fname, eB.fname) < 0)
                return -1;
            else if (strcmp(eA.fname, eB.fname) > 0)
                return 1;
            else
                return 0;
        }
    }

  9. #9
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Lose the subscript [20] in both fname and lname. Look at an example of fscanf, and tell me what looks different from your call to fscanf.

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    dumb mistakes its like this now fscanf(ifp, "%d", &num_people);

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    Now the next step seems very confusing to me how i would go about setting up this for loop, any ideas??

  12. #12
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Get a piece of paper and walk through what you need to do slowly. You already have the sring compare function written for you, all you really need is a for loop with a couple "if" statments.

  13. #13
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    Ok so I think this is how the for loop should go, i probably made some mistakes. thanks for your help

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    struct record{
        char fname[20];
        char lname[20];
    };
    
    
    int namecmp (struct record eA, struct record eB);
    
    
    int main() {
        FILE *ifp = fopen("input.txt", "r");
        FILE *ofp = fopen("roster.txt", "w");
        int num_people, i, flag=0;
        struct record new_employee, temp_employee;
    
    
        printf("What is the first name of the new employee?\n");
        scanf("%s", new_employee.fname);
    
    
        printf("What is the last name of the new employee?\n");
        scanf("%s", new_employee.lname);
    
    
        fscanf(ifp, "%d", &num_people);
    
    
        for("i = 0; i < num_people; i++){
        fscanf(ifp, "%s", temp_employee.fname);
        fscanf(ifp, "%s", temp_employee.lname);
        
        namecmp(struct record new_employee.lname, record temp_employee.lname)
        namecmp(struct record new_employee.lname, record temp_employee.lname)
        }
        
         //-Set up a loop for the number of employees
        //-Each time you should do the following:
        //--Read in a name from the input file
        //--Use the provided namecmp function to see if the
        //      new employee should be printed first
        //--Print the old employee to the output file
    
    
        //-If the new employee was never printed, print at the end of the file
    
    
        fclose(ifp);
        fclose(ofp);
    
    
        return 0;
    }
    
    
    /* Pre-Conditions: This function takes in two structures of type record.
     * These are labeled eA and eB for employee A and employee B, respectively.
     * Post-Conditions: This function compares the names of the two
     * employee to see which should come first lexicographically.
     * The function returns -1 if eA comes first, -1 if eB comes first,
     * and 0 if the employees have the same name.
     */
    int namecmp (struct record eA, struct record eB) {
        if (strcmp(eA.lname, eB.lname) < 0)
            return -1;
        else if (strcmp(eA.lname, eB.lname) > 0)
            return 1;
        else {
            if (strcmp(eA.fname, eB.fname) < 0)
                return -1;
            else if (strcmp(eA.fname, eB.fname) > 0)
                return 1;
            else
                return 0;
        }
    }

  14. #14
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Your calls to namecmp function are wrong. Look at what it returns, and what parameters you need to pass it.

  15. #15
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    Should i take off the record out of the functions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-25-2011, 01:12 PM
  2. File input/output, using a string as a filename?
    By Helgso in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2008, 03:13 AM
  3. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  4. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  5. Trouble with string output...Help!
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 06-08-2002, 02:26 PM