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;
}