943,539 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2906
  • C RSS
You are currently viewing page 4 of this multi-page discussion thread; Jump to the first page
Jul 22nd, 2008
0

Re: urgent: modify code please

Click to Expand / Collapse  Quote originally posted by raja289 ...
>>>>>line 15: remove the quotes around filename.
i removed quotes at line 15 and


>>>>line 74: you close the file at the end of the first loop iteration. Move that line down after the }
closed file after first for loop
And the results were ???
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,947 posts
since Aug 2005
Jul 22nd, 2008
0

Re: urgent: modify code please

i think i have to sleep now because i am living in someone else house now a days
but tomorrow i will be here at same time
and at morning time i will check for updates
PLEASE RUN THIS PROGRAM ON UR COMPUTER AND SEE WHAT EXCEPTIONS COME THERE
I AM USING BLOODSHED DEV C++ 4.96 VERSION
THANKS ALOT I REALLY APPREICAITE YOU PEOPLE GIVING ME UR IMPORTANT TIME
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
raja289 is offline Offline
54 posts
since Jul 2008
Jul 22nd, 2008
0

Re: urgent: modify code please

Oh sorry
results are same !
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
raja289 is offline Offline
54 posts
since Jul 2008
Jul 22nd, 2008
0

Re: urgent: modify code please

It is writing the data to file but now not reading properly.
i just saved a file and saw all data is written there
but results are same .
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
raja289 is offline Offline
54 posts
since Jul 2008
Jul 22nd, 2008
0

