Thread: Structs homework

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    Structs homework

    Hello, I am a little lost and confused as to where to start this program and dont fully understand structs... if anyone can give me some feedback or tips on getting this program started it will be greatly appreciated. Thank you.

    Your program deals with files of student records:

    #define NAMESIZE 20
    struct student{
    char lname[NAMESIZE]; // lastname
    char fname[NAMESIZE]; // firstname
    int mid;
    int final;
    int hmwks;
    };

    We assume that all the records have different last names.

    Your program will display to the user the following menu:

    1. Sort a file
    2. Merge two sorted files
    3. Query a sorted file
    4. Display a file, including the scores of the students
    5. Quit
    Enter your choice:

    If the choice is 1, 2, 3, or 4 the corresponding action will be taken. Otherwise the user will be asked if to quit or continue.

    If the choice was 1., the user is asked to enter the name of a file containing unsorted student records and of the file where to store such records in sorted order. Then the action is carried out.

    If the choice was 2., the user is asked to enter the name of two sorted files of student records and the name of the merge file that has to be created. Then the action is carried out.

    If the choice was 3., the user is asked to enter the name of a (sorted) file of students. Then in a loop the user is asked for the last name of a student and the correspondent record is printed out if present in the file. Otherwise an apology is given. The loop terminates when the null string is entered.

    If the choice was 4., the user is asked to enter the name of a file of students. Then the student information is printed out together with a score computed with weight 50% for the final, 30% for the homeworks, and 20% for the midterm.

    Here are two unsorted files of students records that you may use to test your program: students1.txt and students2.txt

    As a comment at the beginning of your program you should do a case analysis for this problem: problem statement, analysis, design, implementation, and testing.

    This is what i have so far:
    Code:
    /*
    give user options and depending on a number, executes a function or 
    
    command (reads int)use switch or if statements & case when choice = 1(5 
    
    options). 
    
    Must know how to read information from a file and some functions writing 
    
    to a file (fopen specify with R or W)
    
    then read info from file fscanf
    
    */
    
    
    
    #include <stdio.h>
    
    #include <stdlib.h>
    
    
    
    #define SIZE 10
    
    #define NAMESIZE 25
    
    
    
    //compilation of student. organization.
    
    typedef struct {
    
        char lname[NAMESIZE];  //lastname
    
        char fname[NAMESIZE];  //firstname
    
        int mid;
    
        int final;
    
        int hmwrk;
    
    }
    
    
    
    int readFile(char filename[], student a[], int nmax) {
    
        //reads nmax student records from filename and stores them
    
        //in a. Returns number of records read.
    
        FILE *fd;  //file descriptor
    
        int i = 0;
    
    
    
        if((fd = fopen(filename, "r") == NULL) {
    
    	perror("fopen");
    
    	exit(1);
    
        }
    
        while (fscanf(fd,"%s %d %d %d",
    
    		a[i].name, a[i].fname, &a[i].midterm, &a[i].final, 
    
    &a[i].homework) != EOF) {
    
    	if (++i == nmax)
    
    	    break;  //table is full
    
        }
    
        fclose(fd);
    
        return i;
    
    }
    
    
    
    void writeFile(char filename[], student a[], int n) {
    
    
    
    }
    
    
    
    int main() {
    
        int x;  //input for function execution.
    
        int nmax;
    
        char filename[NAMESIZE];
    
    
    
        while(1) {
    
    	printf("1. Sort a file\n");
    
    	printf("2. Merge two sorted files\n");
    
    	printf("3. Query a sorted file\n");
    
    	printf("4. Display a file\n");
    
    	printf("5. Quit\n");
    
    	printf("Enter your choice:\n");
    
    
    
    	scanf("%d", &x);
    
    
    
    	switch(x) {
    
    	    case 1: readFile(filename, student a[], nmax);
    
    		writeFile(filename, student a[], nmax);
    
    		break;
    
    	    case 2: mergeSort();
    
    		break;
    
    	    case 3: querySort();
    
    		break;
    
    	    case 4: displayFile();
    
    		break;
    
    	    case 5: printf("You chose to quit.\n");
    
    		break;
    
    	    default: printf("Enter a valid value between 1 and 5.\n");
    
    		break;
    
    	}
    
        }
    
        return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Check for erros in your code! Basically you want to compile it and check to see if it does all that is required of it to do. Once you figure out what your program does or doesn't do. Then you can go on from there.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Don't double space your code please.

    A few other things:
    1. You need a name for your struct and a semicolon after the decalration/typedef.
    2. You're missing a parentheses in your first if statement in readFile.
    3. There are some syntax errors (wrong struct member names) in your read function, and your format string only has one %s, but you have two string variables.
    4. You don't have any way to break out of your while(1) loop for the menu. Better to ditch the while(1) and try something like:
    Code:
    do {
        // request input
        switch (input) {
            case '1':
                // read file
                // sort data
                // write file
                break;
        }
    } while (input != '5');
    By and large, you're on the right track, and I think your menu will work fine with the change I suggested. Now you just need to implement the rest of the functions you have in your case statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  2. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  3. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  4. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM