student grading system

Reply

Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: student grading system

 
0
  #11
Mar 23rd, 2009
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
Last edited by ssharish2005; Mar 23rd, 2009 at 7:25 pm.
"Any fool can know, point is to understand"
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 71
Reputation: r.stiltskin is an unknown quantity at this point 
Solved Threads: 9
r.stiltskin r.stiltskin is offline Offline
Junior Poster in Training

Re: student grading system

 
0
  #12
Mar 23rd, 2009
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
  1. if( x == 1 )
  2. { //nothing in here
  3. }
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:
  1. for( i = 0; i < 2; i++ ) {
  2. while( fgetc(infile) != '\n' )
  3. ;
  4. }
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:
  1. #include <stdio.h>
  2.  
  3. int main (void)
  4. {
  5. char matrix[20][200];
  6. int i, j, no[20], quiz_1[20], quiz_2[20], quiz_3[20], project[20], midT[20], final[20];
  7. FILE *infile;
  8.  
  9. infile = fopen("studentsmark.txt","r");
  10.  
  11. for( i = 0; i < 2; i++ ) {
  12. while( fgetc(infile) != '\n' )
  13. ;
  14. }
  15. i=0;
  16.  
  17. 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)
  18. { 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]);
  19. i++;
  20. }
  21.  
  22. fclose(infile);
  23. return 0;
  24. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC