Thread: Program for grading

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    Question Program for grading

    We have a project for our CS class thats purpose is to read 3 different files, compare them, and list exam scores and user names in a new file

    The files are to be as follows:

    class.txt - This is the roster of the course. The first column is the identification number of the student followed by the name of the student.

    webCT.txt - This is the file with the necessary information to upload scores to WebCT. The first column is the identification number of the student and the second is the university ITaP login.

    exam.txt - This file contains the student identification number and the score earned on the exam.

    uploadExam.txt - The file you will create, this file will have the the score and university ITaP login for each student separated by a comma and a space (this is known as a comma delimited file). If a student in the class.txt file does not have a score in exam.txt then the university ITaP login and a score of zero should be listed in this file.

    badID.txt - In this file you will place all records in exam.txt that DO NOT have a matching student identification number in the class.txt file. Data placed in this file are a result of an incorrectly coded student identification number.

    The files are also formatted as follows:

    In class.txt:

    12345 Jackson, Keith
    11111 Gibson, Robert G
    22222 Sanders, Ryan
    33333 Anderson, Jennifer A
    44444 Martinez, Mary

    In webCT.txt:

    12345 jacksonk
    11111 gibsong
    44444 martinez33
    22222 sandersr
    33333 janders

    In exam.txt:

    11111 75 Gibson, Bob G
    22222 88 Sanders, R
    33333 90 Anderson, Jenny
    12344 77 Jackson, Keith

    In uploadExam.txt:

    Exam1Score, User Id
    0, jacksonk
    75, gibsong
    0, martinez33
    88, sandersr
    90, janders

    In badID.txt:

    12344 77 Jackson, Keith

    so far we have gotten our program to load the first two files, but we are stuck on how to load the exam.txt file. We can only use strings for this project and it will eventually need to be able to accept any number of inputs. Here is our code:

    Code:
    #include <stdio.h>
    
    int main ()
    {
      FILE *fpClass;
      FILE *fpwebCT;
      FILE *fpexam;
      int id1;
      int id2[5];
      int counter;
      int counter2;
      char character[35];
      char names[6];
      char names2[6];
      int id3;
      int id4[5];
      char login[35];
      int counter3;
      int i;
      int score;
      int id5;
      int id6[5];
      int scores[6];
    
    //********class.txt********
      
      fpClass = fopen("class.txt", "r");
    
    for(counter=1; counter <= 5; counter++)
       {
       fscanf(fpClass, "%d%d%d%d%d", &id1);
       //printf("%d\n", id1);
       id2[counter] = id1;
    //printf("\n\n%d\n\n", id2[counter]);
    
       fgets(character, 35, fpClass);
    //   printf("%s\n", character);
       names[counter] = character;
    //printf("\n\n%s\n\n", names[counter]);
       }
    
    fclose(fpClass);
    
    //********webCT*******
    
    fpwebCT = fopen("webCT.txt", "r");
    
    for(counter2 = 1; counter2 <= 5; counter2++)
       {
    	   fscanf(fpwebCT, "%d%d%d%d%d", &id3);
    	   //printf("%d\n", id3);
       	   id4[counter2] = id3;
    	   fgets(login, 35, fpwebCT);
    	   //printf("%s\n", login);
    	   names2[counter2] = login;
    	   for (counter3=1; counter3 <=5; counter3++)
    	   {
    		   if (id4[counter2] == id2[counter3])
    		   {
    			   printf("%d\t", id4[counter2]);
    			   printf("%s\n", login);
    		   }
    	   }
       }
       
    	  fclose(fpwebCT);
    
    //*******exam*******
      fpexam = fopen("exam.txt", "r");
        counter2 = 0;
        for(i = 1; i <= 4; i++)
        {
    	   fscanf(fpexam, "%d%d%d%d%d", &id5);
    
    	   printf("%d\n", id5);
       	   id6[i] = id5;
    	   fscanf(fpexam, "%d%d", &score);
    	   printf("%d\n", score);
    	   //scores[i] = score;
    	   //printf("%d", scores[i]);
    	   for (counter2=1; counter2 <=5; counter2++)
    	   {
    		   if (id4[i] == id6[counter2])
    		   {
    			   printf("%d\t", score);
    			   printf("%d\n", login);
    		   }
    	   }
    	   
       }
    
    
    fclose(fpexam);
       return 0;
    }
    Currently we are recieving a segmentation fault when we run the program for the exam.txt file. Any help that anyone could give us pertaining to this problem, or any other help in writing this code would be great.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Welcome to the boards

    Code:
    fscanf(fpClass, "%d%d%d%d%d", &id1);
    You're asking fscanf to read in five variables, but you're only giving the function one pointer, this isn't good and will likely cause a crash.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean there's a reason for compiler warnings!? No way! Tell me it isn't so!

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

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Expanding what jverkoey said, it looks like you want to read in numbers such as "55555" or "12345" these can be achieved from 1 "%d" not 5 "%d"'s.
    So instead of having:
    Code:
    fscanf(fpClass, "%d%d%d%d%d", &id1);
    You should have something like:
    Code:
    fscanf(fpClass, "%d", &id1);
    Secondly, you are not controlling how you read from the files very well. What happens say, in the exam.txt file you had 5 lines instead of 4? In your code, you simply won't read it in. Instead, use fscanf() to control when the loop terminates. fscanf() returns an EOF indicator when an EOF is reached, so you can have something like the following:
    Code:
    while( fscanf(fpClass, "%d", &id1) != EOF)
    {
            /* do something here */
    }
    You will need more than the %d to read in <number> <string>: "%d %s".
    Please read up on how fscanf() works as well and then implement it into your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM