Hi ive developed a code that does 2 things. It is about 20 students doing a test and getting a mark out of 100.
First is using the class, it will look in a file, display the names of all the students(20) and their respective test scores.It will also show their grades(A,B...F for fail etc).
Secondly i created a function which again looks in a file and checks the same data of the student scores but then identifies the highest mark and displays it.
Here is my code: it works fine.
#include <iostream>
#include <fstream>
using namespace std;
void high();
class student
{
private:
char grade;
int calc;
public:
string first;
string last;
int score;
void display(student a);
};
struct highestcalc {
string name;
string last;
float point;
};
void student::display(student a)
{
student end;
cout<<a.first<<" "<<a.last<<endl;
cout<<"score: "<<a.score<<endl;
calc=a.score;
if(calc>=90 && calc<=100)
grade='A';
else if(calc>=80 && calc<=89) grade='B';
else if(calc>=70 && calc<=79) grade='C';
else if(calc>=60 && calc<=69) grade='D';
else grade='F';
cout<<"grade is "<<grade<<endl;
cout<<" "<<endl;
}
void highest(){
int count=0;
int x;
int y=0;
highestcalc pro[100];
ifstream infile;
infile.open("C:\\students.dat");
while(infile.peek()!=EOF) {
infile
>>pro[count].name
>>pro[count].last
>>pro[count].point;
x=pro[count].point;
if(x>y)
y=x;
count++;
}
infile.close();
cout<<"The highest score achieved was "<<y<<endl;
cout<<endl;
}
int main(){
student end;
int count=0;
student test[100];
ifstream infile;
infile.open("C:\\students.dat");
while(infile.peek()!=EOF) {
infile
>>test[count].first
>>test[count].last
>>test[count].score;
count++;
}
infile.close();
for(int i=0; i<20; i++)
end.display(test[i]);
highest();
system("pause");
return 0;
}
as you can see i had to use the "infile" to look in the text(where the student names and results are stored) on both occasions. Once for the main. And once for the function looking for the highest number. My question is, that is it possible to only have the "infile" lookup onthe main()?
i do not wish to "copy and paste" the code so to speak, again for my highest() function from my main().
i also realise if there is a way, it would mean i can get rid off the struct highestcalc.
any help would be much appreciated. The code works fine its just i beleive there is another way and hence making the code look better.