OK i need to convert what i wrote so far into a system that simply takes input from a text file instead of from console input upon execution.
HERE's the code, it's very straightforward:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 4 //how many students u will enter
struct student{
char last[100],first[100];
int t1,t2,t3;
float avg;
char grade;
};
void load(struct student s[], int n){
int i;
for(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);
//calc avg
s[i].avg=(s[i].t1+s[i].t2+s[i].t3)/(float)3;
if(s[i].avg>70)
s[i].grade='p';
else
s[i].grade='f';
fflush(stdin);
}
}
void print(struct student s[], int n)
{
for(int i=0;i<n;i++)
{
printf("first name is %s, last name is %s\n", s[i].first, s[i].last);
printf("score 1 is %d, score 2 is %d, score 3 is %d\n", s[i].t1, s[i].t2, s[i].t3);
printf("the average is %f\n", s[i].avg);
printf("The grade is %c\n", s[i].grade);
}
}
void sort(struct student s[], int n)
{
int i, j;
student t;
for(i=0; i<n-1;i++)
for(j=0;j<n-1;j++)
{
if (s[j].avg<s[j+1].avg)//decending order
{
t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
}
}
void passed(struct student s[], int n)
{
int count=0;
for(int i=0;i<n;i++)
{
if(s[i].grade=='p')
count++;
printf("passed count is %d\n", count);
}
}
void main()
{
student s[SIZE];
load (s, SIZE);
sort (s, SIZE);
print (s, SIZE);
passed (s, SIZE);
system("PAUSE");
}
Any idea's on how i can take input from a text file instead of console?