sequential file program

This is a discussion on sequential file program within the C Programming forums, part of the General Programming Boards category; I'll be testing it with the trans and data file you previously supplied. Any changes made (such as removing the ...

  1. #61
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'll be testing it with the trans and data file you previously supplied. Any changes made (such as removing the commas), will be shown on the file when I post it back up to the forum.

    Yes, other changes are being made. All initial input is from the data and transaction files.

    More, later today.

  2. #62
    Registered User
    Join Date
    May 2008
    Posts
    61
    sounds great, can't wait to see it Adak, thanks again

  3. #63
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Unfortunately, the program is not cooperating.

    What I have is unfinished, right in the very spot that I most wanted to finish. I had to throw out the switch statements you'd worked on, because the assignment was much too complex for it.

    See if it compiles OK for you, and runs about right, (minus the unfinished part, of course), and is your understanding of the assignment. I'm not sure of the scanf parts for new records (type 0 transactions). I can't work on it anymore today, but tomorrow I'll give it a shot.

    The latest version:
    Code:
    /*Modifications to a program posted by IneedHelpBad */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Student {
        int  key;
        char name [7];
        int  test1, test2, test3;
        int  hwCount;
        int  hwPoints;
    } 
    dummy = {-1, "", 0, 0, 0, 0, 0},
    * aStudent;
    
    struct Transaction {
       int type;		
       int key;		
       int value;	
       char name [7];
    };
    
    
    FILE * dataFile, * recordFile, * transFile;
    
    int openFiles ( ) {
        if ((dataFile = fopen ("seq_data.txt", "r")) == NULL) {
    	   fprintf (stderr, "Error in opening input file\n");
    	   return 1;
        }
        if ((transFile = fopen ("seq_tran.txt", "r")) == NULL) {
    	   fprintf (stderr, "Error in opening transaction file\n");
    	   return 1;
        }
    
    
        if ((recordFile = fopen ("seq_recs.txt", "w+b")) == NULL) {
    	   fprintf (stderr, "Error in opening record file\n");
    	   return 1;
        }
        return 0;
    }
    
    void closeFiles ( ) {
        fclose (dataFile);
        fclose (recordFile);
        fclose(transFile);
    }
    
    struct Student * getOneStudentFromData ( ) {
       struct Student * aStudentPtr;
       int theId, theTest1, theTest2, theTest3, theHwCount, theHwPoints;
       char theName [7];
       char cr;
       fscanf (dataFile, "&#37;d%s%d%d%d%d%d%c",
    	&theId, theName, &theTest1, &theTest2, &theTest3,
    	&theHwCount, &theHwPoints, &cr);
    
       if(theId > -1)   {
          aStudentPtr = (struct Student *) malloc (sizeof (struct Student));
          aStudentPtr->key = theId;
          aStudentPtr->test1 = theTest1;
          aStudentPtr->test2 = theTest2;
          aStudentPtr->test3 = theTest3;
          aStudentPtr->hwCount = theHwCount;
          aStudentPtr->hwPoints = theHwPoints;
          strcpy (aStudentPtr->name, theName);    
       }
       else 
          aStudentPtr = &dummy;
    
       return aStudentPtr;
    }
    struct Transaction * getOneTransFromData ( ) {
       struct Transaction * aTransPtr;
       
       int Ttype, Tkey, Tvalue;
       char Tname [7] = { '\0' };
       char cr;
       fscanf (transFile, "%d%d%d%s%c", &Ttype, &Tkey, &Tvalue, Tname, &cr);
    
       //if(Ttype != -1)   {
          aTransPtr = (struct Transaction *) malloc (sizeof (struct Transaction));
          aTransPtr->type = Ttype;
          aTransPtr->key = Tkey;
          aTransPtr->value = Tvalue;
          strcpy (aTransPtr->name, Tname);    
       //}
       //else 
       //  aTransPtr = &dummy2;
    
       return aTransPtr;
    }
    void writeRecord (struct Student * theStudentPtr) {
       fwrite (theStudentPtr, sizeof (struct Student), 1, recordFile);
    }
    int readDataAndWriteRecords (void) {
       char newName[7] = {'\0'};
       int newKey, getStudent, getTrans, transSeen, score[4], hwcount, hwscore;
       int transError, test, gar;
       static int recordsSeen = 0;	
       struct Student * theStudentPtr;
       struct Student * newStudentPtr;
       struct Transaction * theTransPtr;
       
       theTransPtr = (struct Transaction *) malloc (sizeof (struct Transaction));
       theStudentPtr = (struct Student *) malloc (sizeof (struct Student));
       newStudentPtr = (struct Student *) malloc (sizeof (struct Student));
    
       transSeen = transError = newKey = 0;
       getStudent = getTrans = 1;
    //88888888  This is the unfinished part 888888888888
    
       do {
          theStudentPtr = getOneStudentFromData();
          if(theStudentPtr->key == 0)  {
             writeRecord (theStudentPtr);
             //getStudent = 0;
          }
          else
             recordsSeen++;
          
          if(getTrans == 1)  {
             theTransPtr = getOneTransFromData();
             if(theTransPtr->type < 0)  {
                gar++;
                getTrans = 0;             //get no more transactions
             }
             else
                transSeen++;
          }
          //if(getStudent && getTrans)  {
             if(theStudentPtr->key < theTransPtr->key)
                writeRecord (theStudentPtr);
             else if(theTransPtr->key < theStudentPtr->key) {
                switch (theTransPtr->type)   {
                   case 0: 
                     getStudent = 0; getTrans = 1;
                     printf("\n Enter New Student's Name: ");
                     scanf("%7s", newName);
                     gar = getchar();
                     printf("\n Their temporary key is: %c", newKey++);         
                     for(test = 1; test < 4; test++)  {
                       printf("\n Enter the score for test number: %d", test); 
                       scanf("%d", score[test]);  //1-3 only
                       gar = getchar();
                       if(score[test] < 0 || score[test] > 120)  {
                          printf("\n Test Score is out of range, please re-enter");
                          test--;
                       }
                     }
                     do  {
                       printf("\n Enter the homework count number");
                       scanf("%d", &hwcount);
                       gar = getchar();
                       if(hwcount < 0 || hwcount > 10)
                          printf("\n Count is out of range - please re-enter");
                     }while(hwcount < 0 || hwcount > 10);
                     do  {
                       printf("\n Enter the homework score for this record");
                       scanf("%d", &hwscore);
                       gar = getchar();
                       if(hwscore < 0 || hwscore > 50)
                          printf("\n Score is out of range - please re-enter");
                     }while(hwscore < 0 || hwscore > 50);
    
                     newStudentPtr->key = newKey;
                     newStudentPtr->test1 = score[1];
                     newStudentPtr->test2 = score[2];
                     newStudentPtr->test3 = score[3];
                     newStudentPtr->hwCount = hwcount;
                     newStudentPtr->hwPoints = hwscore;
                     strcpy (newStudentPtr->name, newName);    
                     writeRecord (newStudentPtr);
                     break;
                   case 1: case 2: case 3: case 4: case 5:
                     transError++;
                     printf("\n Error - that record is not available, hit Enter to Continue");
                     gar = getchar();
                     break;
                }//end of switch
             }//end of else if
          //}
             else if(theTransPtr->key == theStudentPtr->key) {
                switch (theTransPtr->type)   {
                   case 0:
                     transError++;
                     if(strcmp(theTransPtr->name, theStudentPtr->name) == 0)  
                       printf("\n Error: The record for %s already exists", theTransPtr->name);
                     else
                       printf("\n Error: That record's key number is already assigned");
    
                     printf("\n Press Enter to Continue");
                     gar = getchar();
                     break;
                   case 1:
                     theStudentPtr->test1 += theTransPtr->value;   
                     if(theStudentPtr->test1 > 120)
                       transError++;
                     break;
                   case 2:
                     theStudentPtr->test2 += theTransPtr->value;
                     if(theStudentPtr->test2 > 120)
                       transError++;
                     break;
                   case 3:
                     theStudentPtr->test3 += theTransPtr->value;
                     if(theStudentPtr->test3 > 120)
                       transError++;
                     break;
                   case 4:
                     theStudentPtr->hwCount++;
                     theStudentPtr->hwPoints += theTransPtr->value;
                     if(theStudentPtr->hwCount > 10 || theStudentPtr->hwPoints > 50) 
                       transError++;
                     break;
                   case 5:
                     theStudentPtr->hwPoints += theTransPtr->value;
                     if(theStudentPtr->hwPoints > 5 * theStudentPtr->hwCount)
                       transError++;
                     break;
                }//end of switch
             }//end of else if(key == key)
    
    	   writeRecord (theStudentPtr);
       } while (theStudentPtr->key != 0);
    //888888888888  End of unfinished part of function 88888888888
    
       free(theStudentPtr);
       free(theTransPtr);
       free(newStudentPtr);  
       printf("\n %d Transactions were processed, with %d errors", transSeen, transError);
       gar++;     //prevents a nuisance warning on my compiler
       return recordsSeen;
    }
    
    int readRecordsAndPrintData ( ) {
       static int recordsSeen = 0;
       int total;
       float percent;
       struct Student * myStudentPtr;
    
       myStudentPtr = (struct Student *) malloc (sizeof (struct Student));
       putchar('\n');
       printf("\nKey\tName\tTotal Points\tPercent of 350 Points\n");
       printf("=============================================================\n");
       do {
       	fread (myStudentPtr, sizeof (struct Student), 1, recordFile);
    
          if (myStudentPtr->key != 0) {
             total = 0;
             total = myStudentPtr->test1+myStudentPtr->test2+myStudentPtr->test3;
             if(myStudentPtr->hwPoints > 5 * myStudentPtr->hwCount)
                myStudentPtr->hwPoints = 5 * myStudentPtr->hwCount;
    
             total += myStudentPtr->hwCount + myStudentPtr->hwPoints;
             //(float) total;
             percent = (total/350.0) * 100;
    
    	      printf ("%d\t%s\t    %d\t\t        %.2f%\n",
             myStudentPtr->key,
    		   myStudentPtr->name,
    		   //myStudentPtr->test1,
    		   //myStudentPtr->test2,
             //myStudentPtr->test3,
    		   //myStudentPtr->hwCount,
    		   //myStudentPtr->hwPoints, 
             total, percent);
             recordsSeen++;
    	   }	
       } while (myStudentPtr->key != 0);
    
       free(myStudentPtr);
       return recordsSeen;
    }
    
    int main ( ) {
       int i, numRecords, gar;
    
       for(i = 0; i < 10; i++)
          putchar('\n');
    
       if (openFiles ( )) 
    	   return 1;
        
       numRecords = 0;
       numRecords = readDataAndWriteRecords ( );
       printf ("\n %d records read from data file\n", numRecords);
    
       fseek (recordFile, 0, SEEK_SET);
    
       numRecords = 0;
       numRecords = readRecordsAndPrintData ( );
       printf ("\n %d records read from record file\n", numRecords);
    
       closeFiles ( );
       printf("\n                   Program Complete - Press Enter When Ready");
       gar = getchar(); gar++;
       return 0;
    }
    
    The Data I used:
    100     Jeff    98 76 86         8       41
    101     Mark    66 51 76         4       31
    102     Jenn    79 89 93         9       44
    103     Chris   44 78 45         3       28
    104     Matt    74 81 88         8       39
    105     Dan     67 91 83         6       24
    106     Dave    55 81 75         7       45
    107     Rich    91 93 89         9       32 
    108     Mike    55 61 78         3       26
    109     Tony    67 68 78         5       45
    110     Rob     89 71 80         7       42
    111     Sonny   78 81 89         8       15
    112     Luke    91 83 75         8       24
    113     April   66 79 70         6       35
    114     Sheryl  89 81 85         9       50
    115     Tom     79 61 90         5       33
    116     Lamont  80 76 89         7       41
    117     Steve   90 98 100        10      50
    118     Jake    90 78 69         8       25
    219     Austin  91 45 88         7       29
    0       ""      0  0  0          0       0
    
    And this transaction file:
    3	100	5	Jeff	
    4	101	5	Mark	
    1	102	3	Jenn	
    2	103	5	Chris	
    4	104	4	Matt	
    3	105	3	Dan	
    0	106	4	Dave	
    5	107	14	Rich	
    3	108	2	Mike
    1	109	4       Tony	
    0	110	5	Rob	
    2	111	-4	Sonny	
    3	112	4	Luke	
    4	113	3	April	
    2	114	9	Sheryl	
    4	115	4	Tom	
    5	116	18	Lamont	
    0	117	3	Steve	
    2	118	1	Jake	
    3	119	3	Austin	
    -1	0	0	0
    0	0	0	0
    Last edited by Adak; 06-05-2008 at 08:18 AM.

  4. #64
    Registered User
    Join Date
    May 2008
    Posts
    61
    This is some great work Adak, truely. Thanks.

    Yes it does compile for me, and obviously as you said it won't all work due to the transaction parts, so when ran, there is about 10 lines of empty space (not sure from which function but jsut looks like repeated \n's) and then it begins with the:

    Code:
     Error: The record for Dave already exists
     Press Enter to Continue
    
     Error: The record for Rob already exists
     Press Enter to Continue
    
     Error: The record for Steve already exists
     Press Enter to Continue
    
     Error - that record is not available, hit Enter to Continue
    
     20 Transactions were processed, with 6 errors
     20 records read from data file
    
    
    Key     Name    Total Points    Percent of 350 Points
    =============================================================
    100     Jeff        313                 89.43&#37;
    101     Mark        223                 63.71%
    102     Jenn        317                 90.57%
    103     Chris       190                 54.29%
    104     Matt        295                 84.29%
    105     Dan         274                 78.29%
    106     Dave        253                 72.29%
    107     Rich        327                 93.43%
    108     Mike        214                 61.14%
    109     Tony        247                 70.57%
    110     Rob         282                 80.57%
    111     Sonny       267                 76.29%
    112     Luke        285                 81.43%
    113     April       257                 73.43%
    114     Sheryl      318                 90.86%
    115     Tom         266                 76.00%
    116     Lamont      287                 82.00%
    117     Steve       348                 99.43%
    118     Jake        271                 77.43%
    219     Austin      260                 74.29%
    
     20 records read from record file
    
                       Program Complete - Press Enter When Ready
    Utterly perfect program other than those few errors, again, amazing work Adak.

    Is it only the type zero functions that aren't working properly? everything else is working perfectly?

    Clearly your skills in this are far and away a million times better than mine, but conceptually, it doesn't seem like the type 0 should give much trouble since its just asking the user for a new name and key (which could be just the next in line key? Id think? and then simply setting all the other values to zero. I don't know how I would do it but after looking at you're work and in that area, I am not seeing what the issue may be.. Sorry I am truly of no help heh.

    I can see you're workings of asking the user for the new name, but then I see asking for the test scores and such, I thought they were supposed to be set to zero? or are they just set to zero by default and the user enters them in from there, which it seems you are trying to do.

    and this has an address for dummy2 but you don't see it anywhere else in the program, so I assume this is code you're still working on (esp. since its negated with the //'s)?
    Code:
       //if(Ttype != -1)   {
          aTransPtr = (struct Transaction *) malloc (sizeof (struct Transaction));
          aTransPtr->type = Ttype;
          aTransPtr->key = Tkey;
          aTransPtr->value = Tvalue;
          strcpy (aTransPtr->name, Tname);    
       //}
       //else 
       //  aTransPtr = &dummy2;
    aaaand another thing (I know, broken record, sorry..) but what does this do;
    Code:
       free(theStudentPtr);
       free(theTransPtr);
       free(newStudentPtr);
    Does that just wipe it clean of all the information?


    and lastly, just out of curiosity, which compiler are you using?
    Last edited by needhelpbad; 06-05-2008 at 02:55 PM.

  5. #65
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by needhelpbad View Post
    This is some great work Adak, truely. Thanks.

    Yes it does compile for me, and obviously as you said it won't all work due to the transaction parts, so when ran, there is about 10 lines of empty space (not sure from which function but jsut looks like repeated \n's) and then it begins with the:

    Code:
     Error: The record for Dave already exists
     Press Enter to Continue
    
     Error: The record for Rob already exists
     Press Enter to Continue
    
     Error: The record for Steve already exists
     Press Enter to Continue
    
     Error - that record is not available, hit Enter to Continue
    
     20 Transactions were processed, with 6 errors
     20 records read from data file
    
    
    Key     Name    Total Points    Percent of 350 Points
    =============================================================
    100     Jeff        313                 89.43%
    101     Mark        223                 63.71%
    102     Jenn        317                 90.57%
    103     Chris       190                 54.29%
    104     Matt        295                 84.29%
    105     Dan         274                 78.29%
    106     Dave        253                 72.29%
    107     Rich        327                 93.43%
    108     Mike        214                 61.14%
    109     Tony        247                 70.57%
    110     Rob         282                 80.57%
    111     Sonny       267                 76.29%
    112     Luke        285                 81.43%
    113     April       257                 73.43%
    114     Sheryl      318                 90.86%
    115     Tom         266                 76.00%
    116     Lamont      287                 82.00%
    117     Steve       348                 99.43%
    118     Jake        271                 77.43%
    219     Austin      260                 74.29%
    
     20 records read from record file
    
                       Program Complete - Press Enter When Ready
    Utterly perfect program other than those few errors, again, amazing work Adak.

    Is it only the type zero functions that aren't working properly? everything else is working perfectly?

    Clearly your skills in this are far and away a million times better than mine, but conceptually, it doesn't seem like the type 0 should give much trouble since its just asking the user for a new name and key (which could be just the next in line key? Id think? and then simply setting all the other values to zero. I don't know how I would do it but after looking at you're work and in that area, I am not seeing what the issue may be.. Sorry I am truly of no help heh.

    I can see you're workings of asking the user for the new name, but then I see asking for the test scores and such, I thought they were supposed to be set to zero? or are they just set to zero by default and the user enters them in from there, which it seems you are trying to do.

    and this has an address for dummy2 but you don't see it anywhere else in the program, so I assume this is code you're still working on (esp. since its negated with the //'s)?
    Code:
       //if(Ttype != -1)   {
          aTransPtr = (struct Transaction *) malloc (sizeof (struct Transaction));
          aTransPtr->type = Ttype;
          aTransPtr->key = Tkey;
          aTransPtr->value = Tvalue;
          strcpy (aTransPtr->name, Tname);    
       //}
       //else 
       //  aTransPtr = &dummy2;
    aaaand another thing (I know, broken record, sorry..) but what does this do;
    Code:
       free(theStudentPtr);
       free(theTransPtr);
       free(newStudentPtr);
    Does that just wipe it clean of all the information?


    and lastly, just out of curiosity, which compiler are you using?
    Oddly, those "Error: " messages, are required by the assignment. The part where it's asking for the student's name, etc., IS wrong, and will be removed. Seemed like a good idea (well, actually it is), but not according to the assignment sheet.

    They test values are indeed supposed to be set to zero for a new record (type 0) transaction. But then the assignment sheet goes on to say the values for that record are to be filled in. I just envisioned how it would do that, the wrong way.

    The part of the program that's just wrong is the way the transaction file data is handled. Right now, it's pretty much ignored.

    Yes, the //stuff is just remarked out, and doesn't compile. I was using Turbo C ver. 1.01, but I've found a bug in the compiler I can't work around, so I'm changing over to Microsoft Visual C Express Edition (Free download), ver. 8, from now on.

    If you don't have it, google it, and use it, is my recommendation. You can well forget the other microsoft software they want to also download to your system, IMO.

    The code with free(Some Memory that was malloc'd), is needed to stop a "memory leak". If you don't free memory you malloc, when you exit the function that malloc'd it, you will lose that memory, until the program is over. Very bad form not to free your malloc'd memory. If the program runs long enough, it will completely crash because it has no more memory.

    The next in line key for a new record? Nyet! . Next key is probably being used.

  6. #66
    Registered User
    Join Date
    May 2008
    Posts
    61
    Hah, I had to chuckle at the Nyet, might be the first time I've seen that, heard or read :P

    I figured it would just tack the key on the bottom adding it as just a new record to the list on the bottom.

    I use SSH Secure Shell, any thoughts on whether thats a good compiler or not?

    So currently how this program is running you're saying is that it is basically ignoring the transaction file, so is it just grabbing the types from it? or the types and names? or it in fact is not being touched whatsoever?

  7. #67
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by needhelpbad View Post
    Hah, I had to chuckle at the Nyet, might be the first time I've seen that, heard or read :P

    I figured it would just tack the key on the bottom adding it as just a new record to the list on the bottom.

    I use SSH Secure Shell, any thoughts on whether thats a good compiler or not?

    So currently how this program is running you're saying is that it is basically ignoring the transaction file, so is it just grabbing the types from it? or the types and names? or it in fact is not being touched whatsoever?
    Problem is, it hasn't gotten to the bottom yet, and couldn't possibly know without looking ahead and running through the file, what keys were available. Assignment sheet says he wants new name to be "name" and key, "key", so I'm using the first hundred numbers (between 0 and 100) for new temporary keys.

    SSH is not a compiler to my knowledge ->Nyet! gcc, g++, maybe? It's not ignoring the transaction file, it just isn't getting output from it into the records file, yet. The logic, what logic?, stinks.

    So I've downloaded 297 MB's for Microsoft's Visual C++. Do you think those morons would be so kind as to put a lousy one little icon to start the damn thing, on my desktop?

    Nyet, Nyet!

    They did install it in the program list, at least, so all is well. I was used to MS Visual Studio 6, which gave you an icon, and I expected the same from MS Visual C++.
    Last edited by Adak; 06-06-2008 at 03:23 AM.

  8. #68
    Registered User
    Join Date
    May 2008
    Posts
    61
    So do you have any idea how this could even be worked out with the transaction file getting its output into the record file?

    and clearly Microsoft has no sense, look at Vista!

  9. #69
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sure do. I'm working on it right now, actually. I ran out of time yesterday, because it takes so long to understand the rambling and scattered details of the assignment, itself. Plus, no examples, and obviously I never heard him lecture about this.

    He's so curt with what he wants and he has so many details, that sometimes I took his meaning to be something not quite right. Naturally, I discover this after a good deal of wasting time. He's a very poorly organized writer.

    I'm not changing my compiler, after all. Well, I did change it on one computer, (to MS Visual Express, which isn't quite as stupid as I had thought), but not the one I'm programming on. The compiler "error", on Turbo C++, was just my unfamiliarity with fwrite(), apparently.

    I'll post up what I've got, later today (early pm I expect).

    Getting nervous?

  10. #70
    Registered User
    Join Date
    May 2008
    Posts
    61
    haha nervous? nyet! nothing to be nervous about. I know you're coding will be perfect and I will just gawk at it until I understand every aspect of it

    and yea, the assignment wasn't even explained, it was just given to us. You can understand my confusion and troubles with it i suppose

  11. #71
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is the latest version. No bug spray has been applied, though. I can almost guarantee that it is not accurate, so please put your rose colored glasses aside.

    Please make sure that it compiles OK, and looks OK when you run it. Big errors will be in readDataAndWriteRecords(), of course.

    The logic requires that all transactions will have a valid key number, including type 0 (insert new records). I can't wrap my head around keeping a database sorted by key #, if new records have no key # (and no name, as well).

    If you could describe any bugs you find, that would be great. This uses the same input files as I posted, above.

    I had to add some extra line breaks into this post to avoid "breaking the forum tables". You'll find them OK, most are error messages.

    This was the worst written, and most detailed assignment, I've ever seen. Whoever wrote it knows their stuff imo, but needs a copy writer, *badly*. There are details about the transaction on new records, that I never did fully understand.

    Code:
    /*Modifications to a program posted by IneedHelpBad */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Student {
        int  key;
        char name [7];
        int  test1, test2, test3;
        int  hwCount;
        int  hwPoints;
    } 
    dummy = {-1, "", 0, 0, 0, 0, 0},
    * aStudent;
    
    struct Transaction {
       int type;		
       int key;		
       int value;	
       char name [7];
    };
    
    
    FILE * dataFile, * recordFile,  * transFile; //* recordFile2
    
    int openFiles ( ) {
        if ((dataFile = fopen ("seq_data.txt", "r")) == NULL) {
    	   fprintf (stderr, "Error in opening input file\n");
    	   return 1;
        }
        if ((transFile = fopen ("seq_tran.txt", "r")) == NULL) {
    	   fprintf (stderr, "Error in opening transaction file\n");
    	   return 1;
        }
        if ((recordFile = fopen ("seq_recs.txt", "w+t")) == NULL) {
    	   fprintf (stderr, "Error in opening record file\n");
    	   return 1;
        }
        return 0;
    }
    
    void closeFiles ( ) {
        fclose (dataFile);
        fclose (recordFile);
        fclose(transFile);
    }
    
    struct Student * getOneStudentFromData ( ) {
       struct Student * aStudentPtr;
       int theId, theTest1, theTest2, theTest3, theHwCount, theHwPoints;
       char theName [7];
       char cr;
       fscanf (dataFile, "&#37;d%s%d%d%d%d%d%c",
    	&theId, theName, &theTest1, &theTest2, &theTest3,
    	&theHwCount, &theHwPoints, &cr);
    
       if(theId > -1)   {
          aStudentPtr = (struct Student *) malloc (sizeof (struct Student));
          aStudentPtr->key = theId;
          aStudentPtr->test1 = theTest1;
          aStudentPtr->test2 = theTest2;
          aStudentPtr->test3 = theTest3;
          aStudentPtr->hwCount = theHwCount;
          aStudentPtr->hwPoints = theHwPoints;
          strcpy (aStudentPtr->name, theName);    
       }
       else 
          aStudentPtr = &dummy;
    
       return aStudentPtr;
    }
    struct Transaction * getOneTransFromData ( ) {
       struct Transaction * aTransPtr;
       int Ttype, Tkey, Tvalue;
       char Tname [7] = { '\0' };
       char cr;
       fscanf (transFile, "%d%d%d%s%c", &Ttype, &Tkey, &Tvalue, Tname, &cr);
    
       aTransPtr = (struct Transaction *) malloc (sizeof (struct Transaction));
       aTransPtr->type = Ttype;
       aTransPtr->key = Tkey;
       aTransPtr->value = Tvalue;
       strcpy (aTransPtr->name, Tname);    
       
       return aTransPtr;
    }
    void writeRecord (struct Student * theStudentPtr) {
       fprintf(recordFile, "%d\t%s\t%d\t%d\t%d\t%d\t%d\n", theStudentPtr->key, 
          theStudentPtr->name, theStudentPtr->test1, theStudentPtr->test2, 
          theStudentPtr->test3, theStudentPtr->hwCount, theStudentPtr->hwPoints);    
    }
    int readDataAndWriteRecords (void) {
       char newName[7] = {'\0'};
       int key0, getTrans, getStudent, transSeen, hwcount, hwscore;
       int i, transError, points, gar;
       int recordsSeen = 0;	
       struct Student * theStudentPtr;
       struct Student * newStudentPtr;
       struct Transaction * theTransPtr;
       
       theTransPtr = (struct Transaction *) malloc (sizeof (struct Transaction));
       theStudentPtr = (struct Student *) malloc (sizeof (struct Student));
       newStudentPtr = (struct Student *) malloc (sizeof (struct Student));
    
       transSeen = transError = 0;
       getStudent = getTrans = 1;
    
       do {
          if(getStudent == 1)
             theStudentPtr = getOneStudentFromData();
          if(theStudentPtr->key == 0)  {   
             getStudent = 0;
          }
          else
             recordsSeen++;
          
          if(getTrans == 1)  {
             theTransPtr = getOneTransFromData();
             if(theTransPtr->type < 0)  {
                getTrans = 0;             //get no more transactions
             }
             else
                transSeen++;
          }
          if(theStudentPtr->key > 1 && theTransPtr->key < 0)  
             continue;
          
          if(theStudentPtr->key < theTransPtr->key)
             writeRecord (theStudentPtr);
          else if(theTransPtr->key < theStudentPtr->key) {
             switch (theTransPtr->type)   {
                case 0: 
                   //assign initial values for new record
                   newStudentPtr->key = theTransPtr->key;
                   strcpy(newStudentPtr->name, newName);
                   newStudentPtr->test1 = 0;
                   newStudentPtr->test2 = 0;
                   newStudentPtr->test3 = 0;
                   newStudentPtr->hwCount = 0;
                   newStudentPtr->hwPoints = 0;
    
                   key0 = newStudentPtr->key;
                   while(key0 == newStudentPtr->key)  {
                      theTransPtr = getOneTransFromData();
                      transSeen++;
                      if(key0 != theTransPtr->key)  {
                         getTrans = 0;
                         break;
                      }
                      i = theTransPtr->type;
                      if(i == 1)
                         newStudentPtr->test1 = theTransPtr->value;
                      else if(i == 2)
                         newStudentPtr->test2 = theTransPtr->value;
                      else if(i == 3)
                         newStudentPtr->test3 = theTransPtr->value;
                      if(i > 0 && i < 4)  {
                         if(theTransPtr->value < 0 || theTransPtr->value > 120)  {
                            printf("\n Error: Key %d has a test score out of range, continuing", theTransPtr->key);
                            transError++;
                         }
                      }
                      if(i == 4)  {
                         newStudentPtr->hwCount++;
                         newStudentPtr->hwPoints += theTransPtr->value;
                         if(theTransPtr->value < 0 || theTransPtr->value > 5)  {
                            printf("\n Error: Key %d has a homework point value that is out of range,
     continuing", theTransPtr->key);
                            transError++;
                         }
                         if(newStudentPtr->hwCount++ < 0 || newStudentPtr->hwCount > 10)  {
                            printf("\n Error: Key %d has a homework count that is out of range, 
    continuing", newStudentPtr->hwCount);
                            transError++;
                         }  
                      }
                      if(i == 5)  {
                         newStudentPtr->hwPoints += theTransPtr->value;
                         points = newStudentPtr->hwPoints;
                         if(points < 0 || points > 50 || points > 5 * newStudentPtr->hwCount)  {
                            printf("\n Error: Key %d has homework points that are out of range,
     continuing", newStudentPtr->hwPoints);
                            transError++;
                         }
                      }
                   }   //end of while
                   writeRecord(newStudentPtr);
                   break;
                //our student key # is too high - key synch error
                case 1: case 2: case 3: case 4: case 5:
                   transError++;
                   printf("\n Error: The student key %d and transaction key %d numbers do not
     match,", theStudentPtr->key, theTransPtr->key);
                   printf("\n        and should. Usual cause is a typo in one or the other key
     numbers");
                   printf("\n        - continuing"); 
                   writeRecord(theStudentPtr);
                   break;
             }     //end of switch
          }        //end of else if
          else if(theTransPtr->key == theStudentPtr->key) {
             switch (theTransPtr->type)   {
                case -1: break;
                case 0:
                   transError++;
                   if(strcmp(theTransPtr->name, theStudentPtr->name) == 0)  
                      printf("\n Error: The record for %s, already exists, continuing", 
     theTransPtr->name);
                   else
                      printf("\n Error: Key number %d, is already assigned, continuing", 
     theTransPtr->key);
                   break;
                case 1:
                   theStudentPtr->test1 += theTransPtr->value;   
                   if(theStudentPtr->test1 > 120)  {
                      printf("\n Error: Key number %d, has a test one value out of range, continuing",
     theTransPtr->key);
                      transError++;
                   }
                   break;
                case 2:
                   theStudentPtr->test2 += theTransPtr->value;
                   if(theStudentPtr->test2 > 120)  {
                      printf("\n Error: Key number %d, has a test two value out of range, continuing", 
     theTransPtr->key);
                      transError++;
                   }
                   break;
                case 3:
                   theStudentPtr->test3 += theTransPtr->value;
                   if(theStudentPtr->test3 > 120)  {
                      printf("\n Error: Key number %d, has a test three value out of range, 
     continuing", theTransPtr->key);
                      transError++;
                   }
                   break;
                case 4:
                   theStudentPtr->hwCount++;
                   theStudentPtr->hwPoints += theTransPtr->value;
                   if(theStudentPtr->hwCount < 0 || theStudentPtr->hwCount > 10)  {
                      printf("\n Error: Key number %d has a homework count that is out of range, 
     continuing", theTransPtr->key);
                      transError++;
                   }
                   if(theStudentPtr->hwPoints < 0 || theStudentPtr->hwPoints > 50)  {
                      printf("\n Error: Key number %d has homework points that are out of range, 
     continuing", theTransPtr->key);
                      transError++;
                   }
                   break;
                case 5:
                   theStudentPtr->hwPoints += theTransPtr->value;
                      if(theStudentPtr->hwPoints < 0 || theStudentPtr->hwPoints > 
     5 * theStudentPtr->hwCount)  {
                         printf("\n Error: Key number %d has homework points that are out of range, 
     continuing", theTransPtr->key);
                         transError++;
                      }
                      break;
             }        //end of switch
             writeRecord (theStudentPtr);
          }           //end of else if(key == key)
          //for debug aid, only
          //if(recordsSeen > 18)
          //   gar++;
    	   
       } while (theStudentPtr->key != 0);
    
       free(theStudentPtr);
       free(theTransPtr);
       free(newStudentPtr);  
       printf("\n %d Transactions were processed, with %d errors", transSeen, transError);
       
       return recordsSeen;
    }
    
    int readRecordsAndPrintData ( ) {
       int total, theKey, theTest1, theTest2, theTest3, theHwCount, theHwPoints;
       int recordsSeen, gar;
       char theName [7] = {'\0'};
       char cr;
       float percent;
       struct Student * myStudentPtr;
       recordsSeen = 0;
       myStudentPtr = (struct Student *) malloc (sizeof (struct Student));
       putchar('\n');
       printf("\n Key\tName\tTotal Points\tPercent of 350 Points\n");
       printf(" =============================================================\n");
       do {
       	fscanf (recordFile, "%d%s%d%d%d%d%d%c", &theKey, theName, &theTest1, 
          &theTest2, &theTest3, &theHwCount, &theHwPoints, &cr);
    
          myStudentPtr->key = theKey;
          myStudentPtr->test1 = theTest1;
          myStudentPtr->test2 = theTest2;
          myStudentPtr->test3 = theTest3;
          myStudentPtr->hwCount = theHwCount;
          myStudentPtr->hwPoints = theHwPoints;
          strcpy (myStudentPtr->name, theName);    
    
          if (myStudentPtr->key != 0) {
             total = 0;
             total = myStudentPtr->test1+myStudentPtr->test2+myStudentPtr->test3;
             if(myStudentPtr->hwPoints > 5 * myStudentPtr->hwCount)
                myStudentPtr->hwPoints = 5 * myStudentPtr->hwCount;
    
             total += myStudentPtr->hwCount + myStudentPtr->hwPoints;
             percent = (total/350.0) * 100;
    
    	      printf (" %d\t%s\t    %d\t\t        %.2f%\n",  myStudentPtr->key,
    		   myStudentPtr->name, total, percent);
    
             recordsSeen++;
    	   }	
       } while (myStudentPtr->key != 0);
    
       free(myStudentPtr);
       return recordsSeen;
    }
    
    int main ( ) {
       int i, numRecords, gar;
    
       for(i = 0; i < 10; i++)
          putchar('\n');
    
       if (openFiles ( )) 
    	   return 1;
        
       numRecords = 0;
       numRecords = readDataAndWriteRecords ( );
       printf ("\n %d records read from data file\n", numRecords);
    
       fseek (recordFile, 0, SEEK_SET);
    
       numRecords = 0;
       numRecords = readRecordsAndPrintData ( );
       printf ("\n %d records read from record file\n", numRecords);
    
       closeFiles ( );
       printf("\n                   Program Complete - Press Enter When Ready\n");
       gar = getchar(); gar++;
       return 0;
    }
    As expected, readDataAndWriteRecords() is badly flawed. Don't bother telling me all it's problems, I'm seeing them clearly, now that the other stuff is done. I'm re-touching it.
    Last edited by Adak; 06-07-2008 at 01:56 PM.

  12. #72
    Registered User
    Join Date
    May 2008
    Posts
    61
    This did not compile, and as you assumed, it was because of readDataAndWriteRecords();

    Code:
    sequential.c: In function `readDataAndWriteRecords':
    sequential.c:166: error: missing terminating " character
    sequential.c:167: error: missing terminating " character
    sequential.c:168: error: `continuing' undeclared (first use in this function)
    sequential.c:168: error: (Each undeclared identifier is reported only once
    sequential.c:168: error: for each function it appears in.)
    sequential.c:168: error: syntax error before "transError"
    sequential.c:171: error: missing terminating " character
    sequential.c:172: error: missing terminating " character
    sequential.c:173: error: syntax error before "transError"
    sequential.c:180: error: missing terminating " character
    sequential.c:181: error: missing terminating " character
    sequential.c:182: error: syntax error before "transError"
    sequential.c:191: error: missing terminating " character
    sequential.c:192: error: `match' undeclared (first use in this function)
    sequential.c:192: error: missing terminating " character
    sequential.c:193: error: missing terminating " character
    sequential.c:194: error: missing terminating " character
    sequential.c:195: error: `numbers' undeclared (first use in this function)
    sequential.c:195: error: syntax error before "printf"
    sequential.c:231: error: missing terminating " character
    sequential.c:232: error: missing terminating " character
    sequential.c:233: error: syntax error before "transError"
    sequential.c:240: error: missing terminating " character
    sequential.c:241: error: missing terminating " character
    sequential.c:242: error: syntax error before "transError"
    sequential.c:245: error: missing terminating " character
    sequential.c:246: error: missing terminating " character
    sequential.c:247: error: syntax error before "transError"
    sequential.c:254: error: missing terminating " character
    sequential.c:255: error: missing terminating " character
    sequential.c:256: error: syntax error before "transError"
    I am sure this is what you meant by not telling you all the problems, but I figured maybe my compiler might possibly tell you something different, be more picky or vice versa, so there is the error coding. Thanks as always for ur extreme dedication and assistance.

    and btw, you are spot on by saying my professor knows what he is doing, but is not organized.. by any means. lol.

  13. #73
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The forum editor goofed up my paste job.

    The readDataAndWriteRecords() is flawed, but it handled the transaction file snippet you gave me, perfectly, and compiled no problems or warnings. I wouldn't post something THAT bad.

    When I said that function was flawed, I meant that *now* that I finally understand what the *bleep* the assignment sheet is talking about, I made up a more complex real life type of transaction file, and it couldn't handle THAT at all well.

    Well of course not, I didn't design it for that, because I didn't understand what the *bleep* he was expressing in that *bleep* *bleep*, assignment sheet, which of course, had NOT ONE, example.

    Anyway, I'm halfway through redoing the function. A lot of the particulars are right, but the overall flow is not up to par yet. This is backwards to how I usually work on a program, so it's kinda weird.

  14. #74
    Registered User
    Join Date
    May 2008
    Posts
    61
    yea that is one of my main gripes, the fact that he doesn't cover this kind of programming much at all during the class, but then also provides nyet, zero, zilch examples for me to look over and interpret and understand.

    I can normally go off an example, but with nothing to go off of, I was up *bleeps* creek.

  15. #75
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a version of the above program that should be OK with the forum editor. It's not right, yet, but does handle the trans file you and database file you gave me, OK.

    I'm still working on the more robust version.
    Code:
    /*Modifications to a program posted by IneedHelpBad */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Student {
        int  key;
        char name [7];
        int  test1, test2, test3;
        int  hwCount;
        int  hwPoints;
    } 
    dummy = {-1, "", 0, 0, 0, 0, 0},
    * aStudent;
    
    struct Transaction {
       int type;		
       int key;		
       int value;	
       char name [7];
    };
    
    
    FILE * dataFile, * recordFile,  * transFile; //* recordFile2
    
    int openFiles ( ) {
        if ((dataFile = fopen ("seq_data.txt", "r")) == NULL) {
    	   fprintf (stderr, "Error in opening input file\n");
    	   return 1;
        }
        if ((transFile = fopen ("seq_tran.txt", "r")) == NULL) {
    	   fprintf (stderr, "Error in opening transaction file\n");
    	   return 1;
        }
        if ((recordFile = fopen ("seq_recs.txt", "w+t")) == NULL) {
    	   fprintf (stderr, "Error in opening record file\n");
    	   return 1;
        }
        return 0;
    }
    
    void closeFiles ( ) {
        fclose (dataFile);
        fclose (recordFile);
        fclose(transFile);
    }
    
    struct Student * getOneStudentFromData ( ) {
       struct Student * aStudentPtr;
       int theId, theTest1, theTest2, theTest3, theHwCount, theHwPoints;
       char theName [7];
       char cr;
       fscanf (dataFile, "%d%s%d%d%d%d%d%c",
    	&theId, theName, &theTest1, &theTest2, &theTest3,
    	&theHwCount, &theHwPoints, &cr);
    
       if(theId > -1)   {
          aStudentPtr = (struct Student *) malloc (sizeof (struct Student));
          aStudentPtr->key = theId;
          aStudentPtr->test1 = theTest1;
          aStudentPtr->test2 = theTest2;
          aStudentPtr->test3 = theTest3;
          aStudentPtr->hwCount = theHwCount;
          aStudentPtr->hwPoints = theHwPoints;
          strcpy (aStudentPtr->name, theName);    
       }
       else 
          aStudentPtr = &dummy;
    
       return aStudentPtr;
    }
    struct Transaction * getOneTransFromData ( ) {
       struct Transaction * aTransPtr;
       int Ttype, Tkey, Tvalue;
       char Tname [7] = { '\0' };
       char cr;
       fscanf (transFile, "%d%d%d%s%c", &Ttype, &Tkey, &Tvalue, Tname, &cr);
    
       aTransPtr = (struct Transaction *) malloc (sizeof (struct Transaction));
       aTransPtr->type = Ttype;
       aTransPtr->key = Tkey;
       aTransPtr->value = Tvalue;
       strcpy (aTransPtr->name, Tname);    
       
       return aTransPtr;
    }
    void writeRecord (struct Student * theStudentPtr) {
       fprintf(recordFile, "%d\t%s\t%d\t%d\t%d\t%d\t%d\n", theStudentPtr->key, 
          theStudentPtr->name, theStudentPtr->test1, theStudentPtr->test2, 
          theStudentPtr->test3, theStudentPtr->hwCount, theStudentPtr->hwPoints);    
    }
    int readDataAndWriteRecords (void) {
       char newName[7] = {'\0'};
       int key0, getTrans, getStudent, transSeen, hwcount, hwscore;
       int i, transError, points, gar;
       int recordsSeen = 0;	
       struct Student * theStudentPtr;
       struct Student * newStudentPtr;
       struct Transaction * theTransPtr;
       
       theTransPtr = (struct Transaction *) malloc (sizeof (struct Transaction));
       theStudentPtr = (struct Student *) malloc (sizeof (struct Student));
       newStudentPtr = (struct Student *) malloc (sizeof (struct Student));
    
       transSeen = transError = 0;
       getStudent = getTrans = 1;
    
       do {
          if(getStudent == 1)
             theStudentPtr = getOneStudentFromData();
          if(theStudentPtr->key == 0)  {   
             getStudent = 0;
          }
          else
             recordsSeen++;
          
          if(getTrans == 1)  {
             theTransPtr = getOneTransFromData();
             if(theTransPtr->type < 0)  {
                getTrans = 0;             //get no more transactions
             }
             else
                transSeen++;
          }
          if(theStudentPtr->key > 1 && theTransPtr->key < 0)  
             continue;
          
          if(theStudentPtr->key < theTransPtr->key)
             writeRecord (theStudentPtr);
          else if(theTransPtr->key < theStudentPtr->key) {
             switch (theTransPtr->type)   {
                case 0: 
                   //assign initial values for new record
                   newStudentPtr->key = theTransPtr->key;
                   strcpy(newStudentPtr->name, newName);
                   newStudentPtr->test1 = 0;
                   newStudentPtr->test2 = 0;
                   newStudentPtr->test3 = 0;
                   newStudentPtr->hwCount = 0;
                   newStudentPtr->hwPoints = 0;
    
                   key0 = newStudentPtr->key;
                   while(key0 == newStudentPtr->key)  {
                      theTransPtr = getOneTransFromData();
                      transSeen++;
                      if(key0 != theTransPtr->key)  {
                         getTrans = 0;
                         break;
                      }
                      i = theTransPtr->type;
                      if(i == 1)
                         newStudentPtr->test1 = theTransPtr->value;
                      else if(i == 2)
                         newStudentPtr->test2 = theTransPtr->value;
                      else if(i == 3)
                         newStudentPtr->test3 = theTransPtr->value;
                      if(i > 0 && i < 4)  {
                         if(theTransPtr->value < 0 || theTransPtr->value > 120)  {
                            printf("\n Error: Key %d has a test score out of range, continuing", theTransPtr->key);
                            transError++;
                         }
                      }
                      if(i == 4)  {
                         newStudentPtr->hwCount++;
                         newStudentPtr->hwPoints += theTransPtr->value;
                         if(theTransPtr->value < 0 || theTransPtr->value > 5)  {
                            printf("\n Error: Key %d has a homework point value that is out of range, continuing", theTransPtr->key);
                            transError++;
                         }
                         if(newStudentPtr->hwCount++ < 0 || newStudentPtr->hwCount > 10)  {
                            printf("\n Error: Key %d has a homework count that is out of range, continuing", newStudentPtr->hwCount);
                            transError++;
                         }  
                      }
                      if(i == 5)  {
                         newStudentPtr->hwPoints += theTransPtr->value;
                         points = newStudentPtr->hwPoints;
                         if(points < 0 || points > 50 || points > 5 * newStudentPtr->hwCount)  {
                            printf("\n Error: Key %d has homework points that are out of range, continuing", newStudentPtr->hwPoints);
                            transError++;
                         }
                      }
                   }   //end of while
                   writeRecord(newStudentPtr);
                   break;
                //our student key # is too high - key synch error
                case 1: case 2: case 3: case 4: case 5:
                   transError++;
                   printf("\n Error: The student key %d and transaction key %d numbers do not match,", theStudentPtr->key, theTransPtr->key);
                   printf("\n        and should, continuing");
                   writeRecord(theStudentPtr);
                   break;
             }     //end of switch
          }        //end of else if
          else if(theTransPtr->key == theStudentPtr->key) {
             switch (theTransPtr->type)   {
                case -1: break;
                case 0:
                   transError++;
                   if(strcmp(theTransPtr->name, theStudentPtr->name) == 0)  
                      printf("\n Error: The record for %s, already exists, continuing", theTransPtr->name);
                   else
                      printf("\n Error: Key number %d, is already assigned, continuing", theTransPtr->key);
                   break;
                case 1:
                   theStudentPtr->test1 += theTransPtr->value;   
                   if(theStudentPtr->test1 > 120)  {
                      printf("\n Error: Key number %d, has a test one value out of range, continuing", theTransPtr->key);
                      transError++;
                   }
                   break;
                case 2:
                   theStudentPtr->test2 += theTransPtr->value;
                   if(theStudentPtr->test2 > 120)  {
                      printf("\n Error: Key number %d, has a test two value out of range, continuing", theTransPtr->key);
                      transError++;
                   }
                   break;
                case 3:
                   theStudentPtr->test3 += theTransPtr->value;
                   if(theStudentPtr->test3 > 120)  {
                      printf("\n Error: Key number %d, has a test three value out of range, continuing", theTransPtr->key);
                      transError++;
                   }
                   break;
                case 4:
                   theStudentPtr->hwCount++;
                   theStudentPtr->hwPoints += theTransPtr->value;
                   if(theStudentPtr->hwCount < 0 || theStudentPtr->hwCount > 10)  {
                      printf("\n Error: Key number %d has a homework count that is out of range, continuing", theTransPtr->key);
                      transError++;
                   }
                   if(theStudentPtr->hwPoints < 0 || theStudentPtr->hwPoints > 50)  {
                      printf("\n Error: Key number %d has homework points that are out of range, continuing", theTransPtr->key);
                      transError++;
                   }
                   break;
                case 5:
                   theStudentPtr->hwPoints += theTransPtr->value;
                      if(theStudentPtr->hwPoints < 0 || theStudentPtr->hwPoints > 5 * theStudentPtr->hwCount)  {
                         printf("\n Error: Key number %d has homework points that are out of range, continuing", theTransPtr->key);
                         transError++;
                      }
                      break;
             }        //end of switch
             writeRecord (theStudentPtr);
          }           //end of else if(key == key)
          //for debug aid, only
          //if(recordsSeen > 16)
          //   gar++;
    	   
       } while (theStudentPtr->key != 0);
    
       free(theStudentPtr);
       free(theTransPtr);
       free(newStudentPtr);  
       printf("\n %d Transactions were processed, with %d errors", transSeen, transError);
       
       return recordsSeen;
    }
    
    int readRecordsAndPrintData ( ) {
       int total, theKey, theTest1, theTest2, theTest3, theHwCount, theHwPoints;
       int recordsSeen, gar;
       char theName [7] = {'\0'};
       char cr;
       float percent;
       struct Student * myStudentPtr;
       recordsSeen = 0;
       myStudentPtr = (struct Student *) malloc (sizeof (struct Student));
       putchar('\n');
       printf("\n Key\tName\tTotal Points\tPercent of 350 Points\n");
       printf(" =============================================================\n");
       do {
       	fscanf (recordFile, "%d%s%d%d%d%d%d%c", &theKey, theName, &theTest1, 
          &theTest2, &theTest3, &theHwCount, &theHwPoints, &cr);
    
          myStudentPtr->key = theKey;
          myStudentPtr->test1 = theTest1;
          myStudentPtr->test2 = theTest2;
          myStudentPtr->test3 = theTest3;
          myStudentPtr->hwCount = theHwCount;
          myStudentPtr->hwPoints = theHwPoints;
          strcpy (myStudentPtr->name, theName);    
    
          if (myStudentPtr->key != 0) {
             total = 0;
             total = myStudentPtr->test1+myStudentPtr->test2+myStudentPtr->test3;
             if(myStudentPtr->hwPoints > 5 * myStudentPtr->hwCount)
                myStudentPtr->hwPoints = 5 * myStudentPtr->hwCount;
    
             total += myStudentPtr->hwCount + myStudentPtr->hwPoints;
             percent = (total/350.0) * 100;
    
    	      printf (" %d\t%s\t    %d\t\t        %.2f%\n",  myStudentPtr->key,
    		   myStudentPtr->name, total, percent);
    
             recordsSeen++;
    	   }	
       } while (myStudentPtr->key != 0);
    
       free(myStudentPtr);
       return recordsSeen;
    }
    
    int main ( ) {
       int i, numRecords, gar;
    
       for(i = 0; i < 10; i++)
          putchar('\n');
    
       if (openFiles ( )) 
    	   return 1;
        
       numRecords = 0;
       numRecords = readDataAndWriteRecords ( );
       printf ("\n %d records read from data file\n", numRecords);
    
       fseek (recordFile, 0, SEEK_SET);
    
       numRecords = 0;
       numRecords = readRecordsAndPrintData ( );
       printf ("\n %d records read from record file\n", numRecords);
    
       closeFiles ( );
       printf("\n                   Program Complete - Press Enter When Ready\n");
       gar = getchar(); gar++;
       return 0;
    }
    
    It still "breaks the forum table", but just barely. Try it out with the same files as last time.

Page 5 of 6 FirstFirst 123456 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file help plz
    By dutrachr in forum C Programming
    Replies: 4
    Last Post: 04-18-2006, 11:41 AM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. fputs and sequential file
    By sballew in forum C Programming
    Replies: 5
    Last Post: 11-11-2001, 03:48 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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