I have the code written, compiling and no errors, I just need it to write to a file now.

This is the code I have.

Is the header piece correct?

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using std::ofstream;
using namespace std;

class finalgrade
{
     char firstName[20];                        //to hold the first name
     char lastName[20];                         //to hold the last name
     int exam1;                                 //to hold Exam-1-Grade
     int exam2;                                 //to hold Exam-2-Grade
     int homeworkavg;                           //to hold Homework-Average
     int finalexamgrade;                        //to hold the Final-Exam-Grade
     double average;                            
     char grade;                                //to hold the students letter 
public:
    static int numStudents;
    static int innerCount;
    void ReadRecord();
    void CalculateGrade();
    void WriteRecord();
    void ReportSummary();
};
int finalgrade::numStudents=0;
int finalgrade::innerCount=0;  
//ReadRecord's function is to read in all of the individual
//Students information and exam grades and store in student_t
void finalgrade::ReadRecord()
 {
cout << "Please enter Students First Name: ";
          cin >> firstName;
          cout << "Please enter student Last name: ";
          cin >> lastName;
          cout << "Please enter the students First Exam score: ";
          cin >> exam1;
          cout << "Please enter the students Second Exam score: ";
          cin >> exam2;
          cout << "Please enter the students Homework Average: ";
          cin >> homeworkavg;
          cout << "Please enter the students Final Exam Grade: ";
          cin >> finalexamgrade;
          numStudents++;
 }
//CalculateGrade's function is to determine the letter grade
//and average grade for the student 
void finalgrade::CalculateGrade()
 {
        double avg=0;
        avg = (exam1*.2)+(exam2*.2)+(homeworkavg*.35)+(.25*finalexamgrade);
        average = avg;                            //send average
         if(avg >= 90)grade='A';                 //assign letter grade as a *single* character
         else if(avg >= 80)grade='B';
         else if(avg >= 70)grade='C';
         else if(avg >= 60)grade='D';
         else grade='F';
 }
//WriteRecord's function is to display all the gathered information on 
//the screen after the averages and letter grades have been assigned
void finalgrade::WriteRecord()
 {
          cout << firstName << "\t";
          cout << lastName << " \t";
          cout << exam1 << " \t";
          cout << exam2 << " \t";
          cout << homeworkavg << " \t";
          cout << average << " \t";
          cout << grade << " \t";
//return 0;
}
int main(void)
{
     finalgrade Students[20];
     //int StudentCount = 0;
     finalgrade::numStudents = 0;
     int iNo;
     do{
     cout<<"Enter No of Students between(0 to 20): Enter 0 to Exit:  ";
     cin>>iNo;
     }while (!(iNo >= 0 && iNo <= 20));
     for(int j=0; j<iNo; j++)
     {
     Students[j].ReadRecord();
     }
     if(finalgrade::numStudents > 0)
     cout << "Sr.No\tFirst Name\tLast Name\tExam #1 Grade\tExam #2 Grade\tHomework Average\tFinal Grade\tGrade\n";
     for(int j=0; j<finalgrade::numStudents; j++)
     {
     Students[j].CalculateGrade();
     cout<<j+1 << "\t";
     Students[j].WriteRecord();
     cout<<"\n";
     }
     system("pause");             //this line was added to cause the screen to pause
     return 0;
}

Any help is appreciated. Thank you.

since finalgrade class does not use any c++ std containers such as std::string you can write the entire class in one statement. put this in main()

ofstream out("file.dat", ios::binary);
   if( out.is_open())
   {
      out.write(Students,sizeof(Students));
      out.close();
   }
}

and to read it back is just as simple

ifstream in("file.dat", ios::binary);
   if( in,is_open())
   {
      in.read(Students,sizeof(Students));
      in.close();
   }
}

The above is simple and quick. The problem with it is that you can't change the size of Students after the file has been written unless you add some additional info about how many student records are in the file then read only that many records.

Where in the Main do I put this? Before or After:

finalgrade Students[20];
//int StudentCount = 0;
finalgrade::numStudents = 0;
int iNo;
do{
cout<<"Enter No of Students between(0 to 20): Enter 0 to Exit: ";
cin>>iNo;
}while (!(iNo >= 0 && iNo <= 20));
for(int j=0; j<iNo; j++)
{
Students[j].ReadRecord();
}
if(finalgrade::numStudents > 0)
cout << "Sr.No\tFirst Name\tLast Name\tExam #1 Grade\tExam #2 Grade\tHomework Average\tFinal Grade\tGrade\n";
for(int j=0; j<finalgrade::numStudents; j++)
{
Students[j].CalculateGrade();
cout<<j+1 << "\t";
Students[j].WriteRecord();
cout<<"\n";
}


Thank you once again.

>>Where in the Main do I put this? Before or After:

put it somewhere then test your program to see if it works the way you want it to. Trial-and-error is part of learning to program.

If I put

ofstream out("file.dat", ios::binary);
if( out.is_open())
{
out.write(Students,sizeof(Students));
out.close();
}
}

Anywhere in the main, I get errors out the waazoo.

Ok, I'm going to first post the code. So here that piece is again.

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using std::ofstream;
using namespace std;



