Linked Lists with multiple programs

This is a discussion on Linked Lists with multiple programs within the C Programming forums, part of the General Programming Boards category; I am currently working on a new c language project that is supposed to deal with structures and linked lists. ...

  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
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    Here are the detailed error messages, with the line numbers.


    41 G:\School\MATH 202\Homework\Student.c [Warning] passing arg 1 of `fscanf' from incompatible pointer type

    I was trying to scan in the first number of the file, which should indicate the number of students that are in the file. I attempted to use the fscanf file, but this is the error message that I keep receiving. I fixed several of my other error messages, but I am not sure why i am receiving this one or the ones below.

    Code:
               fscanf(filename, "%d",&z);
    105 G:\School\MATH 202\Homework\Student.c [Warning] passing arg 1 of `fscanf' from incompatible pointer type

    Same as above

    150 G:\School\MATH 202\Homework\Student.c incompatible types in assignment

    Here as as well as the other error messages throughout the rest of program are because I was attempting to compare the different last names of the linked list. The idea was to go through and sort the list by last name, and then go through again and sort that list by first name. I am not sure at all how to do this at all. I had attempted to do this by using the for loop and by comparing the last name of the current student to that of the previous.

    Code:
    prev->last_name=next;
    156 G:\School\MATH 202\Homework\Student.c [Warning] assignment from incompatible pointer type

    Same as above

    Code:
    next = temp->last_name;

    162 G:\School\MATH 202\Homework\Student.c [Warning] assignment from incompatible pointer type

    Same as above

    171 G:\School\MATH 202\Homework\Student.c [Warning] assignment from incompatible pointer type

    Same as above

    The two main problems I am having is that I am trying to read this first number in and I am not sure why the fscanf is incorrect in the context that I am using it. The second problem is the way that I could sort through these students. I had thought that it would be possible to delete the other elements of the structure except for the last name, but the problem with that theory is that I want those elements to be printed out after I go through the sort. I know that to start out the sort that I will need a for statement as well as comparison, but I am not sure about exactly where to go from there. If anyone has anyone comment or advice to give about how I could go about doing these tasks, just leave a comment. i am not sure what these errors mean, and I am not sure how to change them to work in the program correctly. Thanks!

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by crimsonpetal13 View Post
    Code:
               fscanf(filename, "%d",&z);
    Are you sure you are getting an error for that? The previous post you only had one such error, which would definitely be the one I pointed out.

    150 G:\School\MATH 202\Homework\Student.c incompatible types in assignment

    Here as as well as the other error messages throughout the rest of program are because I was attempting to compare the different last names of the linked list. The idea was to go through and sort the list by last name, and then go through again and sort that list by first name. I am not sure at all how to do this at all. I had attempted to do this by using the for loop and by comparing the last name of the current student to that of the previous.

    Code:
    prev->last_name=next;
    The problem is you are not comparing the last names. You are comparing the last name in prev to next itself (a pointer). You probably want something more like:

    Code:
    prev->last_name=next->last_name;
    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

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    Thanks! it has helped. I will get back on if I run into any more problems that I can't figure out.

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by MK27 View Post
    Code:
    fscanf(filename, "%d",&z);
    Are you sure you are getting an error for that? The previous post you only had one such error, which would definitely be the one I pointed out.
    Code:
    char filename[80];
    I'm pretty sure it's an error since filename is not a FILE*

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    Thanks for all of your help so far! In the main project of my program I have just now written the switch statement for the user to choose whether they want to create a new linked list or load one in. I have been compiling constantly and I have not run into any errors through writing the program, but when I have tried to run the program it is not working correctly. For example...

    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);
    Code:
    switch(choice)  //Switch Statement
        {
                      case '1':
    Code:
    case '2':
                     {
    Code:
    default:
    {
    I have tried to input the number one in the program and it ran what the default is supposed to be. I have done the same thing with the number two. I have worked with switch statements before, and i cannot seem to find why it is doing it.

  12. #12
    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

  13. #13
    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

  14. #14
    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

  15. #15
    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

Page 1 of 2 12 LastLast
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, 02: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21