I get about 30 error messages. I know that there is a differnt way to wrote this linked like. This way i only get the error that NExt is not defined any ideas
Code:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define Nlen 26
typedef struct person_t {
char FName[Nlen];
char LName[Nlen];
char Sex;
char Grade;
} PERSON;
void amend_file( char file[], PERSON *STU );
void show_file( char file[], PERSON *STU );
//Funstion to Ordered linked list
PERSON* InsertNode (PERSON *Top, PERSON *Node)
{
PERSON *Here, /* Node being compared to new node */
*Prev; /* Node in front of Here node */
if (Top == NULL)/* List is empty – Place new node at top of list */
Top = Node;
else { /* Top != NULL *//* Start at Top of list */
Here = Top;
if ( Here->LName > Node->LName ) {/* Age less than first node’s age insert node at top of list */
Node->Next = Top;
Top = Node;
}
else {/* Search for place to insert new node */
Prev = Top;
Here = Prev->Next;
while ( (Here != NULL) &&
(Here->LName < Node->LName) ) {
Prev = Here;
Here = Here->Next;
}/* Insert into list at found spot */
Node->Next = Prev->Next;
Prev->Next = Node;
}
} /* end of Top != NULL */
return Top;
}
int main (void)
{
PERSON *STU;
int Ans;
struct listNode *start = NULL;
struct listNode *last = NULL;
struct listNode *data = NULL;
struct listNode *tempnode = NULL;
char file[] = "data.txt";
int Records [100];
do
{
printf("\n\n"
"1. Enter new record\n"
"2. Display List\n"
"3. Quit\n\n");
printf("Prompt: ");
fflush(stdout);
if ( scanf("%d", &Ans) == 1 ) /* get answer */
{
STU = (PERSON*) malloc (sizeof (PERSON));
if (STU == NULL)
{
perror("PERSON");
exit(EXIT_FAILURE);
}
switch(Ans)
{
case 1:
amend_file(file, STU);
break;
case 2:
show_file(file, STU);
break;
case 3: default:
Ans = 3;
break;
}
} /* /get answer */
}
while ( Ans != 3 );
fflush(stdout);
free(STU);
return EXIT_SUCCESS;
}
/*
* amend_file: Enter stuff into the working file.
* Very much a bare bones function so far; work on making this better
* Perhaps make it work with a linked list, as your struct suggests?
*/
void amend_file( char file[], PERSON *HEAD )
{
assert(HEAD != NULL);
FILE *fin = fopen(file, "a");
if ( fin == NULL )
{
perror(file);
exit(EXIT_FAILURE);
}
printf("Enter new record: ");
printf ("\nEnter students First name ( up to %d letters):", Nlen);
printf ("\nEnter students Last name ( up to %d letters):", Nlen);
printf ("\nPlease enter Sex");
printf ("\nPlease enter Grade\n");
scanf("%s %s %c %c", HEAD->FName, HEAD->LName, &HEAD->Sex,
&HEAD->Grade);
fprintf(fin, "%s %s %c %c\n", HEAD->LName, HEAD->FName, HEAD->Sex, HEAD->Grade);
fflush(fin);
fclose(fin);
}
/*
* show_file: Reads the working file.
* Bare bones again...
*/
void show_file ( char file[], PERSON *HEAD )
{
assert(HEAD != NULL);
int b = 1;
FILE *fout = fopen(file, "r");
if (file == NULL)
{
perror(file);
exit(EXIT_FAILURE);
}
while (fscanf(fout, "%s %s %c %c", HEAD->FName, HEAD->LName, &HEAD->Sex,
&HEAD->Grade) == 4)
{
printf("Record %d: %s %s %c %c\n", b, HEAD->FName, HEAD->LName,
HEAD->Sex, HEAD->Grade);
b++;
}
fclose(fout);
}