Thread: Linked Lists with multiple programs

  1. #16
    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

  2. #17
    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.

  3. #18
    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

  4. #19
    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.

  5. #20
    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.

  6. #21
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    I have been compiling with warnings on, but when I typed the code in last night onto here, I forgot to include the fp.

    Here is the code I have that won't work, copied straight from the code.

    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", studentnumber);     
               
               printf("First Name\tMiddle Name\tLast Name\t");
               printf("Street\tCity\tState\tZipcode\tHours\tFees Paid\n\n\n");
               
               temp = root;

  7. #22
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    I have worked a lot on my code and have fixed the problem of scanning in the number of students, but I am still having difficulty with scanning in the information of the file.

    Code:
    void LoadStudent(int studentnumber, StudentLink *root)            //Loads a file of student records
    {
         StudentLink *temp, *prev, *next;
         FILE *fp;
         char filename[80];
         int counter;
         char line[80];
         int stream;
    
         
         printf("Enter the file name: ");          //Filename to be opened
         scanf("%s", filename);
         
    
    
    
         fp = fopen(filename, "r+");
         
         if(fp != NULL)  //If the file does open
         {
               printf("You have successfully opened the file!\n");    
               fscanf(fp, "%d", &studentnumber);
               printf("The number of students on the file is %d\n\n\n", studentnumber);     //First number in the file should be the number of students
               
               
               printf("First Name Middle Name Last Name\t");               //Headers for style
               printf("Street City State Zipcode\tHours\tFees Paid\n\n\n");
               
               root = NULL;       //Sets the root to null
               prev = NULL;       //Sets the prev to null
    
               
               for(counter = 0; counter < studentnumber; counter++)           //counting up through the students
               {
                           fscanf(fp, "%s", temp->first_name);
                           fscanf(fp, "%s", temp->middle_name);
                           fscanf(fp, "%s", temp->last_name);
                           fscanf(fp, "%s", temp->street);
                           fscanf(fp, "%s", temp->city);
                           fscanf(fp, "%s", temp->state);
                           fscanf(fp, "%d", &(temp->zipcode));
                           fscanf(fp, "%d", &(temp->hours));
                           fscanf(fp, "%d", &(temp->feespaid));
                           
                                        if (root == NULL) 
                                        {
                                                 root = temp;         //Assigns root as temp
                                        }
                                        if (prev != NULL)
                                        { 
                                                 prev->next = temp;   //Assigns prev as next                       
                                        }
                           temp = temp->next;       //Move on to the next student
               }
               
               fclose(fp);      //Close the file
         }

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