Write a C program that will get 10 grades of the student from a file named grades.txt. The program will check each grade if its passing, failing or invalid. Passing grade is 70-100. Failing grade is 0-69. Other value will be considered invalid. Store all the passing grade to an output file named passed.txt. Store all the failing grades to an output file named failed.txt. And store all the invalid grades to another output file named invalid.txt. The program must comply with the given sample output below:
NOTE:
Filename to read: grades.txt
Filename to write: passed.txt, failed.txt, invalid.txt
NOTE:
Your source code must display any of the given sample output below.
It means your source code should be flexible enough to meet any of the given sample output.
Your source code output must be identical to any of the given sample output.
It means you have to strictly follow what are the displayed text, labels, casing of characters in the sample output.
Strictly follow the naming of file.
Sample OUTPUT1:
|
---|
Enter File Name: grades.txt |
All grades are evaluated and stored in their respective files. |
DISPLAY FILE MENU |
[1] Passing |
[2] Failing |
[3] Invalid |
Option: 1 |
Passing Grades: 100, 90, 70, 99, |
Sample OUTPUT2:
|
---|
Enter File Name: grades.txt |
All grades are evaluated and stored in their respective files. |
DISPLAY FILE MENU |
[1] Passing |
[2] Failing |
[3] Invalid |
Option: 2 |
Failing Grades: 50, 0, 69, 1, |
Sample OUTPUT3:
|
---|
Enter File Name: grades.txt |
All grades are evaluated and stored in their respective files. |
DISPLAY FILE MENU |
[1] Passing |
[2] Failing |
[3] Invalid |
Option: 3 |
Invalid Grades: 101, -1, |
Sample OUTPUT4:
|
---|
Enter File Name: grades.txt |
All grades are evaluated and stored in their respective files. |
DISPLAY FILE MENU |
[1] Passing |
[2] Failing |
[3] Invalid |
Option: 4 |
Invalid Option |
Sample OUTPUT5:
|
---|
Enter File Name: grade.txt |
The file can’t be open. File does not exists. |
PLEASE HELP!
How to fix my code? please look at the expected results I'll rate!
My code
#include
#include
#include
int main(){
int temp,opt;
int a[10];
FILE *fp2,*fp3,*fp4;
// open file
FILE *myFile = fopen("grades.txt", "r");
// if opening file fails, print error message and exit 1
if (myFile==NULL){
perror("Error: Failed to open file.");
exit(1);
}
//read values from file until EOF is returned by fscanf
for(int i=0;i<>
// assign the read value to variable (tek), and enter it in array (radica)
if(fscanf(myFile,"%d",&temp) == 1) {
a[i]=temp;
}
else{
break;
}
}
fclose(myFile);
fp2=fopen("passed.txt","w");
fp3=fopen("failed.txt","w");
fp4=fopen("invalid.txt","w");
for (int i=0;i<>
if(a[i]>=70){
putw(a[i],fp2);
}
else if(a[i]>=0||a<>
putw(a[i],fp3);
}
else if(a[i]<0||a>100){
putw(a[i],fp4);
}
}
fclose(fp2);
fclose(fp3);
fclose(fp4);
printf("\n[1] Passing\n[2] Failing\n[3] Invalid\nOption: ");
scanf("%d",&opt);
int x;
if(opt==1){
printf("\nPassing Grades: ");
fp2=fopen("passed.txt","r");
while(x!=-1){
x=fgetc(fp2);
if(x<>
printf("");
else
printf("%d ",x);
}
printf("\n");
}
else if(opt==2){
printf("\nFailing Grades: ");
fp3=fopen("failed.txt","r");
while(x!=-1){
x=fgetc(fp3);
if(x<>
printf("");
else{
printf("%d ",x);
}
}
printf("\n");
}
else if(opt==3){
printf("\nInvalid Grades: ");
fp4=fopen("invalid.txt","r");
while(x!=-1){
x=fgetc(fp3);
printf("%d ",x);
}
printf("\n");
}
else{
printf("\nInvalid Option\n");
}
return 0;
}
=70){ 28 29 - [1] Passing 30 - [2] Failing 31 putw(a[i],fp2); [3] Invalid 32 } Option: else if(a[i]>=0||a<7®){ 33="" -="" putw(a[i],fp3);="" invalid="" option="" 34="" 35="" }="" expected:="" 36="" -="" else="" if(a[i]="">7®){><0|[a>100){ Enter File Name: 37 putw(a[i], fp4); All grades are evaluated and stored in their respective files. 38 } DISPLAY FILE MENU } fclose (fp2); fclose (fp3); fclose (fp4); printf("\n[1] Passing\n[2] Failing\n[3] Invalid\n0ption: "); scanf ("%d", &opt); 39 40 [1] Passing 41 [2] Failing 42 [3] Invalid 43 44 Option: YK Failing Grades: 50, 0, 69, 1, 0% (1:18) C } else if(a[i]<0||a>100){ 35 Check 3 failed 36 - Output: 37 putw (a[i], fp4); 38 } [1] Passing } fclose (fp2); fclose (fp3); fclose (fp4); printf("\n[1] Passing\n[2] Failing\n[3] Invalid\n0ption: "); scanf("%d", &opt); 39 40 [2] Failing 41 [3] Invalid 42 43 Option: 44 Invalid Option 45 int x; Expected: 46 - if(opt==1){ Enter File Name: printf("\nPassing Grades: "); fp2=fopen ("passed.txt",","); 47 48 All grades are evaluated and stored in their respective files. 49 - while(x!=-1) { DISPLAY FILE MENU x=fgetc(fp2); if(x<=0) printf("");="" 50="" [1]="" passing="" 51="" [2]="" failing="" 52="" 53="" else="" [3]="" invalid="" 54="" printf("%d="" ",x);="" option:="" 55="" }="" invalid="" grades:="" 101,="" -1,="" 56="" printf("\n");="" check="" 4="" failed="" }="" else="" if(opt="=2){" 57="" 58="" -="" output:="" printf("\nfailing="" grades:="" ");="" fp3="fopen" ("failed.txt","r");="" while(x!="-1){" 59="" 60="" [1]="" passing="" 61="" -="" [2]="" failing="" x="fgetc(fp3);">=0)><=0) 62="" 63="" [3]="" invalid="" printf("");="" else{="" printf("%d="" ",x);="" }="" 64="" option:="" 65="" -="" invalid="" option="" 66="" 67="" expected:="" 68="" }="" enter="" file="" name:="" 69="" printf("\n");="" all="" grades="" are="" evaluated="" and="" stored="" in="" their="" respective="" files.="" 70="" }="" else="" if(opt="=3){" display="" file="" menu="" 71-="" printf("\ninvalid="" grades:="" ");="" fp4="fopen" ("invalid.txt","r");="" 72="" [1]="" passing="" 73="" [2]="" failing="" 74="" -="" while(x!="-1)" {="" [3]="" invalid="" 75="" x="fgetc(fp3);" option:="" printf("%d="" ",x);="" }="" 76="" 77="" invalid="" option="" 78="" printf("\n");="" check="" 5="" failed="" 0%="" (1:18)="" output:="" display="" file="" menu="" printf("\n[1]="" passing\n[2]="" failing\n[3]="" invalid\n0ption:="" ");="" scanf("%d",="" &opt);="" int="" x;="" 43="" 44="" [1]="" passing="" 45="" [2]="" failing="" 46="" -="" if(opt="=1){" [3]="" invalid="" 47="" printf("\npassing="" grades:="" ");="" fp2="fopen" ("passed.txt","r");="" option:="" 48="" 49="" -="" while(x!="-1){" invalid="" grades:="" 101,="" -1,="" x="fgetc(fp2);">=0)><=0) printf("");="" 50="" check="" 4="" failed="" 51="" output:="" 52="" 53="" else="" 54="" printf("%d="" ",x);="" [1]="" passing="" 55="" }="" [2]="" failing="" 56="" printf("\n");="" [3]="" invalid="" }="" else="" if(opt="=2){" 57="" 58="" -="" option:="" printf("\nfailing="" grades:="" ");="" fp3="fopen("failed.txt","r");" while(x!="-1){" 59="" invalid="" option="" 60="" expected:="" 61="" -="" enter="" file="" name:="" x="fgetc(fp3);">=0)><=0) printf("");="" else{="" printf("%d="" ",x);="" }="" 62="" 63="" all="" grades="" are="" evaluated="" and="" stored="" in="" their="" respective="" files.="" 64="" display="" file="" menu="" 65="" -="" [1]="" passing="" 66="" [2]="" failing="" 67="" 68="" }="" [3]="" invalid="" 69="" printf("\n");="" option:="" }="" else="" if(opt="=3){" 70="" invalid="" option="" 71="" -="" check="" 5="" failed="" printf("\ninvalid="" grades:="" ");="" fp4="fopen("invalid.txt","r");" while(x!="-1){" 72="" 73="" output:="" 74="" -="" 75="" x="fgetc(fp3);" [1]="" passing="" printf("%d="" ",x);="" }="" 76="" 77="" [2]="" failing="" 78="" printf("\n");="" [3]="" invalid="" }="" else{="" printf("\ninvalid="" option\n");="" }="" 79="" option:="" 80="" -="" invalid="" option="" 81="" 82="" expected:="" 83="" return="" 0;="" enter="" file="" name:="" 84="" }="" the="" file="" can't="" be="" open.="" file="" does="" not="" exists.="" show="" diff="" 0%="" (1:18)="" "/="">
Extracted text: #include #include LAST RUN on 3/28/2021, 3:49:40 PM #include int main(){ 3 Check 1 failed 4 . Output: int temp, opt; int a[10]; FILE *fp2, * fp3,*fp4; // open file FILE *myFile = fopen ("grades.txt", "r"); // if opening file fails, print error message and exit 1 if (myFile==NULL){ perror ("Error: Failed to open file."); exit(1); } //read values from file until EOF is returned by fscanf 6 7 [1] Passing 8 [2] Failing 9 [3] Invalid 10 11 - Option: 12 Invalid Option 13 Expected: 14 Enter File Name: 15 All grades are evaluated and stored in their respective files. for (int i=0;i<10;++i){ assign="" the="" read="" value="" to="" variable="" (tek),="" and="" enter="" it="" in="" array="" (radica)="" if(fscanf(myfile,"%d",&temp)="=" 1)="" {="" a[i]="temp;" }="" else{="" break;="" }="" }="" fclose="" (myfile);="" fp2="fopen" ("passed.txt","w");="" fp3="fopen" ("failed.txt","w");="" 16="" -="" 17="" display="" file="" menu="" 18="" -="" [1]="" passing="" [2]="" failing="" 19="" 20="" 21="" -="" [3]="" invalid="" 22="" option:="" 23="" passing="" grades:="" 100,="" 90,="" 70,="" 99,="" 24="" 25="" check="" 2="" failed="" 26="" output:="" 27="" fp4="fopen" ("invalid.txt",="" "w");="" for="" (int="" i="">10;++i){><10;++i){ if(a[i]="">=70){ 28 29 - [1] Passing 30 - [2] Failing 31 putw(a[i],fp2); [3] Invalid 32 } Option: else if(a[i]>=0||a<7®){ 33="" -="" putw(a[i],fp3);="" invalid="" option="" 34="" 35="" }="" expected:="" 36="" -="" else="" if(a[i]="">7®){><0|[a>100){ Enter File Name: 37 putw(a[i], fp4); All grades are evaluated and stored in their respective files. 38 } DISPLAY FILE MENU } fclose (fp2); fclose (fp3); fclose (fp4); printf("\n[1] Passing\n[2] Failing\n[3] Invalid\n0ption: "); scanf ("%d", &opt); 39 40 [1] Passing 41 [2] Failing 42 [3] Invalid 43 44 Option: YK Failing Grades: 50, 0, 69, 1, 0% (1:18) C } else if(a[i]<0||a>100){ 35 Check 3 failed 36 - Output: 37 putw (a[i], fp4); 38 } [1] Passing } fclose (fp2); fclose (fp3); fclose (fp4); printf("\n[1] Passing\n[2] Failing\n[3] Invalid\n0ption: "); scanf("%d", &opt); 39 40 [2] Failing 41 [3] Invalid 42 43 Option: 44 Invalid Option 45 int x; Expected: 46 - if(opt==1){ Enter File Name: printf("\nPassing Grades: "); fp2=fopen ("passed.txt",","); 47 48 All grades are evaluated and stored in their respective files. 49 - while(x!=-1) { DISPLAY FILE MENU x=fgetc(fp2); if(x<=0) printf("");="" 50="" [1]="" passing="" 51="" [2]="" failing="" 52="" 53="" else="" [3]="" invalid="" 54="" printf("%d="" ",x);="" option:="" 55="" }="" invalid="" grades:="" 101,="" -1,="" 56="" printf("\n");="" check="" 4="" failed="" }="" else="" if(opt="=2){" 57="" 58="" -="" output:="" printf("\nfailing="" grades:="" ");="" fp3="fopen" ("failed.txt","r");="" while(x!="-1){" 59="" 60="" [1]="" passing="" 61="" -="" [2]="" failing="" x="fgetc(fp3);">=0)><=0) 62="" 63="" [3]="" invalid="" printf("");="" else{="" printf("%d="" ",x);="" }="" 64="" option:="" 65="" -="" invalid="" option="" 66="" 67="" expected:="" 68="" }="" enter="" file="" name:="" 69="" printf("\n");="" all="" grades="" are="" evaluated="" and="" stored="" in="" their="" respective="" files.="" 70="" }="" else="" if(opt="=3){" display="" file="" menu="" 71-="" printf("\ninvalid="" grades:="" ");="" fp4="fopen" ("invalid.txt","r");="" 72="" [1]="" passing="" 73="" [2]="" failing="" 74="" -="" while(x!="-1)" {="" [3]="" invalid="" 75="" x="fgetc(fp3);" option:="" printf("%d="" ",x);="" }="" 76="" 77="" invalid="" option="" 78="" printf("\n");="" check="" 5="" failed="" 0%="" (1:18)="" output:="" display="" file="" menu="" printf("\n[1]="" passing\n[2]="" failing\n[3]="" invalid\n0ption:="" ");="" scanf("%d",="" &opt);="" int="" x;="" 43="" 44="" [1]="" passing="" 45="" [2]="" failing="" 46="" -="" if(opt="=1){" [3]="" invalid="" 47="" printf("\npassing="" grades:="" ");="" fp2="fopen" ("passed.txt","r");="" option:="" 48="" 49="" -="" while(x!="-1){" invalid="" grades:="" 101,="" -1,="" x="fgetc(fp2);">=0)><=0) printf("");="" 50="" check="" 4="" failed="" 51="" output:="" 52="" 53="" else="" 54="" printf("%d="" ",x);="" [1]="" passing="" 55="" }="" [2]="" failing="" 56="" printf("\n");="" [3]="" invalid="" }="" else="" if(opt="=2){" 57="" 58="" -="" option:="" printf("\nfailing="" grades:="" ");="" fp3="fopen("failed.txt","r");" while(x!="-1){" 59="" invalid="" option="" 60="" expected:="" 61="" -="" enter="" file="" name:="" x="fgetc(fp3);">=0)><=0) printf(""); else{ printf("%d ",x); } 62 63 all grades are evaluated and stored in their respective files. 64 display file menu 65 - [1] passing 66 [2] failing 67 68 } [3] invalid 69 printf("\n"); option: } else if(opt==3){ 70 invalid option 71 - check 5 failed printf("\ninvalid grades: "); fp4=fopen("invalid.txt","r"); while(x!=-1){ 72 73 output: 74 - 75 x=fgetc(fp3); [1] passing printf("%d ",x); } 76 77 [2] failing 78 printf("\n"); [3] invalid } else{ printf("\ninvalid option\n"); } 79 option: 80 - invalid option 81 82 expected: 83 return 0; enter file name: 84 } the file can't be open. file does not exists. show diff 0% (1:18) printf("");="" else{="" printf("%d="" ",x);="" }="" 62="" 63="" all="" grades="" are="" evaluated="" and="" stored="" in="" their="" respective="" files.="" 64="" display="" file="" menu="" 65="" -="" [1]="" passing="" 66="" [2]="" failing="" 67="" 68="" }="" [3]="" invalid="" 69="" printf("\n");="" option:="" }="" else="" if(opt="=3){" 70="" invalid="" option="" 71="" -="" check="" 5="" failed="" printf("\ninvalid="" grades:="" ");="" fp4="fopen("invalid.txt","r");" while(x!="-1){" 72="" 73="" output:="" 74="" -="" 75="" x="fgetc(fp3);" [1]="" passing="" printf("%d="" ",x);="" }="" 76="" 77="" [2]="" failing="" 78="" printf("\n");="" [3]="" invalid="" }="" else{="" printf("\ninvalid="" option\n");="" }="" 79="" option:="" 80="" -="" invalid="" option="" 81="" 82="" expected:="" 83="" return="" 0;="" enter="" file="" name:="" 84="" }="" the="" file="" can't="" be="" open.="" file="" does="" not="" exists.="" show="" diff="" 0%="">=0) printf(""); else{ printf("%d ",x); } 62 63 all grades are evaluated and stored in their respective files. 64 display file menu 65 - [1] passing 66 [2] failing 67 68 } [3] invalid 69 printf("\n"); option: } else if(opt==3){ 70 invalid option 71 - check 5 failed printf("\ninvalid grades: "); fp4=fopen("invalid.txt","r"); while(x!=-1){ 72 73 output: 74 - 75 x=fgetc(fp3); [1] passing printf("%d ",x); } 76 77 [2] failing 78 printf("\n"); [3] invalid } else{ printf("\ninvalid option\n"); } 79 option: 80 - invalid option 81 82 expected: 83 return 0; enter file name: 84 } the file can't be open. file does not exists. show diff 0% (1:18)>0||a>0|[a>10;++i){>=0)>0||a>0|[a>0||a>