c - Programming using Structs -


i having trouble program created project.i keep receiving crash report when try print out grade , name, not sure wrong code. advice needs looked @ again appreciated. still trying pick things go.

#include <stdio.h> #include <string.h> #define sl 30  // gets exam score double getscore() { double x, temp; printf("enter exam score:");  scanf("%lf", &x); if (0.0 > x || x > 100.0) { printf("\ninvaild score. please enter vaild score:"); scanf("%lf", &temp); x = temp; } return x;  }    // averages exam scores double getavg(double a, double b, double c)  { double avg, sum; sum = + b + c; avg = (sum / 3); return avg;  }   // claculates grade  char calcgrade(double avg) { char grade; char a, b, c, d, f; if(avg > 89.5) {grade == a; } else if (avg > 79.5) {grade == b; } else if (avg > 69.5) {grade == c; } else if(avg > 59.5) {grade == d; } else {grade == f; } return grade; }  // calculates gpa  double getgpa(char grade) {  double gpa;  char a, b, c, d;  if (grade == a) { gpa = 4.00; }  else if (grade == b) { gpa = 3.00; } else if  (grade == c) { gpa = 2.00; } else if  (grade == d) { gpa = 1.00; } else  { gpa = 0.00;  }  return gpa;  }  // gets id number int getid() {int temp=0; printf("\nplease enter student id (a negative number exit)--"); scanf("%i", &temp); return temp; }  // struct struct student { int id;    char first[sl];   char last[sl];  double exam1, exam2, exam3;  double avg;  char grade;    };  struct student students[10];      int main()   {int count=0, a=0, b=0, j=0, i=0, k=0; int temp=0, l=0, count2=0;   double class[10];  double exam1[10];  double exam2[10];  double exam3[10];  double oavg, exam1_avg, exam2_avg, exam3_avg;  double cgpa, hold1=0.0, hold2=0.0, hold3=0.0, holdavg=0.0;  double sum1, sum2, sum3, sumg;  char str1[sl], str2[sl], holdg;  char a, b, c, d, f;    {    students[count].id = getid();    if (students[count].id < 0 )   { count=10;    } else    {  printf("\nplease enter first name:" ); scanf("%s", &str1); strcpy (students[count].first, str1); printf("\nplease enter last name:" ); scanf("%s", &str2); strcpy (students[count].last, str2); students[count].exam1 = getscore(); hold1 = students[count].exam1; students[count].exam2 = getscore(); hold2 = students[count].exam2; students[count].exam3 = getscore(); hold3 = students[count].exam3; students[count].avg = getavg(hold1, hold2, hold3); holdavg= students[count].avg; students[count].grade = calcgrade(holdavg); holdg = students[count].grade;  class[count] = getgpa(holdg);  exam1[count] = hold1;  exam2[count] = hold2;  exam3[count] = hold3;   a++;  count++;  }   }    while (count < 10);     (i=0; < a; i++ ) { int sum1=0;  sum1 += exam1[i]; } exam1_avg = (sum1 / i);  (j=0; j < a; j++ ) { int sum2=0;  sum2 += exam2[j]; } exam2_avg = (sum1 / j);  (k=0; k < a; k++ ) { int sum3=0;  sum3 += exam3[k]; } exam3_avg = (sum1 / k);  (l=0; l < a; l++ ) { int sumg=0;  sumg += class[l]; } cgpa = (sumg / l); oavg = ((exam1_avg + exam2_avg + exam3_avg) / 3);   printf("\n-------------------------------------------------------------------");   printf("\nid   exam1   exam2   exam3   average   grade   name");   printf("\n-----------------------------------------------------------------");     {  printf("\n%i", students[count2].id); printf("   %.02lf",students[count2].exam1); printf("   %.02lf",students[count2].exam2); printf("   %.02lf",students[count2].exam3); printf("   %.02lf",students[count2].avg); printf("   %s",students[count2].grade); printf("   %s, %s", students[count2].last, students[count2].first);  count2++;  } while(count2 < a);  printf("\n-------------------------------------------------------------------\n");  printf("\n ---------------------------------------------\n");  printf(" course summary report ");  printf("\n ---------------------------------------------\n");  printf("\texam 1 average\t%lf\n",exam1_avg);  printf("\texam 2 average\t%lf\n",exam2_avg);  printf("\texam 3 average\t%lf\n",exam3_avg);  printf("\toverall average\t%lf\n",oavg);  printf("\tclass gpa\t%lf\n",cgpa);   return 0;  } 

i not sure error is, few things jump out me in calcgrade() , in getgpa(). in both of these functions, declare letter grades char a,b,c,d; yet declares them not initialize them (i.e., give them value); result a,b,c,d not contain values. think mean instead along lines of char a='a',b='b',c='c',d='d'; declaration , assignment give variables actual values.

also in if's of calcgrade(), assignment done = , not ==.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -