I am currently working on a new c language project that is supposed to deal with structures and linked lists. A description of the end program is listed below...
Load a set of student records from a file into a linked list structure
Sort the data structure by last name then the first name and print out to another file
The first line of the opened file will tell how many students there are in the list
Decide the appropriate data type and size based on the test data
Include a main program file, c program with maintenance functions, and a header file
I have recently started working on this and have not put in all of the parts just yet. As of right now I have a c program with the function, main function, and a header file. The problem that I am having right now is in the c program with functions. I am currently working on loading the file and sorting the file....
The error messages for the loading are as follows:
incompatible pointer type for fscanf
student undeclared
too few arguements for fgets
The error messages for the sorting are as follows:
fprintf from incompatible pointer type
I understand that the code that I have written is wrong, but I am not sure how I would go about scanning in the list from the opened file as well as sorting through the data. If anyone would have any ideas about how I could possibly go about doing this, it would be definately appreciated. Thanks!
Header File
Code:
typedef struct Student //Defines Structure
{
char first_name[40]; //First Name
char last_name[40]; //Last Name
char street[20]; //Street Name
char city[20]; //City Name
char state[2]; //State Initials
int zipcode; //Zipcode
int hours; //Number of hours
int feespaid; //Fees Paid
struct Student *next; //Points to the next
} StudentLink;
StudentLink * createNode(int x); //Creates New Student
void LoadStudent(StudentLink *root); //Loads set of student records
void SortStudent(StudentLink *root); //Sorts through the student records
void PrintStudent(StudentLink *root); //Prints out the records
void DeleteStudent(StudentLink *root); //Prototype Function: Deletes Records
Main Program
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include "student.h" //Includes the student library that was made
int main(int argc, char *argv[])
{
StudentLink *root, *temp, *prev; //Linked List
int x; //integer used for the loops
int choice; //Choice number for switch statement
int number; //Number of student in the new record
printf("To create a new list of students, enter 1\n");
printf("To load a file list of students, enter 2\n");
scanf("%d",&); //What case to choose
switch(choice) //Switch Statement
{
case "1":
{
printf("How many students would you like to create?");
scanf("%d",&number); //How many student are going to be in the link list
root = NULL; //Sets the root to null
prev = NULL; //Sets the prev to null
for (x = 0; x < number; x++)
{
temp = createNode(x); //Creates another student
printf("Enter the first name: "); //First Name
scanf("%s", temp->first_name);
printf("Enter the last name: "); //Last Name
scanf("%s", temp->last_name);
printf("Enter the street address: "); //Street
scanf("%s", temp->street);
printf("Enter the city: "); //City
scanf("%s", temp->city);
printf("Enter the zipcode: "); //Zipcode
scanf("%d", &(temp->zipcode));
printf("Enter the amount of hours: "); //Hours
scanf("%d", &(temp->hours));
printf("Enter the amount of fees paid: "); //Fees
scanf("%d", &(temp->feespaid));
if (root == NULL) root = temp; //Assigns root as temp
if (prev != NULL) prev->next = temp; //Assigns prev as next
prev = temp;
}
SortStudent(root); //Function to sort
PrintStudent(root); //Function to print
DeleteStudent(root); //Function to free up memory
break;
case "2":
{
LoadStudent(root); //Function to load
SortStudent(root); //Function to sort
PrintStudent(root); //Function to print
DeleteStudent(root); //Function to free up memory
break;
}
system("pause");
return 0;
}
C Program with Functions
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include "student.h" //Student Header File
StudentLink *createNode(int zipcode) //Creates a new student
{
StudentLink *temp; //Declaring temp
temp = malloc(sizeof(StudentLink));
temp->next = NULL;
temp->zipcode = zipcode; //Temp is assigned to x
return temp; //Returns the temp to the main program
}
void LoadStudent(StudentLink *root) //Loads a file of student records
{
StudentLink *temp;
FILE *fp;
char filename[40]; //Filename of the file to open
char mode[3];
char *fgets(char*str, int z, FILE *fp);
int x;
int z;
printf("Enter file name ");
gets(filename); //Recieving the filename
printf("Enter mode ");
gets (mode);
if( (fp = fopen(filename, mode)) != NULL)
{
printf("You have successfully opened the file\n");
fscanf("%d",&z);
temp = root;
for( x = 0; x < z; x++) //counting up through the number of students
{
fgets(student.first_name); //Scanning in the first name on the opened file
fgets(student.last_name);
fgets(student.street);
fgets(student.city);
fgets(student.state);
fgets(student.zipcode);
fgets(student.hours);
fgets(student.feespaid);
temp = temp->next; //Moves the temp up to the next set in the linked list
}
fclose (fp); //Closes the file
}
else
printf("File cannot be opened..."); //Error check
}
void SortStudent(StudentLink *root)
{
StudentLink *temp, *prev, *changeme;
int x;
int number;
for(x = 0; x < number; x++)
{
Student.last_name = *temp;
Student.last_name2 = *prev;
if(strcmp(Student.last_name, Student.last_name2) < 0
{
fprintf("%s",Student.last_name);
fprintf("%s",Student.last_name2);
}
if (strcmp(Student.first_name, Student.first_name2) < 0
{
fprintf("%s",Student.last_name2);
fprintf("%s",Student.last_name);
}
else
fprintf("%s",Student.last_name);
fprintf("%s",Student.last_name2);
}
void PrintStudent(StudentLink *root) //Prints out the student records
{
StudentLink *temp;
printf("First Name\t"); //Header for the First Name
printf("Last Name\t"); //Header for the Last Name
printf("Street\t");
printf("City\t");
printf("State\t");
printf("Zipcode");
printf("Hours");
printf("Fees Paid");
printf("\n\n\n"); //Line Break for style
temp = root;
while(temp != NULL) //Loop for printing out the students
{
printf("%s\t", temp->first_name); //Prints out the student's first name
printf("%s\t", temp->last_name); //Prints out the student's last name
printf("%s\t", temp->street);
printf("%s\t", temp->city);
printf("%s\t", temp->state);
printf("%d\t", temp->zipcode);
printf("%d\t", temp->hours);
printf("%d\t", temp->feespaid);
temp = temp->next; //Moves on to the next student
}
}
void DeleteStudent(StudentLink *root) //Frees up the memory of the program
{
StudentLink *temp, *deletethis;
temp = root;
while(temp)
{
deletethis = temp; //Temp is named to be freed
temp = temp->next; //Moves onto the next
free(deletethis); //Frees this particular temp
}
}