urgent: modify code please

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: urgent: modify code please

 
0
  #31
Jul 22nd, 2008
Originally Posted by raja289 View Post
>>>>>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 ???
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 41
Reputation: raja289 is an unknown quantity at this point 
Solved Threads: 0
raja289 raja289 is offline Offline
Light Poster

Re: urgent: modify code please

 
0
  #32
Jul 22nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 41
Reputation: raja289 is an unknown quantity at this point 
Solved Threads: 0
raja289 raja289 is offline Offline
Light Poster

Re: urgent: modify code please

 
0
  #33
Jul 22nd, 2008
Oh sorry
results are same !
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 41
Reputation: raja289 is an unknown quantity at this point 
Solved Threads: 0
raja289 raja289 is offline Offline
Light Poster

Re: urgent: modify code please

 
0
  #34
Jul 22nd, 2008
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 .
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 41
Reputation: raja289 is an unknown quantity at this point 
Solved Threads: 0
raja289 raja289 is offline Offline
Light Poster

Re: urgent: modify code please

 
0
  #35
Jul 22nd, 2008
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 41
Reputation: raja289 is an unknown quantity at this point 
Solved Threads: 0
raja289 raja289 is offline Offline
Light Poster

Re: urgent: modify code please

 
0
  #36
Jul 22nd, 2008
?????
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: urgent: modify code please

 
0
  #37
Jul 22nd, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 79
Reputation: Adak is an unknown quantity at this point 
Solved Threads: 7
Adak Adak is offline Offline
Junior Poster in Training

Re: urgent: modify code please

 
0
  #38
Jul 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 41
Reputation: raja289 is an unknown quantity at this point 
Solved Threads: 0
raja289 raja289 is offline Offline
Light Poster

Re: urgent: modify code please

 
0
  #39
Jul 22nd, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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