Hi,
I've learned structures in class, and I tried out an example given by the teacher. I'm using MS Visual Studio C++ 2008 express. I think I have the code he gave written correctly.
Here's the code, you enter in student's first, last name, three test scores and avg is calculated. It's using a structure and some functions. I think it should work, but when I try running the program, there's no compiling error, but command prompt always ends up with an error box, saying the exe crashed or something like that and program need to be closed. Did I do something wrong?? THANKS!!
Code:
#include<stdio.h>
#define SIZE 10
struct Student
{
char last[15], first [10];
int t1, t2, t3;
float avg;
};
void load(struct Student s[], int n)
{
for(int i=0; i<n; i++)
{
printf("Enter last name: ");
gets(s[i].last);
printf("Enter first name: ");
gets(s[i].first);
printf("Enter 3 scores: ");
scanf("%d%d%d", s[i].t1, s[i].t2, s[i].t3);
s[i].avg = (s[i].t1 + s[i].t2 + s[i].t3) / (float)3;
fflush(stdin);
}
}
void print(struct Student s[], int n)
{
for(int i=0; i<n; i++)
{
printf("%s %s\n", s[i].first, s[i].last);
printf("Test scores are: %d %d %d\n", s[i].t1, s[i].t2, s[i].t3);
printf("Avg is: %f\n\n\n", s[i].avg);
}
}
void main()
{
Student s[SIZE];
load(s, SIZE);
print(s, SIZE);
}