Having trouble identifying whats wrong.

This is a discussion on Having trouble identifying whats wrong. within the C Programming forums, part of the General Programming Boards category; input file is 3 Abdul Rachel Lopez Charlie Schofield Theo by writing kyle richards and the new employee, im trying ...

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

    Having trouble identifying whats wrong.

    input file is

    3
    Abdul Rachel
    Lopez Charlie
    Schofield Theo

    by writing kyle richards and the new employee, im trying to get an output like this.

    Rachel Abdul
    Charlie Lopez
    Kyle Richards
    Theo Schofield

    So im trying to get it to write in alphabetical order to whatever employee name I type in.
    This code that i have on the bottom is writing this to the output file.

    Abdul
    KyleLopez
    KyleSchofield
    Kyle

    I ve been looking at this for a while and i dont know where to go next, any ideas why my code is not working?






    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);
    
    
        fprintf(ofp, "%s\n", &temp_employee);
        fprintf(ofp, "%s", &new_employee);
    
    
        }
    
    
        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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    FILE *ofp = fopen("roster.txt", "w");
    That is going to start with a new file every time (in effect) because it truncates the file when you open it to w mode.
    Code:
        fprintf(ofp, "%s\n", &temp_employee);
        fprintf(ofp, "%s", &new_employee);
    Neither of those two & should be there. You are also apparently trying to write the entire structure, instead of writing both names one at a time. You need either fwrite or something like new_employee.fname and new_employee.lname. Also note that you are writing new_ once and temp_ once. So if that was .fname and .lname, you'd be writing the .fname of one and the .lname of the other.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by quzah View Post
    Code:
    FILE *ofp = fopen("roster.txt", "w");
    That is going to start with a new file every time (in effect) because it truncates the file when you open it to w mode.
    I doubt that is going to be an issue during a dev/test stage especially with such a small output file. I never care if I overwrite output files during dev and test stages until I'm moving closer to finalizing.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    I found an example code that is doing similar functions that i am supposed to do, I just dont really know how to change the code so it applies to my code.
    Any ideas??

    Code:
    //included libraries
    #include <stdio.h>
    #include <string.h>
    
    
    //constants
    #define NUMSTUDENTS 5
    
    
    //Structures
    //Structures are used to create new data types
    struct record {
        char fname[20];
        char lname[20];
        int PID;
    };
    
    
    //Function Prototypes
    void print_students (struct record students[]);
    void sort (struct record students[]);
    void swap (struct record students[], int a, int b);
    int namecmp (struct record sA, struct record sB);
    
    
    //Main Function
    int main() {
        FILE *ifp = fopen("students.txt", "r");
        int i;
        //In order to declare a variable of the structure type
        //you need "struct <struct name> <variable name>.
        //The [NUMSTUDENTS] creates an array of structures.
        //The size is dictated by NUMSTUDENTS.
        struct record students[NUMSTUDENTS];
    
    
        //This loop reads in all the student information
        //from the file.
        for(i=0; i<NUMSTUDENTS; i++) {
            fscanf(ifp, "%s", students[i].lname);
            fscanf(ifp, "%s", students[i].fname);
            fscanf(ifp, "%d", &students[i].PID);
        }
    
    
        //Call the function "sort" to sort the array of students
        sort(students);
    
    
        //Call the function "print_students" to print the list of students
        print_students(students);
    
    
        return 0;
    }
    
    
    /* Pre-Conditions: This function takes in an array of
     * structures of type record.
     * Post-Conditions: This function prints the students information.
     * The constant NUMSTUDENTS is used to control the loop.
     */
    void print_students (struct record students[]) {
        int i;
    
    
        for(i=0; i<NUMSTUDENTS; i++)
            printf("%s %s: %d\n",  students[i].fname, students[i].lname,
                   students[i].PID);
    
    
        return;
    }
    
    
    /* Pre-Conditions: This function takes in two structures of type record.
     * These are labeled sA and sB for student A and student B, respectively.
     * Post-Conditions: This function compares the names of the two
     * students to see which should come first lexicographically.
     * The function returns -1 if sA comes first, -1 if sB comes first,
     * and 0 if the students have the same name.
     */
    int namecmp (struct record sA, struct record sB) {
        if (strcmp(sA.lname, sB.lname) < 0)
            return -1;
        else if (strcmp(sA.lname, sB.lname) > 0)
            return 1;
        else {
            if (strcmp(sA.fname, sB.fname) < 0)
                return -1;
            else if (strcmp(sA.fname, sB.fname) > 0)
                return 1;
            else
                return 0;
        }
    
    
        return 3;
    }
    
    
    /* Pre-Conditions: This function takes in an array of structures
     * of type record and two integers a and b.  a and b represent two
     * indices of the array - these are the two structs in the array
     * that are to be swapped.
     * Post-Conditions: This function swaps the two structs in the
     * array at the indicated indices.
     */
    void swap (struct record students[], int a, int b) {
        struct record temp;
        temp = students[a];
        students[a] = students[b];
        students[b] = temp;
        return;
    }
    
    
    /* Pre-Conditions: This function takes in an array of structures
     * of type record.
     * Post-Conditions: This function sorts the array.
     * Specifically, this is an implementation of the Bubble Sort Algorithm.
     * See http://en.wikipedia.org/wiki/Bubble_sort for more information.
     * This function will call both namecmp and swap.
     */
    void sort (struct record students[]) {
        int flag = 0, i;
    
    
        while(flag == 0) {
            flag = 1;
    
    
            for (i=1; i<NUMSTUDENTS; i++) {
                if (namecmp(students[i-1], students[i]) > 0) {
                    swap(students, i-1, i);
                    flag = 0;
                }
            }
        }
        return;
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So you don't actually want to learn anything, you just want someone to do it for you? Ok, cya.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    Its not like that at all, i am trying to work with things im not fully comfortable with. By someone guiding me in the right direction then i will be learning how it all works.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Well the first thing to do is study how they use fprintf properly to print to a file.

    When you can successfully make a copy of your input file without doing anything else, then study how arrays are used.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by douglas481 View Post
    Its not like that at all, i am trying to work with things im not fully comfortable with. By someone guiding me in the right direction then i will be learning how it all works.
    Then take the time to get comfortable with those things.

    Don't try and take shortcuts by plagurising someone else's code. Learning means writing it yourself.

    No amount of explanation is a substitute for figuring things out on your own.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    20
    This example code i have was given to me by my teacher, she wanted me to learn from it. I didnt take it from anywhere else.Thank you guys anyways i figured out my code. You guys were very helpful.

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    Hey I believe we are in the same class, I am having the same problem. It's just a somewhat difficult assignment considering it's the final one for the semester in our Intro to C Programming class. If you could message me somehow and let me know what you have done to fix the issue I'd appreciate it. I've been having similar issues to yours and feel as though I've tried just about everything I could think of, I'm about to give up lol.. Thanks

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > If you could message me somehow and let me know what you have done to fix the issue I'd appreciate it.
    Yes, I'm sure your tutor wouldn't spot that right away.
    The OP has the benefit of "publishing first" to prove they did the work and you copied.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    Lol you really think I would just copy it over, at the very least I would incorporate and alter it into my already written code, so it wouldn't be identical. Besides there are a large number of students also taking our course, so there are definitely going to be similar/identical parts of code being submitted already..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats wrong in this!!!!
    By chintugavali in forum C++ Programming
    Replies: 1
    Last Post: 12-19-2007, 02:55 AM
  2. whats wrong with this?
    By Hugo716 in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2006, 11:52 PM
  3. whats wrong?!?
    By stuart_cpp in forum C++ Programming
    Replies: 7
    Last Post: 04-23-2006, 12:08 PM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-27-2005, 11:48 PM
  5. Can anyone tell me whats wrong?
    By Dragoncaster131 in forum C Programming
    Replies: 2
    Last Post: 05-16-2004, 04:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21