
Amelia G.
asked 06/21/22Need assistance with C code, not running
#include <stdio.h>
struct students{ //struct student
char FirstName; //String to store first name
char secondName; //string to store second name
int ID; //student ID
float GPA; //student gpa
char Progression; //Studnet progression status
};
int main()
{
struct students sp[10] ;
FILE *fp;
fp = fopen("YearOneICT.txt", "r");
if (fp == NULL)
{
printf("File not found");
return 0;
}
int students[10]; // array to h
int i = 0;
while (fscanf(fp, "%s %s %d %f", &sp[i].FirstName, &sp[i].secondName, &sp[i].ID, &sp[i].GPA) != EOF)
{
i++;
}
fclose(fp);
int student[10]; // array to h
for(int i =0;i<10;i++){
if(sp[i].GPA>3.8) {sp[i].Progression = 'H'; sp[i] = 1;} //Decide studens progression depeding on their GPA
else
if(sp[i].GPA>=2.0 && sp[i].GPA<=3.8) sp[i].Progression = 'N'; sp[i] = 0;}
else {sp[i].Progression = 'P'; sp[i] = -1;}
FILE *fp1;
fp1 = fopen("ClassSummary.txt", "w");
if (fp1 == NULL)
{
printf("File not found");
return 0;
}
fprintf(fp1,"Honours List\n");
for(int i =0;i<10;i++){
if(sp[i].Progression=='H'){
fprintf(fp1, "%s %s %d %f", sp[i].FirstName, sp[i].secondName, sp[i].ID, sp[i].GPA));
printf("%s %s %d %f", sp[i].FirstName, sp[i].secondName, sp[i].ID, sp[i].GPA);
}
}
fprintf(fp1,"Progessing Normally\n");
for(int i =0;i<10;i++){
if(sp[i]=='N'){
fprintf(fp1, "%s %s %d %f", sp[i].FirstName, sp[i].secondName, sp[i].ID, sp[i].GPA));
printf("%s %s %d %f", sp[i].FirstName, sp[i].secondName, sp[i].ID, sp[i].GPA);
}
}
fprintf(fp1,"Academic Probation\n");
for(int i =0;i<10;i++){
if(sp[i].Progression=='P'){
fprintf(fp1, "%s %s %d %f", sp[i].FirstName, sp[i].secondName, sp[i].ID, sp[i].GPA);
printf("%s %s %d %f", sp[i].FirstName, sp[i].secondName, sp[i].ID, sp[i].GPA);
}
}
return 0;
}
The data for YearOneICT.txt is as follows:
Alan Bob 13176 3.65
Brad Chance 11198 1.71
Chelsea Donald 39528 2.14
David Edwards 22469 3.88
Elenor Franks 44938 3.46
Fred Guess 11246 3.50
Gavin Hill 79056 1.20
Helen Indar 69360 3.20
Ian James 44888 2.58
Jenny Kahn 34168 1.83
The program aims to capture the data from the text file and generate an output file with the format:
ClassSummary.txt
Honours List:
David Edwards 22469 3.88
Progressing Normally:
Alan Bob 13176 3.65
Chelsea Donald 39528 2.14 …
Academic Probation:
Brad Chance 11198 1.71 …
1 Expert Answer

Anita Z. answered 06/23/22
Computer Science and C/C++/C# Tutor; ESL/ESOL Tutor
Hi Amelia,
Here is the part of your code which has syntax errors:
sp[i].Progression = 'H';
sp[i] = 1; // ERR: cannot set sp[i] equal to an int, check members of sp
} //Decide studens progression depeding on their GPA
else if(sp[i].GPA>=2.0 && sp[i].GPA<=3.8) // ERR: forgot open {
sp[i].Progression = 'N';
sp[i] = 0; // ERR: cannot set sp[i] equal to an int, check members of sp
}
else {
sp[i].Progression = 'P';
sp[i] = -1;
}
fp1 = fopen("ClassSummary.txt", "w");
if (fp1 == NULL)
{
printf("File not found");
return 0;
}
fprintf(fp1,"Honours List\n");
for(int i =0;i<10;i++){
if(sp[i].Progression=='H'){
fprintf(fp1, "%s %s %d %f", sp[i].FirstName, sp[i].secondName, sp[i].ID, sp[i].GPA));// ERR: extra )
printf("%s %s %d %f", sp[i].FirstName, sp[i].secondName, sp[i].ID, sp[i].GPA);
}
}
fprintf(fp1,"Progessing Normally\n");
for(int i =0;i<10;i++){
if(sp[i] == 'N'){ // ERR: cannot set sp[i] equal to a char, check members of sp
fprintf(fp1, "%s %s %d %f", sp[i].FirstName, sp[i].secondName, sp[i].ID, sp[i].GPA)); // ERR: extra )
printf("%s %s %d %f", sp[i].FirstName, sp[i].secondName, sp[i].ID, sp[i].GPA);
If you compile this code, you should see these errors too, either in your IDE or at the command line, depending on how you are compiling. I made comments in the code showing you what is wrong. These are syntax errors. Once the syntax errors are handled, then you can run the code and see if your logic works.
I hope this helps.
Anita
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Donald W.
There are a lot of complier errors in the code and, in some cases, it's not clear what the intention is. For example, what is "sp[i] = -1;" supposed to do? sp is an array of students objects, so you can't assign an integer to it. I think it's best that you find a tutor to help you fix this together so you can learn how to fix these issues yourself at the same time.06/22/22