Re: urgent: modify code please

  1. #include<stdio.h>[LIST=1]
  2. #include<string.h>
  3. #include<conio.h>
  4. #pragma warning(disable: 4996)
  5.  
  6. float avg(float x);
  7. main()
  8. {
  9. FILE *fp;
  10. int k,students=10,subjects=4,i,j,id_number,fail;
  11. float marks[100],total=0,average;
  12. char name[30],course_enrolled[30],filename[255],date_of_birth[30];
  13. printf("filename\n");
  14. scanf("%s",filename);
  15. fp=fopen(filename,"w");
  16. for(i=1;i<=2;i++)
  17. {
  18. printf("Enter Name of Student\n");
  19. fscanf(stdin,"%s",name);
  20. fprintf(fp,"%s\n",name);
  21.  
  22. printf("Enter Course In Which Student Is Enrolled\n");
  23. fscanf(stdin,"%s",course_enrolled);
  24. fprintf(fp,"%s\n",course_enrolled);
  25.  
  26. printf("Enter Date Of Birth\n");
  27. fscanf(stdin,"%s",date_of_birth);
  28. fprintf(fp," %s\n",date_of_birth);
  29.  
  30. printf("Enter ID Number\n");
  31. fscanf(stdin,"%d",&id_number);
  32. fprintf(fp," %d\n",id_number);
  33.  
  34. for(j=1;j<=4;j++)
  35. {
  36. printf("Enter Marks for Subject\n");
  37. scanf("%f",&marks[j]);
  38. total=total+marks[j];
  39. if(marks[j]<50)
  40. fail=fail+1;
  41. else
  42. continue;
  43. }
  44. average=avg(total);
  45. total=0.0;
  46. printf("AVERAGE=%2f\n",average);
  47. fprintf(fp," %2f\n",average);
  48. if(average>=70)
  49. printf("Grade A\n");
  50. else if(average>=60&&average<70)
  51. printf("Grade B\n");
  52. else if(average>=50&&average<60)
  53. printf("Grade C\n");
  54. else if(average>=45&&average<50)
  55. printf("Grade D\n");
  56. else if(average>=40&&average<45)
  57. printf("Grade E\n");
  58. else if(average<40)
  59. printf("Fail\n");
  60. else
  61. continue;
  62.  
  63. if(fail>=2)
  64. printf("Student is failed and cann;t promote\n");
  65. else
  66. continue;
  67. fail=0;
  68. }
  69. fclose(fp);
  70. printf("Input filename");
  71. scanf("%s",filename);
  72. fp=fopen(filename,"r");
  73. for(k=1;k<=2;k++)
  74. {
  75. fscanf(fp,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s AVERAGE= %2f\n",name,course_enrolled,&id_number,date_of_birth,average);
  76. fprintf(stdout,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s AVERAGE= %2f\n",name,course_enrolled,id_number,date_of_birth,average);
  77. }
  78. fclose(fp);
  79. getch();
  80. }
  81. float avg(float x)
  82. {
  83. float a=0.0;
  84. a=(x/400)*100;
  85. return (a);
  86. }[/LIST]
Last edited by Ancient Dragon; Jul 22nd, 2008 at 7:15 pm. Reason: add line numbers
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
raja289 is offline Offline
54 posts
since Jul 2008
Jul 22nd, 2008
0

Re: urgent: modify code please

?????
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
raja289 is offline Offline
54 posts
since Jul 2008
Jul 22nd, 2008
0

Re: urgent: modify code please

delete lines 70 and 71 because the program already knows the filename from lines 13 and 14. No point in asking for the filename again.

line 75: fscanf() is mal-formed. Here is correction
  1. while(fscanf(fp,"%s%s%s%d%f",
  2. name,
  3. course_enrolled,
  4. date_of_birth,
  5. &id_number,
  6. &average) > 0)
  7. {
  8. fprintf(stdout,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s\tAVERAGE= %f\n",name,course_enrolled,id_number,date_of_birth,average);
  9. }
Last edited by Ancient Dragon; Jul 22nd, 2008 at 7:28 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,947 posts
since Aug 2005
Jul 22nd, 2008
0

Re: urgent: modify code please

I believe this will work better. Note that you were writing out the variables in a slightly different order than you were fscanf'ing them back in, and the failing message shouldn't go into the data file, because you have no way of handling it when the data is read back in.

Instead, just add an if statement to your program after the data is read back in from the file, and then print the "student has failed and can't promote" message, onto the screen.

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<conio.h>
  4. #pragma warning(disable: 4996)
  5.  
  6. float avg(float x);
  7. int main()
  8. {
  9. FILE *fp;
  10. int k,students=10,subjects=4,i,j,id_number,fail;
  11. float marks[100],total=0,average;
  12. char name[30],course_enrolled[30],filename[10],date_of_birth[30];
  13. //printf("filename\n");
  14. //scanf("%s",filename);
  15. printf("\n\n\n\n");
  16.  
  17. fp=fopen("filename","w");
  18. for(i=1;i<=10;i++)
  19. {
  20. printf("Enter Name of Student\n");
  21. fscanf(stdin,"%s",name);
  22. fprintf(fp,"%s\n",name);
  23.  
  24. printf("Enter Course In Which Student Is Enrolled\n");
  25. fscanf(stdin,"%s",course_enrolled);
  26. fprintf(fp,"%s\n",course_enrolled);
  27.  
  28. printf("Enter Date Of Birth\n");
  29. fscanf(stdin,"%s",date_of_birth);
  30. fprintf(fp," %s\n",date_of_birth);
  31.  
  32. printf("Enter ID Number\n");
  33. fscanf(stdin,"%d",&id_number);
  34. fprintf(fp," %d\n",id_number);
  35.  
  36. for(j=0;j<4;j++)
  37. {
  38. printf("Enter Marks for Subject\n");
  39. scanf("%f",&marks[j]);
  40. total=total+marks[j];
  41. if(marks[j]<50)
  42. fail=fail+1;
  43. // else
  44. // continue;
  45. }
  46. average=avg(total);
  47. total=0.0;
  48. printf("AVERAGE=%f\n",average);
  49. fprintf(fp," %f\n",average);
  50. if(average>=70)
  51. printf("Grade A\n");
  52. else if(average>=60&&average<70)
  53. printf("Grade B\n");
  54. else if(average>=50&&average<60)
  55. printf("Grade C\n");
  56. else if(average>=45&&average<50)
  57. printf("Grade D\n");
  58. else if(average>=40&&average<45)
  59. printf("Grade E\n");
  60. else if(average<40)
  61. printf("Fail\n");
  62. // else
  63. // continue;
  64.  
  65.  
  66.  
  67.  
  68. if(fail>=2)
  69. {
  70. printf("Student is failed and can't promote\n");
  71. //fprintf(fp,"Student is failed and can't promote\n");
  72. }
  73. // else
  74. // continue;
  75. fail=0;
  76.  
  77. }
  78. fclose(fp);
  79. fp=fopen("filename","r");
  80. for(k=1;k<=10;k++)
  81. {
  82. //printf("Input filename");
  83. //scanf("%s",filename);
  84. //fscanf(fp,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %s\tAVERAGE= %f\n",name,course_enrolled,&id_number,date_of_birth,average);
  85. fscanf(fp,"%s %s %s %d %f",name,course_enrolled,date_of_birth, &id_number,&average);
  86. fprintf(stdout,"NAME= %s\tCOURSE= %s\t ID= %d\t DOB= %s\tAVERAGE= %f\n",name,course_enrolled,id_number,date_of_birth,average);
  87.  
  88. if(average < 40)
  89. printf("\n*This student has failed and can't promote*\n");
  90. }
  91. fclose(fp);
  92. getch();
  93. return 0;
  94. }
  95. float avg(float x)
  96. {
  97. float a=0.0;
  98. a=(x/400)*100;
  99. return (a);
  100. }
Last edited by Adak; Jul 22nd, 2008 at 7:38 pm.
Reputation Points: 416
Solved Threads: 181
Nearly a Posting Virtuoso
Adak is offline Offline
1,463 posts
since Jun 2008
Jul 22nd, 2008
0

Re: urgent: modify code please

Thanks alot for solving my problem but in future i will contact you to get more knowledge from you!
thanks alot everyone/
i still have questions about my program (so that i clear my concepts) but i will ask them at morning . bye
again million thanks
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
raja289 is offline Offline
54 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: two letters = one number?
Next Thread in C Forum Timeline: please help with this question.....





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


Follow us on Twitter


© 2011 DaniWeb® LLC