Thread: Linked Lists with multiple programs

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    11

    Linked Lists with multiple programs

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

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Missing something?
    Code:
    scanf("%d",&);  //What case to choose
    Also are you creating your own fgets()?
    in LoadStudent():
    Code:
    char *fgets(char*str, int z, FILE *fp);
    Last edited by jeffcobb; 02-18-2010 at 05:24 PM.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    I have changed the majority of the code concerning the loadstudent and sortstudent function, but I am still getting error messages. I think that most of them stem from the sort and scanning in the first number which is the number of students.

    Code:
    void SortStudent(StudentLink *root)
    {
         StudentLink *temp, *prev, *next, *changeme;
         int x, y;
         
    
         FILE *fp;
         char filename[80];
         int counter;
         int studentnumber;
         char line[80];
    
         
         printf("Enter the file name: ");           //Enter in the file name
         scanf("%s", filename);
         
    
         
         if((fp = fopen(filename, "w+")) != NULL)   //Opening the file
         {
               printf("You have successfully opened the file!\n");
               fscanf(filename, "%d",&studentnumber);             //Number of students in the list
               printf("The number of students on the file is %d\n\n\n");
               
               
               printf("First Name\tMiddle Name\tLast Name\t");
               printf("Street\tCity\tState\tZipcode\tHours\tFees Paid\n\n\n");
               
               temp = root;
               
               for(counter = 0; counter < studentnumber; counter++)           //Counting up through
               {
                           fgets(line, 80, fp);
                           printf("%s\t",line);          //Printing out hte stucture
                           fgets(line, 80, fp);
                           printf("%s\t",line); 
                           fgets(line, 80, fp);
                           printf("%s\t",line); 
                           fgets(line, 80, fp);
                           printf("%s\t",line);                        
                           fgets(line, 80, fp);
                           printf("%s\t",line);                        
                           fgets(line, 80, fp);
                           printf("%s\t",line); 
                           fgets(line, 80, fp);
                           printf("%d\t",line);                       
                           fgets(line, 80, fp);
                           printf("%d\t",line);                            
                           fgets(line, 80, fp);
                           printf("%d\t",line);     
                           
                           temp = temp->next;       //Moving on to the next
               }
         
          
         for(x = 0; x < studentnumber-1; x++)       //Counting up to the number of students minus one
         {
               for(y = 0; y < studentnumber-x-1; y++)          //Counting up behind the other loop
               {
                     if(temp->last_name>next->last_name)       //if the temp is greater than the next
                     {
                                        root=next;             //next become the root so the last name is sorted
                                        prev=next;
                     }
                     else 
                     {
                          prev->last_name=next;                //other way around
                          prev=next;
                     }
                     
                     if(next != NULL)
                     {
                             next = temp->last_name;           //If it isn't the last keep going
                     }
                     else
                     {
                         prev=temp;
                         temp=next;
                         next=temp->last_name;                 //Ending the linked list
                     }
               }
               
               temp=root;
               
               while(temp != NULL)
               {
                          printf("%s",temp->last_name);        //printing out the last name in order
                          temp=temp->last_name;
               }
               }
               
               fclose(fp);
         }
         
         else
         {
             printf("The file cannot be opened...");
         }
    
    }
    If anyone has any ideas, that would be great!

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by crimsonpetal13 View Post
    If anyone has any ideas, that would be great!
    My idea is you try and be more specific about this:
    I am still getting error messages. I think that most of them stem from the sort and scanning
    Since it is unlikely anyone is going to want to compile and test all this for you.

    It is a very bad practice to write so much code and then at the end discover it is full of errors. You should be finding a way to test compile & run every 5-10 lines -- even if there is not going to be any output.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    I understand what you mean about the errors, so here are the following errors:

    [Warning] passing arg 1 of `fscanf' from incompatible pointer type
    [Warning] passing arg 1 of `fscanf' from incompatible pointer type
    incompatible types in assignment
    [Warning] assignment from incompatible pointer type
    [Warning] assignment from incompatible pointer type
    [Warning] assignment from incompatible pointer type

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Don't you get line numbers for those errors? Then you can post the error and the specific line with a bit of context, say 5 lines on either side. Anyway, the first ones I found to which these would apply:

    "[Warning] passing arg 1 of `fscanf' from incompatible pointer type"
    Code:
    fscanf("%d",&z);
    "incompatible types in assignment"
    Code:
    prev->last_name=next;
    "[Warning] assignment from incompatible pointer type"
    Code:
    next = temp->last_name;
    That last one is repeated a few times exactly the same. If you don't understand why these are errors, just ask.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Did you remember to put "break" in for each case?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    Yes, I have all of the breaks after each case.

    I am wondering if there is a problem with the choice that is being scanned in and the case choice.

    Here is the rest of the code, and there are no error messages.

    Code:
        printf("To create a new list of students, enter 1\n");
        printf("To load a file list of students, enter 2\n");
        
        scanf("%d", &choice);  //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 (counter = 0; counter < number; counter++)
                                   {
                                        temp = createNode(counter);    //Creates another student
         
                                        printf("Enter the first name: ");  //First Name
                                        scanf("%s", temp->first_name);
                                        printf("Enter the middle name: ");
                                        scanf("%s", temp->middle_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;
         
                                   }
                                   
                                   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;    
                              }
                         default:
                               {  
                                 printf("Incorrect input...");
                                 break;
                               } 
         }
    Thanks

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Ah. You are using scanf() naively. Read this, it will take you about 10 minutes:

    STDIN pitfalls

    Once you understand that you should be able to figure out what needs changing and how.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Code:
    scanf("%d", &choice);  //What case to choose
        
    switch(choice)  //Switch Statement
    {
        case '1':
        {
    choice is an int, '1' is a char (which when converted to int would have the value of 49). Remove the ' ' around 1 and 2

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by _Mike View Post
    Code:
    scanf("%d", &choice);  //What case to choose
        
    switch(choice)  //Switch Statement
    {
        case '1':
        {
    choice is an int, '1' is a char (which when converted to int would have the value of 49). Remove the ' ' around 1 and 2
    Bang on!

    After looking at the code again I think the scanf is actually okay, but if you didn't understand the stuff in that page you might as well read it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    Yeah, I went ahead and changed the switch case to integers instead of characters and it worked great. I am almost done with the program. I have tested the creating student function, and I am working out some minor problems there. I am now testing the load and sorting functions of the program, but I keep running into the problem of not reading in the number of students correctly.

    Example--

    Code:
    void SortStudent(StudentLink *root)
    {
         StudentLink *temp, *prev, *next, *changeme;
         int x, y;
         
    
         FILE *fp;
         int counter;
         char filename[80];
         int studentnumber;
         char line[80];
    
         
         printf("Enter the file name: ");           //Enter in the file name
         scanf("%s", filename);
         
    
         
         if((fp = fopen(filename, "w+")) != NULL)   //Opening the file
         {
               printf("You have successfully opened the file!\n");
              
              { 
               fscanf(fp, "%d", &studentnumber);             //Number of students in the list
               printf("The number of students on the file is %d\n\n\n");
               }

    The output comes out something like this, but no error messages.

    The file has been successfully opened!
    The number of students is 426785254.


    When in reality the text file actually only has 7 as the number of students.

    I have tried the following: freeing any information before scanning in the number, using some of the examples in STDIN pitfalls, etc. I know that it is not reading in the number correctly, but i am not sure why. I am new at this, and I am sorry to ask so many questions.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
              { 
               fscanf(fp, "%d", &studentnumber);             //Number of students in the list
               printf("The number of students on the file is %d\n\n\n");
               }
    Something missing here...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    I see that I forgot to tell it what to print...

    Code:
    fprintf("The number of students is %d", studentnumber);
    It is still freezing up and not registering the number. It says the number of students is 0 before it freezes and stops working.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should start compiling with warnings on. You are missing an argument to your fprintf call. Even if this is an example, you seem to be making this same mistake over and over in your use of examples (or your actual code). In any event, compile with warnings on, and pay attention to what you're doing with each function you call.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple linked lists
    By killer in forum C++ Programming
    Replies: 8
    Last Post: 07-06-2006, 01:02 AM
  2. Multiple Linked Lists w/HeadNode
    By tofugirl in forum C Programming
    Replies: 12
    Last Post: 12-10-2005, 10:21 AM
  3. about linked lists
    By armin_miewes in forum C Programming
    Replies: 4
    Last Post: 01-08-2004, 11:17 AM
  4. Questions on Linked Lists
    By Weng in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2003, 01:17 AM
  5. Linked Lists -- Again!!! Help!!
    By Leeman_s in forum C++ Programming
    Replies: 4
    Last Post: 01-22-2002, 08:27 PM

Tags for this Thread