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.

I took you code and made some small changes. Doing it this way fixes what you want to do. I tried to modify your code as little as possible

#include <iostream>
#include <fstream>
using namespace std;

class student
{
      private:
              char grade;
              int calc;
      public:
             string first;
             string last;
             int score;
             //-----------------------
             // Not needed now
             // void display(student a);           
             void display();           
};

void student::display()
{
    cout<<first<<" "<<last<<endl;
    cout<<"score: "<<score<<endl;
    if(score>=90 && score<=100)
    grade='A';
    else if(score>=80 && score<=89) grade='B';
    else if(score>=70 && score<=79) grade='C';
    else if(score>=60 && score<=69) grade='D';
    else grade='F';
    cout<<"grade is "<<grade<<endl;
    cout<<" "<<endl;
}

void highest(student all[],int count){
   int x;
   int y=0;
   for (int i(0); i<count;i++) {
       if(all[i].score>y)
          y=all[i].score;
   }
   cout<<"The highest score achieved was "<<y<<endl;
   cout<<endl;
}

int main(){
    int count=0;
    student test[100];
    ifstream infile;
    infile.open("students.dat");
    while(infile.peek()!=EOF){
       infile>>test[count].first >>test[count].last >>test[count].score;
       //--------------------------------------------------------------
       // Had to put this in because an additional student was being
       // read in at the end because the operator>> leaves the '\n'
       // in the stream
       infile.ignore(numeric_limits<int>::max(),'\n');
       count++;
    }
    infile.close();    
    // -------------------------------------------
    // No need for end.  Just use the test values
    // and add a display function to student class
    for(int i=0; i<count; i++)
      test[i].display(); 
    //-------------------------------------------
    // Change highest to take in the test array
    // and the cound of students in it
    highest(test,count);
    return 0;
}

Input:

$ cat students.dat 
Bill Bob 45
Cat  Dog 90
Foo  Bar 89
$

Output:

$ ./a.out
Bill Bob
score: 45
grade is F

Cat Dog
score: 90
grade is A

Foo Bar
score: 89
grade is B

The highest score achieved was 90
$

thank you very much i understand the changes made.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.