I Have written this code for a programming assignment in a C/C++ class and I can
ID: 3624435 • Letter: I
Question
I Have written this code for a programming assignment in a C/C++ class and I can not find were it is having problems******************
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
char last_name[20];
char frist_name[20];
float points;
char grade;
};
void compute_grade(struct student list[], int n)
{
int i;
for(i=0; i>n; i++)
{
if(list[i].points>=90 && list[i].points<=100)
list[i].grade='A';
else if(list[i].points>=80 &&list[i].points<90)
list[i].grade='B';
else if(list[i].points>=70 && list[i].points<80)
list[i].grade='C';
else if(list[i].points>=60 &&list[i].points<70)
list[i].grade='F';
}/*end for*/
}/*end of comput grade*/
void swap(struct student * s1, struct student * s2)
{
float t;
char te;
char temp_last[20];
char temp_frist[20];
strcpy(temp_last,(*s1).last_name);
strcpy(temp_frist, (*s1).frist_name);
te = (*s1).grade;
t = (*s1).points;
strcpy((*s1).last_name,(*s2).last_name);
strcpy((*s1).frist_name,(*s2).frist_name);
(*s1).grade=(*s2).grade;
(*s1).points=(*s2).points;
strcpy((*s2).last_name, temp_last);
strcpy((*s2).frist_name, temp_frist);
(*s2).grade = te;
(*s2).points = t;
}
void sort(struct student list[], int n)
{
int j,i;
for(i=0; i< n; i++)
for(j=i+1; j<n; j++)
if(strcmp(list[i].last_name, list[j].last_name) > 0)
swap(&list[i], &list[j]);
}
int main(int argc, char * argv[])
{
struct student list[100];
int n , check, i, len;
n=0;
len=0;
FILE * fp1;
fp1=fopen(argv[1], "r");
if(fp1==NULL)
{
printf("File does not exist ");
exit(0);
}
check=fscanf(fp1, "%s %s %d",list[n].last_name,
list[n].frist_name, list[n].points);
if(check==EOF)
{
printf("The file is empy");
exit(0);
}
while(check!=EOF)
{
n++;
check=fscanf(fp1, "%s %s %d",list[n].last_name,
list[n].frist_name, list[n].points);
}
compute_grade(list,n);
sort(list, n);
printf(" Grade list for CS-XXXX ");
printf(" Last Name First Name Points Grade ");
for(i=0;i<n; i++)
{
len=strlen(list[i].last_name);
while(len<19)
{
strcat(list[i].last_name," ");
len++;
}
list[i].last_name[19]='';
len=strlen(list[i].frist_name);
while(len<19)
{
strcat(list[i].frist_name," ");
len++;
}
list[i].frist_name[19]='';
printf("%s %s %5d %c ",list[i].last_name,
list[i].frist_name, list[i].points, list[i].grade);
}
fclose(fp1);
return 0;
}
**********************
I think the problems with how I pass the list but I am not sure the error when I put the the FILE with the following data
Smith John 90.00
Willis Josh 60.50
Apple Kate 75.33
Ford Mary 85.45
is a Segmentation fault
not very much help I know
any step in the right direction will be a great help.
Explanation / Answer
please rate - thanks
the grade is still wrong, but pretty good for first shot--poits you input as int, they are float among other things
I tried to highlight all the lines I changed
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
char last_name[20];
char frist_name[20];
float points;
char grade;
};
void compute_grade(struct student list[], int n)
{
int i;
for(i=0; i>n; i++)
{
if(list[i].points>=90 && list[i].points<=100)
list[i].grade='A';
else if(list[i].points>=80 &&list[i].points<90)
list[i].grade='B';
else if(list[i].points>=70 && list[i].points<80)
list[i].grade='C';
else if(list[i].points>=60 &&list[i].points<70)
list[i].grade='F';
}/*end for*/
}/*end of comput grade*/
void swap(struct student * s1, struct student * s2)
{
float t;
char te;
char temp_last[20];
char temp_frist[20];
strcpy(temp_last,(*s1).last_name);
strcpy(temp_frist, (*s1).frist_name);
te = (*s1).grade;
t = (*s1).points;
strcpy((*s1).last_name,(*s2).last_name);
strcpy((*s1).frist_name,(*s2).frist_name);
(*s1).grade=(*s2).grade;
(*s1).points=(*s2).points;
strcpy((*s2).last_name, temp_last);
strcpy((*s2).frist_name, temp_frist);
(*s2).grade = te;
(*s2).points = t;
}
void sort(struct student list[], int n)
{
int j,i;
for(i=0; i< n; i++)
for(j=i+1; j<n; j++)
if(strcmp(list[i].last_name, list[j].last_name) > 0)
swap(&list[i], &list[j]);
}
int main(int argc, char * argv[])
{
struct student list[100];
int n , check, i, len;
n=0;
len=0;
FILE * fp1;
fp1=fopen(argv[1], "r");
if(fp1==NULL)
{
printf("File does not exist ");
exit(0);
}
check=fscanf(fp1, "%s",&list[n].last_name);
if(check==EOF)
{
printf("The file is empy");
exit(0);
}
while(check!=EOF)
{fscanf(fp1,"%s %f",&list[n].frist_name, &list[n].points);
n++;
check=fscanf(fp1, "%s",&list[n].last_name);
}
compute_grade(list,n);
sort(list, n);
printf(" Grade list for CS-XXXX ");
printf(" Last Name First Name Points Grade ");
for(i=0;i<n; i++)
{
len=strlen(list[i].last_name);
while(len<19)
{
strcat(list[i].last_name," ");
len++;
}
list[i].last_name[19]='';
len=strlen(list[i].frist_name);
while(len<19)
{
strcat(list[i].frist_name," ");
len++;
}
list[i].frist_name[19]='';
printf("%s %s %5.2f %c ",list[i].last_name,
list[i].frist_name, list[i].points, list[i].grade);
}
fclose(fp1);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.