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

    I have all of this done so far but having trouble going on the next part, any ideas??

    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;
    
    
        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(new_employee, temp_employee);
        }
    
    
        //-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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
     * The function returns -1 if eA comes first, -1 if eB comes first,
     * and 0 if the employees have the same name.
     */
    What?

    Anyway, after you fix that, you just need to do exactly what it says. Think of what happens if you read a file in sorted order. The only time you would need to print anything different from what's already in the file is when you get to the spot where the new person should be in the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 04-19-2012, 10:09 PM
  2. Replies: 5
    Last Post: 04-25-2011, 01:12 PM
  3. 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
  4. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  5. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM