Thread: Functions and Structs...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    Unhappy Functions and Structs...

    So, I'm debugging my awesome program. Basically, the sub-problem I am working on now is to get everyone's GPA's. You are given an input file that gives you the names, 'action', credit hours for a class, and the letter grade the student recieved for a class. So, for example, one line of the input file would be:

    SMITH JESSICA TOOK_CLASS 4 A

    Meaning Jessica Smith took a class worth 4 credit hours, and got an A in it. In order to calculate the GPA, you must multiply the credit hours by the value of the grade (4 for an A, 3 for a B, etc) then divde by the total number of credit hours. There are a couple of actions: 'TOOK_CLASS' (add in credit hours and grade), 'GRADE_FORGIVE' (this 'erases' the class, so subtract credit hours and grade), 'CHANGE_GRADE' (changes a grade from an old one to a new one, so subtract old grade points and add in new ones), 'REMOVE' (which removes the student from the 'system' entirely. I've done this by setting that person's credit hours = -1, and the program will not print for credit hours <= 0. I know this is not 'correct', but the point is to make the program do it's job).

    Here is my problem : my TOOK_CLASS if statement works fine. I end up with the correct number of credits for those, but if I try to add and subtract hours, (GRADE_FORGIVE or CHANGE_GRADE) well... it just doesn't happen. I've ended up with the wrong number of credits. Also, my 'REMOVE' statement isn't working... it's as if the program skips right over it. Hopefully, you'll understand what I mean conceptually. This is a lot of code, and there are lots of other function calls I make. But I'll be hanging around if you have questions for me, but I just don't understand why it's not working. Thanks

    Code:
    //This function will read in the file students.in
    //and store the information in struct class student[].
    //It will then return the number of students.
    
    int readfile(FILE *ifp, struct class student[])
    {
       int num, index, i, hours, grade, newgrade, counter=0;
       char first[20];
       char last[20];
       char tempgrade[2];
       char tempnewgrade[2];
    
       //Read in number of records.
       fscanf(ifp, "%d", &num);
    
       //Read in each record one by one.
       for (index=0; index<num; index++)
       {
    
         //Scan in the name from the file.
         fscanf(ifp, "%s", last);
         fscanf(ifp, "%s", first);
    
         //Check for previous records for this student.
         //Returns the index of where student is stored in struct.
         i = search(student, num, last, first);
       if (i < 0) //Enter in a new student.
           {
              counter++;
              strcpy( student[index].last, last);
              strcpy( student[index].first, first);
              fscanf(ifp, "%s", student[index].action);
    
                 //If they are being removed, set credit hours to -1
                 if( strcmp(student[index].action, "REMOVE") == 0)
                   {
                      counter--;
                      student[index].hours = -1;
    
                   }
                 if( strcmp(student[index].action, "TOOK_CLASS") == 0 && student[index].hours >= 0)
                   {
                   fscanf(ifp, "%d", &hours);
                   fscanf(ifp, "%s", tempgrade);
    
                     //Add in hours and grade points to running total.
                     student[index].hours += hours;
                     student[index].grade += (getgrade(tempgrade[0])*hours);
                   }
    
                 //If they are doing grade forgiveness, remove hours and grade.
                 if( strcmp(student[index].action, "GRADE_FORGIVE") == 0 && student[index].hours >= 0)
                 {
                   fscanf(ifp, "%d", &hours);
                   fscanf(ifp, "%s", tempgrade);
    
                     //Add in hours and grade points to running total.
                     student[index].hours -= hours;
                     student[index].grade -= (getgrade(tempgrade[0])*hours);
                 }
    
                 //If they are changing their grade, scan in hours,
                 // old grade, and new grade.
    
                 if( strcmp(student[index].action, "CHANGE_GRADE")== 0)
                   {
                   fscanf(ifp, "%d", &hours);
     fscanf(ifp, "%s", tempgrade);
                   fscanf(ifp, "%s", tempnewgrade);
    
                     //Subtract the old points and add in the new ones.
                     student[index].grade -= (getgrade(tempgrade[0])*hours);
                     student[index].grade += (getgrade(tempnewgrade[0])*hours);
    
                   }
            }
    
           else //Otherwise, add information to student.
    
            {
             fscanf(ifp, "%s", student[index].action);
    
    
    
                 //If they are being removed, credit hours set to a negative number.
                 if( strcmp(student[index].action, "REMOVE") == 0)
                      student[i].hours = -1;
     //If they are taking a class, add in the hours and grade.
                 if( strcmp(student[index].action, "TOOK_CLASS") == 0 && student[i].hours >= 0)
                   {
                   fscanf(ifp, "%d", &hours);
                   fscanf(ifp, "%s", tempgrade);
    
                     //Add in hours and grade points to running total.
                     student[i].hours += hours;
                     student[i].grade += (getgrade(tempgrade[0])*hours);
                   }
    
                 //If they are doing grade forgiveness, remove hours and grade.
                 if( strcmp(student[index].action, "GRADE_FORGIVE") == 0 && student[i].hours >= 0)
                 {
                   fscanf(ifp, "%d", &hours);
                   fscanf(ifp, "%s", tempgrade);
    
                     //Add in hours and grade points to running total.
                     student[i].hours -= hours;
                     student[i].grade -= (getgrade(tempgrade[0])*hours);
                 }
                 //If they are changing their grade, scan in hours,
                 // old grade, and new grade.
    
                 if( strcmp(student[index].action, "CHANGE_GRADE")== 0 && student[i].hours >= 0)
                   {
                   fscanf(ifp, "%d", &hours);
                   fscanf(ifp, "%s", tempgrade);
                   fscanf(ifp, "%s", tempnewgrade);
    
                     //Subtract out the points from the old grade and
                     //add in points for new one. Do nothing with the
                     //hours because we assume it was already added in
                     //previously with TOOK_CLASS.
                       student[i].grade -= (getgrade(tempgrade[0])*hours);
                       student[i].grade += (getgrade(tempnewgrade[0])*hours);
    
    
                   }
    
             }
       }
    
    
     return counter;
    
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    struct class student[]
    Eh?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs, pointers and functions
    By osici in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:35 AM
  2. Passing structs to callback functions (GTK+ related)
    By Raskalnikov in forum C Programming
    Replies: 2
    Last Post: 03-21-2009, 12:46 PM
  3. Pointers, structs, and functions, oh my!
    By funkydude9 in forum C++ Programming
    Replies: 8
    Last Post: 07-30-2003, 12:51 AM
  4. passing structs to functions?
    By Neildadon in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2002, 04:31 PM
  5. probs with structs and functions
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 05-08-2002, 11:52 PM