class finalgrade
{
char firstName[20];                        //to hold the first name
char lastName[20];                         //to hold the last name
int exam1;                                 //to hold Exam-1-Grade
int exam2;                                 //to hold Exam-2-Grade
int homeworkavg;                           //to hold Homework-Average
int finalexamgrade;                        //to hold the Final-Exam-Grade
double average;
char grade;                                //to hold the students letter
public:
static int numStudents;
static int innerCount;
void ReadRecord();
void CalculateGrade();
void WriteRecord();
void ReportSummary();
};
int finalgrade::numStudents=0;
int finalgrade::innerCount=0;
//ReadRecord's function is to read in all of the individual
//Students information and exam grades and store in student_t
void finalgrade::ReadRecord()
{
cout << "Please enter Students First Name: ";
cin >> firstName;
cout << "Please enter student Last name: ";
cin >> lastName;
cout << "Please enter the students First Exam score: ";
cin >> exam1;
cout << "Please enter the students Second Exam score: ";
cin >> exam2;
cout << "Please enter the students Homework Average: ";
cin >> homeworkavg;
cout << "Please enter the students Final Exam Grade: ";
cin >> finalexamgrade;


numStudents++;
}
//CalculateGrade's function is to determine the letter grade
//and average grade for the student
void finalgrade::CalculateGrade()
{
double avg=0;
avg = (exam1*.2)+(exam2*.2)+(homeworkavg*.35)+(.25*finalexamgrade);
average = avg;                            //send average
if(avg >= 90)grade='A';                 //assign letter grade as a *single* character
else if(avg >= 80)grade='B';
else if(avg >= 70)grade='C';
else if(avg >= 60)grade='D';
else grade='F';
}
//WriteRecord's function is to display all the gathered information on
//the screen after the averages and letter grades have been assigned
void finalgrade::WriteRecord()
{
cout << firstName << "\t";
cout << lastName << " \t";
cout << exam1 << " \t";
cout << exam2 << " \t";
cout << homeworkavg << " \t";
cout << average << " \t";
cout << grade << " \t";
//return 0;
}
int main(void)
{
finalgrade Students[20];
//int StudentCount = 0;
finalgrade::numStudents = 0;
int iNo;
do{
cout<<"Enter No of Students between(0 to 20): Enter 0 to Exit:  ";
cin>>iNo;
}while (!(iNo >= 0 && iNo <= 20));
for(int j=0; j<iNo; j++)
{
Students[j].ReadRecord();
}
if(finalgrade::numStudents > 0)
cout << "Sr.No\tFirst Name\tLast Name\tExam #1 Grade\tExam #2 Grade\tHomework Average\tFinal Grade\tGrade\n";
for(int j=0; j<finalgrade::numStudents; j++)
{
Students[j].CalculateGrade();
cout<<j+1 << "\t";
Students[j].WriteRecord();
cout<<"\n";
}
system("pause");             //this line was added to cause the screen to pause
return 0;
}

Could I implement(sp) this into the code and have it do what I am looking for?

void Student_t::WriteToDisk()
{
FILE* fpt=fopen("C:\\Testing.txt","r+");
if(fpt!=NULL)
{
char buffer[10];
fseek(fpt,0,SEEK_END);
fputs(firstName,fpt);
fputs("\t",fpt);
fputs(lastName,fpt);
fputs("\t",fpt);
fputs(itoa(exam1,buffer, 10),fpt);
fputs("\t",fpt);
fputs(itoa(exam2,buffer, 10),fpt);
fputs("\t",fpt);
fputs(itoa(homeworkavg,buffer, 10),fpt);
fputs("\t",fpt);
fputs(itoa(finalexamgrade,buffer, 10),fpt);
fputs("\t",fpt);
fputs(average,buffer, 10), fpt);
fputs("\t",fpt);
fputs(grade,fpt);
fputs("\n",fpt);
fclose(fpt);
}
}

If not, is there anything you can help me with to get it to work?

Thank you.

I might do it like this at the end of main() it will write the file, but not read it. You will have to figure out whether you want to read the file back in at the beginning of main() or enter all the data as you do now from the keyboard. It makes little or no sense to do both.

int main(void)
{
finalgrade Students[20];
//int StudentCount = 0;
finalgrade::numStudents = 0;
int iNo;
do{
cout<<"Enter No of Students between(0 to 20): Enter 0 to Exit: ";
cin>>iNo;
}while (!(iNo >= 0 && iNo <= 20));
for(int j=0; j<iNo; j++)
{
Students[j].ReadRecord();
}
if(finalgrade::numStudents > 0)
cout << "Sr.No\tFirst Name\tLast Name\tExam #1 Grade\tExam #2 Grade\tHomework Average\tFinal Grade\tGrade\n";
for(int j=0; j<finalgrade::numStudents; j++)
{
Students[j].CalculateGrade();
cout<<j+1 << "\t";
Students[j].WriteRecord();
cout<<"\n";


ofstream (out,"datafile.dat",ios::binary);
if(out.is_open())
{
    out.write((char*)Students,sizeof(Students));
    out.close();
}

   return 0;
}

[edit]
your function WriteToDisk is wrong --
1. this is a c++ program so use c++ fstreams, not C FILE.

2. The function opens the file for read but you are attempting to write to it. Can't do that.

3. If you want to retain function WriteToDisk() then ignore the code I added in RED above.

4. fputs() writes '\n' at the end of the string -- I don't think that is what you want to do.

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.