Hi...
I worked on the code, finished it. Unfortunately the program now stays in loop infinitely, and as far as I can notice, it doesn't display the data. I think I messed up with num and course, but can't figure out what... Could someone tell me what I did wrong?
Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
/* type definitions */
struct StudentData {
int idnum;
char lname[16];
char fname[16];
char code[6];
int credits;
char grade[2];
int gradenum;
double gradecredit;
};
typedef struct StudentData DATASTU;
/* function prototypes */
void CalcGradeCredit ( struct StudentData students[], int );
void display ( int, int, int, double, struct StudentData students[] );
int main( void )
{
int TotalSemHrCompl = 0;
int num = 0;
int i = 0;
double Sem_gpa = 0.0;
DATASTU students[1000] = {0};
FILE *incoming;
if( (incoming = fopen( "student.dat", "r" )) != NULL )
{
char buff[BUFSIZ];
while ( fgets( buff, BUFSIZ, incoming) != NULL )
{
int res = 0;
res = sscanf( buff, "%d %s %s %s %d %s",
&students[num].idnum, students[num].lname, students[num].fname,
students[num].code, &students[num].credits, students[num].grade );
if ( res != 6 )
{
printf( "Skipping bad record %s", buff );
}
else
{
num++;
}
}
fclose( incoming );
}
else
{
printf( "Could not open the file\n" );
return 1;
}
CalcGradeCredit( students, num );
display ( i, num, TotalSemHrCompl, Sem_gpa, students );
return 0;
}
/***************************************** CalcGradeCredit() **************************/
void CalcGradeCredit ( struct StudentData students[], int num )
{
int i;
for ( i = 0 ; i < num; i++ )
{
switch ( students[i].grade[0] )
{
case 'A':
case 'a': students[i].gradenum = 4; break;
case 'B':
case 'b': students[i].gradenum = 3; break;
case 'C':
case 'c': students[i].gradenum = 2; break;
case 'D':
case 'd': students[i].gradenum = 1; break;
}
students[i].gradecredit = students[i].gradenum * students[i].credits;
}
}
/*******************************8 display() ********************************************/
void display ( int i, int num, int TotalSemHrCompl, double Sem_gpa, struct StudentData students[] )
{
for (i = 0; i < num; i++)
{
int course = 0;
if (students[num].idnum == students[i+1].idnum)
{
course = num;
num = num+1;
printf( "Student Name: %s %s\n", students[course].lname, students[course].fname);
printf( "Student ID number: %d\n\n", students[course].idnum);
printf( "Course\t\tCourse\t\tCourse\n");
printf( " Code\t\tCredits\t\tGrade\n");
printf( "%s\t\t%d\t\t%s\n", students[course].code, students[course].credits, students[course].grade );
TotalSemHrCompl = students[num - 1].credits + students[num].credits;
printf( "Total Semester Course Credits Completed: %d\n", TotalSemHrCompl );
Sem_gpa = (students[num - 1].gradecredit + students[num].gradecredit) / TotalSemHrCompl;
printf( "Semester Grade Point Average: %.2f\n", Sem_gpa );
}
else course = 1;
}
}
Thanks...