Project Title : Student Grading System
Purpose : Calculate the grade and sort the student matrix in descending

example Input file : studentsmark.txt
example Output file: studentresult.txt

How the user operate the software:
1) user run the software
2) an interface show on the screen
3) user keyin Input file name (example:studentsmarks.txt)
4) user keyin Output file name (example:studentresult.txt)
5) user select function to calculate and sort
6) Then an output file stored student result generated
7) user can select option to continue another input file or quit the software

Formular:
1) please exam studentsmark.txt and studentresult.txt
2) the result listing in the studentresult.txt must be sort by student matrix
3) Grade : MARKS >= 80 Then A
MARKS >= 75 Then A-
MARKS >= 70 Then B+
MARKS >= 65 Then B
MARKS >= 60 Then B-
MARKS >= 55 Then C+
MARKS >= 50 Then C
MARKS >= 45 Then C-
MARKS >= 40 Then D+
MARKS >= 35 Then D
MARKS < 35 Then E

input file : studentsmark.txt
01234012345678901234567890123456789
0123401234567890123456789012345678901234567890123456789012345678901234567890123456789
No Matrix quiz1(5%) quiz1(5%) quiz1(5%) Proj(25%) MidT(20%)
1 BK20008 4 3 5 18 18
2 BK20002 5 5 4 10 8
3 BK20003 3 4 5 18 10
4 BK20006 4 3 4 20 14
5 BK20005 5 4 3 13 17
6 BK20004 2 5 2 4 16
7 BK20007 3 4 4 21 14
8 BK20001 4 3 5 11 15
9 BK20009 3 2 4 10 10
10 BK20010 4 3 3 23 18

i have written the first part that is to read the file content
but obviously it cannot run

#include <stdio.h>


void main (void)
{
	char matrix[200];
	int i, j, num_elem, no[20], quiz_1[20], quiz_2[20], quiz_3[20], project[20], midT[20];
    FILE *infile;

	infile = fopen("studentsmark.txt","r");

	i=1;

	while( fscanf(infile,"%d %s %d %d %d %d %d %d",&no[i],&matrix[i], &quiz_1[i], &quiz_2[i], &quiz_3[i], &project[i], &midT[i]) !=EOF) i++;
	
    num_elem = i;

	for(j=1;j<num_elem;j++)
	{
	 printf("%d %s %d %d %d %d %d %d\n",no[j], matrix[i], quiz_1[j], quiz_2[j], quiz_3[j], project[j], midT[j]);
	}

	fclose(infile);

}

it only can run if i remove the 1st line and matrix coloum, please tell me what to do..i'm real weak in programming..

Is all this stuff:

01234012345678901234567890123456789
0123401234567890123456789012345678901234567890123456789012345678901234567890123456789
No Matrix quiz1(5%) quiz1(5%) quiz1(5%) Proj(25%) MidT(20%)

part of the input file? If so, you have to add some code to read (and ignore) it before trying to read in the real data.

num_elem serves no real purpose, since you already have the value you need in i.

For the student ids, you want an array of strings, not one big string, so change char matrix[200] to char matrix[10][20] . (Perhaps the first dimension should be something bigger than 10 in case there will be more than 10 students.)

Array numbering starts at 0, not 1, so the initial value of i should be 0. Similarly, in the for loop the initial value of j should be 0.

Your fscanf statement and your printf statement both have an extra %d.

In the fscanf parameter list, use matrix[i] (no "&") since matrix[i] is already an address -- it's the address of the ith string -- after you change matrix to a 2-dimensional array as I suggested above.

thanks, i will try working on it with your suggestion


the input file include

012340123456789012345678901234567890123456789012345678
No Matrix quiz1(5%) quiz1(5%) quiz1(5%) Proj(25%) MidT(20%) )

it only has 1 number line. sry i accidently add extra one line, the number line is for coloum reference

this is my corrected code

#include <stdio.h>


int main ()
{
	char matrix[20][200];
	int i, j, no[20], quiz_1[20], quiz_2[20], quiz_3[20], project[20], midT[20];
    FILE *infile;

	infile = fopen("studentsmark.txt","r");

	i=0;

	while( fscanf(infile,"%d %s %d %d %d %d %d",&no[i], matrix[i], &quiz_1[i], &quiz_2[i], &quiz_3[i], &project[i], &midT[i]) !=EOF) i++;

       

	for(j=0;j<i;j++)
	{
	 printf("%d %s %d %d %d %d %d\n",no[j], matrix[j], quiz_1[j], quiz_2[j], quiz_3[j], project[j], midT[j]);
	}

	fclose(infile);
}

but i still i am unable to read tis line
No Matrix quiz1(5%) quiz1(5%) quiz1(5%) Proj(25%) MidT(20%)

what should i add to make the program read it and ignore it so i can read on the real data i need, i really have no idea...

There are many ways to do this. Probably getc or fgetc would be the simplest for you. Look at a reference for stdio.h functions, for example this one, to see how to use those functions: http://www.cplusplus.com/reference/clibrary/cstdio/

By the way, it seems to me that you also have to get rid of the line

012340123456789012345678901234567890123456789012345678

I don't understand what purpose that line serves, but you certainly don't want to read it into any of your existing variables. You can use the same function, e.g. getc , to take care of both header lines.

thanks again, i will try

012340123456789012345678901234567890123456789012345678

this line is for column reference, that is what told by my lecturer..

012340123456789012345678901234567890123456789012345678

this line is for column reference, that is what told by my lecturer..

Yes, you said that already, but it still makes no sense to me for that to be in the input file. Surely, the program doesn't need that to figure out which data is in which column of the file. A file doesn't actually have columns at all -- it is just a continuous stream of bytes representing characters which we interpret as rows and columns based on the location of newline characters, blank space characters, etc.

So, if your assignment requires that to be in the file, I assume your instructor's intention was simply for you to learn how to write a program that can handle an input file in which different lines are formatted differently. And unless you have some other information that you haven't posted, the program doesn't have to use the data in those first two lines, but still must read them in order to get to the "real" data that follows.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Well, it donst make any sense of the input file that your trying to read with regards to the student grade information. If I was you, i would go your lecture and speak about on how the input file should be read. And how the file had been formatted with reference to the student grade information.

If you know that, then it makes it ease to read the chucks of the file where each chuck would be the information about one student!

-ssharish

now my program able to read the input file, i add some code written by other forumer, but i could'nt understand how it actually run..looks like it skips the first 2 line and read the rest...

#include <stdio.h>


void main (void)
{
	char matrix[20][200], line[100][100];
	int i, j, x, no[20], quiz_1[20], quiz_2[20], quiz_3[20], project[20], midT[20], final[20];
    FILE *infile;

	infile = fopen("studentsmark.txt","r");

    x=0;
    while (fgets(line,100,infile)!=NULL)
     {
  
      x++;
      if(x==1)
	  {
      
      }
	 
      else{
	  i=0;
      while( fscanf(infile,"%d %s %d %d %d %d %d %d",&no[i],matrix[i], &quiz_1[i], &quiz_2[i], &quiz_3[i], &project[i], &midT[i], &final[i]) !=EOF)
     
     
   
      printf("%d %s %d %d %d %d %d %d\n",no[i], matrix[i], quiz_1[i], quiz_2[i], quiz_3[i], project[i], midT[i], final[i]);
      i++;




	  }
	  
	}
	fclose(infile);
}

this is the code i add

int x = 0;
while (fgets(line,100,infile)!=NULL)
{
     x++;
      if(x==1)
        {
         }
      else{
      }

   }

this is the actual file i need to read..

0123401234567890123456789012345678901234567890123456789012345678901234567890123456789
No   Matrix    quiz1(5%) quiz1(5%) quiz1(5%) Proj(25%) MidT(20%) Final(40%)
1    BK20008   4         3         5         18        18        36
2    BK20002   5         5         4         10        8         30
3    BK20003   3         4         5         18        10        35
4    BK20006   4         3         4         20        14        36
5    BK20005   5         4         3         13        17        28
6    BK20004   2         5         2         4         16        10
7    BK20007   3         4         4         21        14        32
8    BK20001   4         3         5         11        15        31
9    BK20009   3         2         4         10        10        20
10   BK20010   4         3         3         23        18        22

Ok, this looks a bit more sensible. So as you can see that you don’t have to read anything from the first two lines of the file. You really need to extract the information from the third line.

Follow the steps given bellow:
1. Open the file
2. Read the first 2 lines and discard
3. Start buffering the data from thrid line onward and start extracting the data from the buffer using fscanf or the (fgets + sscanf).
4. And store then in a data structure.

If you can get to this point, I will give more instructions! By the way the code you have posted has a very horrible indentation. I refused to look at it. Indent the code and repost it back again.

-ssharish

Here's how it works: It enters the first while loop, reads the first line, stores it in the "line" array, then makes x=1, then it does "nothing" because of the

if( x == 1 )
  {   //nothing in here
  }

so it enters the first while loop again, reads the next line, stores it in the "line" array, makes x = 2, then (x==1) is false so it executes your code in the "else" block.

OK, it works, but it's very clumsy. First, the line[100][100] is an error. There's no reason at all for a 100 x 100 array, and it's actually the wrong type to give as an argument to fgets. Your compiler should be giving you a warning about that. line[100] would be better, but still not so good.

The real issue is that there is no reason to be storing that "junk" at all (except that fgets requires a buffer to store what it reads). Here's a much nicer way to read and ignore those 2 lines:

for( i = 0; i < 2; i++ ) {
    while( fgetc(infile) != '\n' )
      ;
  }

After that goes your code to read and print the read data, with no extra variable "x", no "line" array, no if statements, etc.

One more thing: you should use "int", not "void" as the return type of your main function. I guess you are using Turbo C++ or some other very old compiler which doesn't insist on that, but int is the standard so you should get in the habit of using it. In other words, write int main() or int main(void) , and then put return 0; as the last line of your program.

By the way, you omitted brackets for your second while loop. Also, I have to agree with ssharish2005 that your indentation needs work. But I'll give you a break this time. Here's how your program should look:

#include <stdio.h>

int main (void)
{
  char matrix[20][200];
  int i, j, no[20], quiz_1[20], quiz_2[20], quiz_3[20], project[20], midT[20], final[20];
  FILE *infile;

  infile = fopen("studentsmark.txt","r");

  for( i = 0; i < 2; i++ ) {
    while( fgetc(infile) != '\n' )
      ;
  }
  i=0;

  while( fscanf(infile,"%d %s %d %d %d %d %d %d",&no[i],matrix[i], &quiz_1[i], &quiz_2[i], &quiz_3[i], &project[i], &midT[i], &final[i]) !=EOF)
  {  printf("%d %s %d %d %d %d %d %d\n",no[i], matrix[i], quiz_1[i], quiz_2[i], quiz_3[i], project[i], midT[i], final[i]);
    i++;
  }

  fclose(infile);
  return 0;
}
This article has been dead for over six months. Start a new discussion instead.