943,696 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1445
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 23rd, 2009
0

Re: student grading system

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.
Reputation Points: 73
Solved Threads: 20
Posting Whiz in Training
ssharish2005 is offline Offline
253 posts
since Dec 2006
Mar 23rd, 2009
0

Re: student grading system

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. }
Reputation Points: 23
Solved Threads: 18
Junior Poster
r.stiltskin is offline Offline
105 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Alien numbers problem
Next Thread in C Forum Timeline: execute some code just before stopping program using CTRL-C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC