I'm having trouble calling my linked list of Insernode. Can some one help me please.
I need ot call it when it read teh data from teh file and puts it in the lists. I also need to use the list once i eneter in there data. adn if i enter is is there a way toadd the data the comparie inser the data that i have into file saved file.

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Nlen    26
typedef struct person_t {
   char FName[Nlen];
   char LName[Nlen];
   char Sex;
   char Grade;
   struct person_t *Next;
  } PERSON;

/*********************************************************************
 Funstion to Ordered linked list
 This is where i will call the Last name and put it into the linked   
 list ordered by last name. 
 
********************************************************************/
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) {               
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;
}
/*************************************************************
end of  Funstion to Ordered linked list
***************************************************************/
void amend_file( char file[], PERSON *STU ); 
void show_file( char file[], PERSON *STU );

int main (void)
{
   PERSON *STU;
   int Ans;
        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);  //calls the amend file funcation
                InsertNode;  //calls the insert Node funcation  which it is not getting into becuase
                //Im haveing issues with getting the file to print. 
              break;
            case 2:
               show_file(file, STU);  //calls teh show file funcation
               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.
*
**************************************************************************/
void amend_file( char file[], PERSON *STU )
{
   assert(STU != 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", STU->FName, STU->LName, &STU->Sex,
      &STU->Grade);
    fprintf(fin, "%s %s %c %c\n", STU->LName, STU->FName, STU->Sex, STU->Grade);
           fflush(fin);
            fclose(fin);  
      }
/******************************************************************************
* show_file: Reads the working file.
* Bare bones again...
******************************************************************************/
void show_file ( char file[], PERSON *STU )
{
  assert(STU != NULL);
  int b = 1;
  FILE *fout = fopen(file, "r");
    
  if (file == NULL)
  {
    perror(file);
    exit(EXIT_FAILURE);
  }
  while (fscanf(fout, "%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
     &STU->Grade) == 4)
  {              
    printf("Record %d: %s %s %c %c\n", b, STU->FName, STU->LName,
       STU->Sex, STU->Grade);
    b++;
 // }
  fclose(fout);
}

I compiled and ran it with Dev-C++ and it seemed to work ok. The only change I had to make was to flush the input stream after selecting a menu. You should not use scanf() especially with strings because it will allow you to type more characters than the input buffer will hold, causing buffer overflow. It also leaves <Enter> keys in the keyboard buffer so that the next time you call scanf() or getchar() these functions will not appear to do anything. The standard C solution is to use fgets() to get the inputs when use atol() to convert the input buffer to integer when necessary. For example:

fgets(STU->FName,sizeof(STU->FName),stdin);

i changed that just for some reason its not getting into the linked list and sorting it. I did chagne the code here it is

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Nlen    26
typedef struct person_t {
   char FName[Nlen];
   char LName[Nlen];
   char Sex;
   char Grade;
   struct person_t *Next;
  } PERSON;
struct person_t  *Top = NULL;         
struct person_t  *Node = NULL;
/*********************************************************************
 Funstion to Ordered linked list
 This is where i will call the Last name and put it into the linked   
 list ordered by last name. 
 
********************************************************************/
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) {               
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;
}
/*************************************************************
end of  Funstion to Ordered linked list
***************************************************************/
void amend_file( char file[], PERSON *STU ); 
void show_file( char file[], PERSON *STU );

int main (void)
{
   PERSON *STU;
   int Ans;
        char file[] = "data.txt";
    int Records [100];
     FILE *fin = fopen(file, "a+");
     struct person_t  *Top = NULL;    
 struct person_t  *Node = NULL;
 
    
    
  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 */
      {
        fgets(STU->FName,sizeof(STU->FName),stdin);
         if (STU == NULL)
         {
            perror("PERSON");
            exit(EXIT_FAILURE);
         }
         switch(Ans)
         {
            case 1:
               amend_file(file, STU);  //calls the amend file funcation 
              break;
            case 2:
               show_file(file, STU);  //calls teh show file funcation
               break;
            case 3: default:
               Ans = 3;
               break;
         }
      } /* /get answer */
   }
    while ( Ans != 3 );
    
      fflush(stdout);
      free(STU);
      getchar ();
   return EXIT_SUCCESS;
}
/*************************************************************************
* amend_file: Enter stuff into the working file.
*
**************************************************************************/
void amend_file( char file[], PERSON *STU )
{
   assert(STU != 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", STU->FName, STU->LName, &STU->Sex,
      &STU->Grade);
       Top = InsertNode (Top, Node);  //calls the insert Node funcation  which it is not getting into becuase
                //Im haveing issues with getting the file to print. 
            fprintf(fin, "%s %s %c %c\n", STU->LName, STU->FName, STU->Sex, STU->Grade);
           fflush(fin);
            fclose(fin); 
      }
