link list problem

i need a way to assign 15 names, originally scaned in from a file, to nodes, then ask user to assign up to 4 quiz scores for each name; assign the pointer to each name, and their score
tryingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

julio011597Commented:
This smells like a homework; post some code first, then you'll get advice.
0
tryingAuthor Commented:
ok, here is what i have so far, (yes, it's homework)

i can't get this to work the way i want it to....



#include<stdio.h>
#include<stdlib.h>

FILE      *fp;                                         //FILE POINTER

int      ctr,x,y;                   //GENERAL PURPOSE COUNTERS

struct Entry
      {
            int  scores[4];
            char name[25];                         //ACCEPT UP TO 25 char IN NAME
            struct Entry *next;
      };
typedef struct Entry NODE;


int      OpenFile(void);            

//MAIN
int main(void)
{
      //DECLARE STORAGE FOR 15 NODES (1 FOR EACH STUDENT)
   NODE N[15];
   NODE *Head, *ptr;
   
   //STUDENT NAMES
      OpenFile();
               
   //STORE NULL IN EACH POINTER
   for(ctr=0;ctr<15;ctr++)
   {
         N[ctr].next=(NODE *)NULL;
   }
   
   //CREATE ADD'L POINTER TO POINT TO Head *(FIRST NODE) OF LIST
   Head = &N[0];
   
   //SET A POINTER TO HEAD OF LIST AND ADVANCE THOUGH LIST VIA THE NEXT
   //POINTER; UNTIL NULL IS ENCOUNTERED. NULL INDICATES THE END OF LIST
   ptr = Head;
   
   for(x=0;x<15;x++)
   {
         while(fgets(N[ctr].name[x], 180, fp) != NULL)
            {
                  fputs(N[ctr].name[x], stdout);//DOES NOT WORK????
            }
      }
   
   printf("\nValue of N[0]=%s", N[0].name[x], stdout); //DON'T WORK????
   
   //CLOSE FILES PREVIOUSLY OPENED
   fclose(fp);            
      
      return(0);
}

// OPEN FILE
int OpenFile(void)
{
      //OPEN FILE FOR READ & CHECK FOR ERRORS
      if(!(fp = fopen("d:\\MSVC\\names.dat", "r")))
      {
            puts("\n\a***ERROR OPENING FILE***");

            //EXIT PROGRAM IF FAILURE
            exit(0);                                        
      }

      return(0);
}
0
ozoCommented:
cfe: Warning 712: trying.c, line 48: illegal combination of pointer and integer
          while(fgets(N[ctr].name[x], 180, fp) != 0 )
 --------------------------------^
cfe: Warning 712: trying.c, line 50: illegal combination of pointer and integer
      fputs(N[ctr].name[x], (&__iob[1]) );
 ----------------------^

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

julio011597Commented:
Hmmm, i see there's some work in progress, but i cannot see big errors other than your struct-typedef couple which should be:

typedef struct Entry {
  int scores[4];
  char name[25];
  struct Entry *next;
} NODE;


Ok, i'll leave this open, so that you can keep developing your code and coming back here for support; i just hope you won't grade the first "Cossak" who comes here and submits a couple of lines as answer, only because you don't feel like hitting the "reject" button! :)
0
julio011597Commented:
I was not talking about you, ozo! ;)
0
keasterCommented:
Hi trying,
I will try to point out some problems to you:
1) You have declared an array of 15 nodes, that makes it an array of 15 nodes, not a linked list. You would realize this when you see that the the "struct Entry *next" is superfluous in the struct. Try to figure out(lots of books) what a linked list is.
2) "while(fgets(N[ctr].name[x], 180, fp) != NULL)"
is wrong..
you get compilation errors because you should be typing:
   while(fgets(N[x].name, 25, fp) != NULL) instead.
   Note that N[x].name[ctr] refers to a character in the string "name" as upposed to N[x].name which is equivalent to "char *".
3) Before you use a function, try to find out what the parameters are and how to use it. Start with "fgets".
(Note: &__iob[1] is your "stdout" buffer)
I feel it woud not be appropriate for me to correct your code for you since you are learning.
Regards.
0
GregLCommented:
I just thought I would give you some tidbits on linked lists.  keaster is correct, your N[15] is just an array of Nodes not a link.  A link is where each node is attached to the next via the internal next pointer.  In your case you would malloc() the first node and then set the next pointer to null.  When a new node is needed you malloc another one and set the previous node to the new nodes pointer value.  You should then set the tail pointer to the newly malloced pointer.  This is called a singly linked list.  If you want a double linked list you would also have prev pointer that has the pointer value of the prevoius entry in the list.  Also, don't forget that you MUST free() all the nodes in sequence.  That is, start at the head and store the next pointer and free the current node then move to the next node until the next pointer is null.

