urgent: modify code please

Thread Solved

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

urgent: modify code please

 
0
  #1
Jul 10th, 2008
http://s3.supload.com/free/lastscan-...3716.jpg/view/
up there is description of code
and here;'s code


  1. #include<stdio.h>
  2. #include<string.h>
  3. float avg(float);
  4. main()
  5. {
  6. FILE *p[2];
  7. int k,students=10,subjects=4,i,j,id_number,date_of_birth,fail=0;
  8. float marks[100],total=0,avg;
  9. char name[30],course_enrolled[30],filename[10];
  10. for(i=1;i<=2;i++)
  11. {
  12. printf("filename");
  13. scanf("%s",&filename);
  14. p[i]=fopen(filename,"w");
  15. printf("Enter Name of Student\n",p[i]);
  16. fscanf(stdin,"%s",&name,p[i]);
  17. printf("Enter Course In Which Student Is Enrolled\n",p[i]);
  18. fscanf(stdin,"%s",&course_enrolled,p[i]);
  19. printf("Enter ID Number Of %s Enrolled in Course %s\n",name,course_enrolled,p[i]);
  20. fscanf(stdin,"%d",&id_number,p[i]);
  21. printf("Enter Date Of Birth Of %s Enrolled in Course %s and\n ID-Number is %d\n",name,course_enrolled,id_number,p[i]);
  22. fscanf(stdin,"%d",&date_of_birth,p[i]);
  23. for(j=1;j<=4;j++)
  24. {
  25. printf("Enter Marks for Subject %d for Student %s\n",j,name);
  26. fscanf(stdin,"%f",&marks[j]);
  27. total=total+marks[j];
  28. if(marks[j]<50)
  29. fail=fail+1;
  30. else
  31. continue;
  32. }
  33. avg=(total/400)*100;
  34. printf("AVERAGE=%f\n",avg);
  35. if(avg>=70)
  36. printf("Grade A\n");
  37. if(avg>=60&avg<70)
  38. printf("Grade B\n");
  39. else if(avg>=50&avg<60)
  40. printf("Grade C\n");
  41. else if(avg>=45&avg<50)
  42. printf("Grade D\n");
  43. else if(avg>=40&avg<45)
  44. printf("Grade E\n");
  45. else if(avg<40)
  46. printf("Fail\n");
  47. if(fail>=2)
  48. printf("Student is failed and cann;t promote\n");
  49. else
  50. continue;
  51. fclose (p[i]);
  52. }
  53. for(k=1;k<=2;k++)
  54. {
  55. printf("Input filename");
  56. scanf("%s",&filename);
  57. p[k]=fopen(filename,"r");
  58. fprintf(stdout,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %d\tAVERAGE= %f\n",name,course_enrolled,id_number,date_of_birth,avg);
  59. fclose (p[k]);
  60. }
  61. getch();
  62. }
Last edited by Ancient Dragon; Jul 10th, 2008 at 6:26 am. Reason: corrected code and img tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
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: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: urgent: modify code please

 
0
  #2
Jul 10th, 2008
The link doesn't work and you didn't describe the problem with your code.
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
  #3
Jul 10th, 2008
oh sorry for that link !
here;s another link
http://www.MegaShare.com/443191

and the main problem with code is that
when i save data in different files and ask them to fetch the same data from files but it just show me the data of last file no matters if i enter the name of first file

and it is adding average of last student with the current one

and i want to use 1 user defined function here can u tell me which function should i make except average !
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
  #4
Jul 10th, 2008
see the link and according to that please suggest me any changes

thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
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: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: urgent: modify code please

 
0
  #5
Jul 10th, 2008
The indentation of your program is terrible -- hopefully it was just a problem with posting here. So here is a corrected copy
  1. #include<stdio.h>
  2. #include<string.h>
  3. #pragma warning(disable: 4996)
  4.  
  5. float avg(float);
  6. main()
  7. {
  8. FILE *p[2];
  9. int k,students=10,subjects=4,i,j,id_number,date_of_birth,fail=0;
  10. float marks[100],total=0,avg;
  11. char name[30],course_enrolled[30],filename[10];
  12. for(i=1;i<=2;i++)
  13. {
  14. printf("filename");
  15. scanf("%s",&filename);
  16. p[i]=fopen(filename,"w");
  17. printf("Enter Name of Student\n",p[i]);
  18. fscanf(stdin,"%s",&name,p[i]);
  19. printf("Enter Course In Which Student Is Enrolled\n",p[i]);
  20. fscanf(stdin,"%s",&course_enrolled,p[i]);
  21. printf("Enter ID Number Of %s Enrolled in Course %s\n",name,course_enrolled,p[i]);
  22. fscanf(stdin,"%d",&id_number,p[i]);
  23. printf("Enter Date Of Birth Of %s Enrolled in Course %s and\n ID-Number is %d\n",name,course_enrolled,id_number,p[i]);
  24. fscanf(stdin,"%d",&date_of_birth,p[i]);
  25. for(j=1;j<=4;j++)
  26. {
  27. printf("Enter Marks for Subject %d for Student %s\n",j,name);
  28. fscanf(stdin,"%f",&marks[j]);
  29. total=total+marks[j];
  30. if(marks[j]<50)
  31. fail=fail+1;
  32. else
  33. continue;
  34. }
  35. avg=(total/400)*100;
  36. printf("AVERAGE=%f\n",avg);
  37. if(avg>=70)
  38. printf("Grade A\n");
  39. if(avg>=60&avg<70)
  40. printf("Grade B\n");
  41. else if(avg>=50&avg<60)
  42. printf("Grade C\n");
  43. else if(avg>=45&avg<50)
  44. printf("Grade D\n");
  45. else if(avg>=40&avg<45)
  46. printf("Grade E\n");
  47. else if(avg<40)
  48. printf("Fail\n");
  49. if(fail>=2)
  50. printf("Student is failed and cann;t promote\n");
  51. else
  52. continue;
  53. fclose (p[i]);
  54. }
  55. for(k=1;k<=2;k++)
  56. {
  57. printf("Input filename");
  58. scanf("%s",&filename);
  59. p[k]=fopen(filename,"r");
  60. fprintf(stdout,"NAME= %s\tCOURSE ENTITLLED= %s\t ID NUMBER= %d\t DATE OF BIRTH= %d\tAVERAGE= %f\n",name,course_enrolled,id_number,date_of_birth,avg);
  61. fclose (p[k]);
  62. }
  63. getch();
  64. }

When I compiled it using VC++ 2008 Express I got several warnings which you need to correct. NEVER EVER ignore warnings because most of the time they are really errors.

>>test1.c(39) : warning C4554: '&' : check operator precedence for possible error; use parentheses to clarify precedence

That warning message tells you to look at line 39 and check of the & operator is correct. Its not what you want -- you wanted the boolean && operator, not the bitwise & operator. The code has similar problem on other lines.

Another problem: lines 15 and 58: remove the & address operator before filename. Character arrays like filename are always passed by address, and adding the & address operator passes a pointer to a pointer, which is not what you want.

I don't have your input file(s) so I can't run the program to test it out.
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: Aug 2005
Posts: 15,401
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: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: urgent: modify code please

 
0
  #6
Jul 10th, 2008
About the files: I see now that your program creates them. Its not necessary to have an array of file pointers, e.g. FILE *p[2]; . All you need is one and just reuse it. FILE* p;
You are asking people to enter the birth date as a single integer -- I don't know about you but I like to enter my birthdate in MM/DD/YYYY or MM-DD-YYYY format and you can't do that with your program. Suggest you change the data type of birthdate to a character array so that you can enter it in normal fashion.
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: Aug 2005
Posts: 15,401
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: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: urgent: modify code please

 
0
  #7
Jul 10th, 2008
>>when i save data in different files and ask them to fetch the same data from files but it just show me the data of last file no matters if i enter the name of first file

You have to add some more code to read the file after it is opened on line 62. Your program opens the file then just ignores it.

>>and it is adding average of last student with the current one
You have to reinitialize total to 0 before starting the loop on line 25.
Last edited by Ancient Dragon; Jul 10th, 2008 at 7:20 am.
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
  #8
Jul 10th, 2008
thanks alot for your useful suggestions i am gonna work on it now
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
  #9
Jul 11th, 2008
>> You have to add some more code to read the file after it is opened on line 62. Your program opens the file then just ignores it.





can u give me any idea or guess about what type of code i will have to write !
thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
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: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: urgent: modify code please

 
0
  #10
Jul 11th, 2008
Actually, now that I look at your program closer, your program doesn't write anything to those files. In post #5 above, the program opens the file on line 16 and closes it on line 53, never writes anything to it inbetween those two lines. So, somewhere before line 53 you have to add code to write the data to the file. What to write? Look at line 60 -- you have to write those entries (name, course title, id number, dob, and average) to the file. Then after line 59 you have to insert another line or so that reads those same variables from the file. Use fscanf() or fgets() to do that.

[icode]
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  
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