I have been struggling with a program that calls functions which return strings. I got some ideas which seemed to work but the program still crashed. Here is part of the code.
printf("CSC 1100 %d %s %f %s\n ",fm_csc_1100[i] ,grades(fm_csc_1100[i]),gradepoint(fm_csc_1100[i]),course_comment(gradepoint(fm_csc_1100[i])) );
printf("CSK 1101 %d %s %f %s\n ",fm_csk_1101[i] ,grades(fm_csk_1101[i]),gradepoint(fm_csk_1101[i]),course_comment(gradepoint(fm_csk_1101[i])) );
The program uses a for loop to display marks of students. The marks have been entered in the earlier part of the program. The function calls work fine and it displays the first printf line and then it crashes. The functions which return strings are grades and course_ comment.Here is there code.
char *grades(int z)
{ char *temp3 = "A+";
char *temp4 = "A";
char *temp5 = "B+";
char *temp6 = "B";
char *temp7 = "C+";
char *temp8 = "C";
char *temp9 = "D+";
char *temp10 = "D";
char *temp11 = "E";
char *temp12 = "E-";
char *temp13 = "F";
if(z >= 90)
return temp3;
else if (z >= 80 && z<=89)
return temp4;
else if (z >= 75 && z<=79)
return temp5;
else if (z >= 70 && z<=74)
return temp6;
else if (z >= 65 && z<=69)
return temp7;
else if (z >= 60 && z<=64)
return temp8;
else if (z >= 55 && z<=59)
return temp9;
else if (z >= 50 && z<=54)
return temp10;
else if (z >= 45 && z<=49)
return temp11;
else if (z >= 40 && z<=44)
return temp12;
else
return temp13;
}
Grades is messy but i had no better way of doing it.
char *course_comment(float b)
{ char *temp ="Retake";
if(b < 2.0)
return temp;
}
Another function i call in the latter parts of the program though it doesnt get there since it crashes is.
char *student_comment(float c)
{
char *temp1 = "Progress";
char *temp2 ="Stay Put" ;
if (c > 2.0)
return temp1;
else
return temp2;
}
All these return strings and the program compiles and runs fine but crashes. How can i handle these functions correctly?