Good Luck
0
q2guoCommented:
Hi trying

I fixed up your program a bit.  The program should construct the
linked list and store names in each node.  Let me know if it works.  If it works, I can help you with the next part of
the program (assign quiz scores).  If it doesn't work, can you please post your 'name.dat' file here.

------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>

FILE *fp;       //FILE POINTER

int ctr,x,y;                   //GENERAL PURPOSE COUNTERS

struct Entry
{
    int  scores[4];
    char name[26]; //ACCEPT UP TO 25 char IN NAME
    struct Entry *next;
};

typedef struct Entry NODE;


int OpenFile(void);

//MAIN
int main(void)
{
    int i;
    NODE *Head, *ptr;
 
    //ALLOCATE STORAGE FOR 15 NODES (1 FOR EACH STUDENT)        
    //AND LINK THEM TOGETHER

    Head = (NODE *) malloc (sizeof(NODE));
    Head->next = 0;
    ptr = Head;
   
    for (i=0; i < 15; i++) {
       ptr = ptr->next;
       ptr = (NODE *) malloc (sizeof(NODE));        
       ptr->next = 0;
    }

    //OPEN FILE FOR READING
    OpenFile();
                       
    //READ NAME INTO THE LINKED LIST
    ptr = Head;
    for(x=0;x<15;x++)
    {
        fgets(ptr->name, 25, fp);
        puts(ptr->name);
        ptr = ptr->next;
    }
           
    //CLOSE FILES PREVIOUSLY OPENED
    fclose(fp);

    return(0);
}


// OPEN FILE
int OpenFile(void)
{
    //OPEN FILE FOR READ & CHECK FOR ERRORS
    if(!(fp = fopen("d:\\MSVC\\names.dat", "r")))
    {
        puts("\n\a***ERROR OPENING FILE***");

        //EXIT PROGRAM IF FAILURE
        exit(0);                                  
    }

    return(0);
}







0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
q2guoCommented:
Did it work, trying?
0
tryingAuthor Commented:
yes! it worked! just fine, best answer i've received yet
(in the past, your answers have been nothing less than perfect.
i'm not just saying that. you've helped me more than anyone. at times, better than my teacher! thanks a mil)

i'm kinda lost though, can you tell me what's happening in
the code?
0
q2guoCommented:
int main(void)
{
    int i;
    NODE *Head, *ptr;
         
    //ALLOCATE STORAGE FOR 15 NODES (1 FOR EACH STUDENT)        
    //AND LINK THEM TOGETHER

    //Allocate space for one node and make head point to it
    Head = (NODE *) malloc (sizeof(NODE));    
    Head->next = 0;
    ptr = Head;
           
    for (i=0; i < 15; i++) {
        ptr = ptr->next;
        //Allocate space for one more node and link it with
        //the previous node
        ptr = (NODE *) malloc (sizeof(NODE));          
        //Make sure the last node is pointing to Null
        ptr->next = 0;
    }

    //OPEN FILE FOR READING
    OpenFile();
                             
    //READ NAME INTO THE LINKED LIST
    ptr = Head;
    for(x=0;x<15;x++)
    {
        //Go through each node and store the name in it
        fgets(ptr->name, 25, fp);    
        puts(ptr->name);
        ptr = ptr->next;
    }
                   
    //CLOSE FILES PREVIOUSLY OPENED
    fclose(fp);

    return(0);
}
0
tryingAuthor Commented:
hi q2guo,

i don't understand. at first, when i ran the program with
your changes, it worked fine. now it won't compile under
turbo c++ 3.0 (lines with: fgets(ptr->name,25,fp); etc.)
the error is something "connot convert char * to char far *"
and under windows using msvc v1.52 it compiles ok. but when
i go to run it, it performs an illegal shutdown? at the same
lines???
PLEASE HELP
0
q2guoCommented:
what compiler did the program work on?
0
tryingAuthor Commented:
q2guo,
it worked on Microsoft Visual C++ v1.52 (real old, but one of the
best out there!)
but, then it didn't work any more
??
could it have something to do with the fgets, and puts statements?

0
q2guoCommented:
Can you post your names.dat file here?  It 's probably not
reading the data file.
0
tryingAuthor Commented:
q2guo, here is the names.dat file: (no space at beginning or
end of file)

Albert Einstein
Steve Abrew
David Nagasake
Mike Black
Andrew Dijkstra
Joanne Nguyen
Chris Walljasper
Fred Albert
Dennis Dudley
Leo Rice
Fred Flintstone
Frances Dupre
Dave Light
Hua Tran
Sarah Trapp
0
q2guoCommented:
try this

---------------------------------------------
int main(void)
{
    int i;
    NODE *Head, *ptr;
                 
    //ALLOCATE STORAGE FOR 15 NODES (1 FOR EACH STUDENT)          
    //AND LINK THEM TOGETHER

    //Allocate space for one node and make head point to it
    Head = (NODE *) malloc (sizeof(NODE));      
    Head->next = 0;
    ptr = Head;
                 
    for (i=0; i < 15; i++) {
        //Allocate space for one more node and link it with
        //the previous node
        ptr->next = (NODE *) malloc (sizeof(NODE));          
        //Make sure the last node is pointing to Null
        ptr = ptr->next;
        ptr->next = 0;
    }

    //OPEN FILE FOR READING
    OpenFile();
                                     
    //READ NAME INTO THE LINKED LIST
    ptr = Head;
    for(x=0;x<15;x++)
    {
        //Go through each node and store the name in it
        fgets(ptr->name, 25, fp);    
        puts(ptr->name);
        ptr = ptr->next;
    }
                           
    //CLOSE FILES PREVIOUSLY OPENED
    fclose(fp);


    //DELETE THE LIST
    ptr = Head;
    for(x=0;x<15;x++)
    {
        Head = ptr->next;        
      delete ptr;        
        ptr = Head->next;
    }

    return(0);
}
0
tryingAuthor Commented:
q2guo,
it did not work, still getting program shutdown.
although, it begins to print the names in the file, then
shuts down after the 2nd or 3rd name.
0
q2guoCommented:
I made some more changes, try this

       ---------------------------------------------
       int main(void)
       {
           int i;
           NODE *Head, *ptr;
                         
           //ALLOCATE STORAGE FOR 15 NODES (1 FOR EACH STUDENT)          
           //AND LINK THEM TOGETHER

           //Allocate space for one node and make head point to it
           Head = (NODE *) malloc (sizeof(NODE));      
           Head->next = 0;
           ptr = Head;
                         
           for (i=0; i < 14; i++) {
               //Allocate space for one more node and link it with
               //the previous node
               ptr->next = (NODE *) malloc (sizeof(NODE));            
               //Make sure the last node is pointing to Null
               ptr = ptr->next;
               ptr->next = 0;
           }

           //OPEN FILE FOR READING
           OpenFile();
                                             
           //READ NAME INTO THE LINKED LIST
           ptr = Head;
           for(x=0;x<15;x++)
           {
               //Go through each node and store the name in it
               fgets(ptr->name, 25, fp);      
               puts(ptr->name);
               ptr = ptr->next;
           }
                                   
           //CLOSE FILES PREVIOUSLY OPENED
           fclose(fp);


           //DELETE THE LIST
           for(x=0;x<15;x++)
           {
               ptr = Head;
               Head = Head->next;        
               free ptr;        
           }

           return(0);
       }
0
tryingAuthor Commented:
q2guo,

it worked! no problems! i ran it several times to make sure.

now all i need to do is have ptr also point to scores[] in
structure, and read those from the keyboard.

thanks a mil
trying.
0
q2guoCommented:
Add the code below before line

//DELETE THE LIST

----------------------------------------------------------------
ptr = Head;
for (x=0; x <15; x++) {
    printf("Enter 4 grades for %s separated by spaces\n", ptr->name);
    scanf("%d %d %d %d", &ptr->scores[0], &ptr->scores[1], &ptr->scores[2], &ptr->scores[3]);;
    ptr = ptr->next;
}

0
tryingAuthor Commented:
q2guo,

the last piece of code works perfectly!
I would like to thank you for all your help.  You're a great
asset to Experts Exchange.

I look forward to working with you in the future.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.