/******************************************************************************
* show_file: Reads the working file.
* Bare bones again...
******************************************************************************/
void show_file ( char file[], PERSON *STU )
{
  assert(STU != NULL);
  int b = 1;
  FILE *fin = fopen(file, "r");
    
  if (file == NULL)
  {
    perror(file);
    exit(EXIT_FAILURE);
  }
  while (fscanf(fin, "%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
     &STU->Grade)== 4)
  {   
   Top = InsertNode (Top, Node);  //read insernode function
    b++;          
    printf("Record %d: %s %s %c %c\n", b, STU->FName, STU->LName,
       STU->Sex, STU->Grade);
  }
  fclose(fin);
}

ok I did that cahnge. I'm still not able to get the code to go into the linked list any help to what or where i messed up please

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Nlen    26
typedef struct person_t {
   char FName[Nlen];
   char LName[Nlen];
   char Sex;
   char Grade;
   struct person_t *Next;
  } PERSON;
struct person_t  *Top = NULL;         
struct person_t  *Node = NULL;
/*********************************************************************
 Funstion to Ordered linked list
 This is where i will call the Last name and put it into the linked   
 list ordered by last name. 
 
********************************************************************/
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) {               
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;
}
/*************************************************************
end of  Funstion to Ordered linked list
***************************************************************/
void amend_file( char file[], PERSON *STU ); 
void show_file( char file[], PERSON *STU );

int main (void)
{
   PERSON *STU;
   int Ans;
        char file[] = "e:data.txt";
    int Records [100];
     FILE *fin = fopen(file, "a+");
     struct person_t  *Top = NULL;    
 struct person_t  *Node = NULL;
 
    
    
  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 */
      {
        fgets(STU->FName,sizeof(STU->FName),stdin);
         if (STU == NULL)
         {
            perror("PERSON");
            exit(EXIT_FAILURE);
         }
         switch(Ans)
         {
            case 1:
               amend_file(file, STU);  //calls the amend file funcation 
              break;
            case 2:
               show_file(file, STU);  //calls teh show file funcation
               break;
            case 3: default:
               Ans = 3;
               break;
         }
      } /* /get answer */
   }
    while ( Ans != 3 );
    
      fflush(stdout);
      free(STU);
      getchar ();
   return EXIT_SUCCESS;
}
/*************************************************************************
* amend_file: Enter stuff into the working file.
*
**************************************************************************/
void amend_file( char file[], PERSON *STU )
{
   assert(STU != 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", STU->FName, STU->LName, &STU->Sex,
      &STU->Grade);
       Top = InsertNode (Top, Node);  //calls the insert Node funcation  which it is not getting into becuase
                //Im haveing issues with getting the file to print. 
            fprintf(fin, "%s %s %c %c\n", STU->LName, STU->FName, STU->Sex, STU->Grade);
           fflush(fin);
            fclose(fin); 
      }
/******************************************************************************
* show_file: Reads the working file.
* Bare bones again...
******************************************************************************/
void show_file ( char file[], PERSON *STU )
{
  assert(STU != NULL);
  int b = 1;
  FILE *fin = fopen(file, "r");
    
  if (file == NULL)
  {
    perror(file);
    exit(EXIT_FAILURE);
  }
  while (fscanf(fin, "%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
     &STU->Grade)== 4)
  {   
   Top = InsertNode (Top, Node);  //read insernode function
    b++;          
    printf("Record %d: %s %s %c %c\n", b, STU->FName, STU->LName,
       STU->Sex, STU->Grade);
  }
  fclose(fin);
}

Please localize the errors or the problem areas so that we can help you out. Just dumping this big code is a bit daunting to read. Tell us which of your functions are not functioning properly and post only those. Eg. is the insertion going on properly, is there problem appearing in append_file etc..

When I compiled your code I got a warning in function main() that variable STU was being used before it is initialized. You need to make certain you fix all warnings as well as errors because most of the time warnings such as this one are actually errors.

fgets(STU->FName,sizeof(STU->FName),stdin);

The above is the offending line. STU has not been initialized to point to anything.

Anyone with some help my program does not want to read right and display it i need help kinda fast. Thanks

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include <math.h>
#define ROWNO 11
#define COLNO 11
#define XVAL 40
#define YVAL 40
#define SIZE 20

struct Node
{
    int x, y, points,state;
    struct Node *n,*p,*u,*d;

};
struct Node *createLList(struct Node *hPtr,int gold, int trap)
{
    struct Node *newptr, *curr,*top,*left,*above;
    int x = 0,y = 0,j;

    newptr = (struct Node *) malloc (sizeof(struct Node));

