fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<string.h>
  4.  
  5. typedef struct{
  6. char name[64];
  7. int math;
  8. int english;
  9. int physics;
  10. double average;
  11.  
  12. } Student;
  13.  
  14. int main ()
  15. {
  16. int i;
  17.  
  18. Student students[5];
  19.  
  20. // (1) scores.txtを読み込みモード(r)でファイルオープン
  21. FILE *fp = fopen("scores.txt","r");
  22.  
  23. if(fp==NULL){
  24. // scores.txtが開けないことを通知+プログラム終了
  25. printf("Cannot open scores.txt\n");
  26. exit(0);
  27. }
  28. else{
  29. for(i=0;i<5;i++){
  30. // {氏名(char),数学の点数(int),英語の点数(int),物理の点数(int)}を読み込んでstudentsに保存
  31. fscanf(fp,"%s%d%d%d",students[i].name,&students[i].math,&students[i].english,&students[i].physics);
  32. }
  33. // ファイルを閉じる
  34. fclose(fp);
  35. }
  36.  
  37.  
  38. // (2) 3教科の平均の計算とaverage.txtへの書込み
  39. for(i=0;i<5;i++){
  40. // students[i]の平均(average)を計算
  41. students[i].average = (double)(students[i].math + students[i].english + students[i].physics)/3 ;
  42. }
  43.  
  44. // average.txtを書き込みモード(w)でファイルオープン
  45. FILE *wfp = fopen("avarage.txt","w");
  46.  
  47. if(wfp==NULL){
  48. // average.txtが開けないことを通知+プログラム終了
  49. printf("Cannot open average.txt\n");
  50.  
  51. exit(0);
  52. }
  53. else{
  54.  
  55. printf("Writing average to average.txt\n");
  56. // {氏名(char),平均(double)}をaverage.txtへ書込み
  57. for(i=0;i<5;i++){
  58. fprintf(wfp,"%s %lf\n",students[i].name,students[i].average);
  59. }
  60. // ファイルを閉じる
  61. fclose(wfp);
  62. }
  63.  
  64. // (3) 任意の名前でstudents を検索
  65. char name[64];
  66. printf("Put the student name ");
  67. scanf("%s", name);
  68.  
  69. // ここから
  70. Student s;
  71. strcpy(s.name, "nanashi");
  72.  
  73. for(i=0;i<5;i++){
  74. if(strcmp(name, students[i].name)==0){
  75. strcpy(s.name, students[i].name);
  76. s.math = students[i].math;
  77. s.english = students[i].english;
  78. s.physics = students[i].physics;
  79. break;
  80. }
  81. }
  82.  
  83. if(strcmp(s.name, "nanashi")==0){
  84. printf("%s is not found in scores.txt\n", name);
  85. }
  86. else{
  87. printf("%s's scores are; math=%d, english=%d, physics=%d\n", s.name, s.math, s.english, s.physics);
  88. }
  89. // ここまで
  90.  
  91. // find 関数の呼び出し
  92. find(name, students);
  93. }
  94.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:92: error: ‘find’ was not declared in this scope
prog.cpp:31: warning: ignoring return value of ‘int fscanf(FILE*, const char*, ...)’, declared with attribute warn_unused_result
prog.cpp:67: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
stdout
Standard output is empty