    newptr->x = XVAL + x * SIZE;
    newptr->y = YVAL + y * SIZE;
    newptr->state = 0;
    newptr->points = 0;
    newptr->n = newptr;
    newptr->p = newptr;
    hPtr = curr = left = above = newptr;
    j=rand() %10;
    if((j==1)&&(gold))
        {curr->points=5;
         gold--;}
    if((j==2)&&(trap))
        {curr->points=-3;
         trap--;}
    for(y=1; y<=11; y++)
    {

        for(x=1; x<=10; x++)
        {
            j=rand() %10;
            if((j==1)&&(gold))
                {curr->points=5;
                 gold--;}
            if((j==2)&&(trap))
                {curr->points=-3;
                 trap--;}
            newptr = (struct Node *) malloc (sizeof(struct Node));
            newptr->x = XVAL + x * SIZE;
            newptr->y = YVAL + ((y-1) * SIZE);
            newptr->state = 0;
            newptr->points = 0;
            newptr->p = curr;
            curr->n = newptr;
            newptr->n = left;
            left->p = newptr;
            curr = newptr;

            above = above->n;
            top=top->n;
            newptr->u = above;
            above->d = newptr;
            newptr->d = top;
            top->u = newptr;
            if((y==11)&&(x==10))
                goto endd;
        }
        x=0;
        top = hPtr;
        above = left;
        j=rand() %10;
        if((j==1)&&(gold))
            {curr->points=5;
             gold--;}
        if((j==2)&&(trap))
            {curr->points=-3;
             trap--;}
        newptr = (struct Node *) malloc (sizeof(struct Node));
        newptr->x = XVAL + x * SIZE;
        newptr->y = YVAL + y * SIZE;
        newptr->state = 0;
        newptr->points = 0;
        newptr->u = above;
        newptr->d = top;
        top->u = newptr;
        above->d = newptr;
        left = newptr;
        curr = left;
    }
    endd:
    return hPtr;
}
void drawAll(struct Node *hptr,struct Node *curr)
{
    struct Node *temp;
    int x, y, row, col, color,statt;
    temp = curr;
    curr = hptr;

    for(y=1;y<=11;y++)
    {
        for(x=1;x<=11;x++)
        {
            switch(curr->state)
            {  case 0:color=15;
                  statt=4;
                  break;
               case 1:color=0;
                  statt=1;
                  break;
            }
            setfillstyle(statt, color);
            bar(curr->x,curr->y,curr->x+15,curr->y+15);
            curr = curr->n;
        }

        curr = curr->n->d;
    }
    curr = temp;
    setfillstyle(1, 15);
    bar(curr->x,curr->y,curr->x+15,curr->y+15);
}
int main(int argc,char *argv[])
{

    int gd = DETECT, gm;
    char key;
    int i , j,gold = 0,trap = 0,points = 0;
    struct Node *curr, *head;
    FILE *fPtr;
    char string[100][50];
//    initgraph(&gd, &gm, "e:\\prog\\TC\\bgi\\");
    initgraph(&gd, &gm, "c:\\TC\\bgi\\");
    head=createLList(head,atoi(argv[1]),atoi(argv[2]));
    curr = head;

    fPtr = fopen("e:\\test.dat", "r");
    if(fPtr == NULL)
       {    cprintf("Error, File could not be opened\n\r");
            getch();
            goto end;}
    else
    {   i=1;
        while(1)
        {
            fscanf(fPtr, "%s", string[i]);
            if(feof(fPtr))
                break;

            if(atoi(string[i]) == 1)
                curr->state = 1;
            else if(atoi(string[i]) == 0)
                     curr->state = 0;
            if(i==12)
               {curr = curr->n->d;
                i=1;}
            else
               curr = curr->n;
            i++;
        }

        fclose(fPtr);
    }
    drawAll(head,curr);

    while(1)
    {key = getch();
     if(key == 0)
        {key = getch();}
     if(key == 0x1b)
        break;
     if(points<0)
        break;
        switch(key)
        {      case 0x4b:if(curr->p->state == 0)
                    curr = curr->p;
                 break;
               case 0x4d:if(curr->n->state == 0)
                    curr = curr->n;
                 break;
               case 0x48:if(curr->u->state == 0)
                    curr = curr->u;
                 break;
               case 0x50:if(curr->d->state == 0)
                    curr = curr->d;
                 break;
        }
     drawAll(head,curr);
     if(key == 13)
        {points = points + curr->points;
         switch(curr->points)
         {  case 5:gold++;
               setfillstyle(1, 14);
               bar(curr->x,curr->y,curr->x+15,curr->y+15);
               break;
        case -3:trap++;
            setfillstyle(1, 4);
            bar(curr->x,curr->y,curr->x+15,curr->y+15);
            break;
         }
     sprinf(points, "No of points: %f\n", M_PI);


        }

    }
    end:
    return 0;
}

Thanks, well it uses a text file that it reads 2 from
example...
1 0 1 1 0 1 0 1 0 1 0
1 0 1 1 0 1 0 1 0 1 0
1 0 1 1 0 1 0 1 0 1 0
1 0 1 1 0 1 0 1 0 1 0
1 0 1 1 0 1 0 1 0 1 0
1 0 1 1 0 1 0 1 0 1 0
1 0 1 1 0 1 0 1 0 1 0
1 0 1 1 0 1 0 1 0 1 0
1 0 1 1 0 1 0 1 0 1 0
1 0 1 1 0 1 0 1 0 1 0
1 0 1 1 0 1 0 1 0 1 0
11 by 11
Then i should be able to walk on a 0 and not allowed
on a one, the program is fine just there are something wrong on left side 2 lines.Sorry bout yesterday was in a hurry.:eek:

This article has been dead for over six months. Start a new discussion instead.