C++ Question

Creat a text file that contains student number ( 9-digit number ) , year ( one of the four years: Senior denoted by SE, Junior denoted by JU, Sophomore denoted by SO or Freshman denoted by FR ) and GPA ( a floating point number greater than 0.0 ) . Add at least two students. For example , Your text file may contain entries as follow :
201212354 JU 3.5
201071256 SE 2.8
Then write a C++ program that uses the file you created above to do the following :
1) Display all the contents of the file in a tabular format with the first row being a header row.
2) write a file that is called StudentGPA.txt only the GPA from the read file.
3) Find out the average GPA

This means breaking your code up into managable bites. What you did (shoving everything into main) is like trying to eat a sandwich in one big bite. Your are going to choke, break it into smaller chunks!

In this case you should create a bunch of helper functions to break up the task into managable segments, like so:

I understand if you do not know about dynamic memory allocation yet, but this problem is easiest if you just use a buffer to store your student gpa's so you can calculate the average. Of course with some clever math it isn't technically necessary, but its much easier.

Thus solving only part 3 would look like this:

int main()
{
fstream infile("StudentGPA.txt");
float GPAs[1000];//space for 1000 GPAs, that should be enough
int numGPAs=0;
while (!infile.eof())//loop through the whole file
{
Student tmp=loadStudent(infile);
//now tmp stores all the information you wanted
//part 1 and 2 follow directly here
//if you want you could also store students in an array just like the GPAs
//part 3:
GPAs[numGPAs]=tmp.GPA;
numGPAs++;//increment the number of GPAs
}
infile.close();//we don't need it anymore
cout<<"Average GPA: "<<calcAverage(GPAs,numGPAs)<<endl;
}

of course you would have to create the calcAverage function like this:

You will have to think a bit. It may not be easy, but if you really give it some effort you may find that programming can be quite fun. Programming is all about solving problems. While it can be frustrating to have a problem, it feels great to come up with a neat solution to it.

I think part of your issue, however, lies in a lack of understanding of C++ (there were many syntactical errors in your code that most people who know C/C++ would have avoided). In that case you may wish to check out learncpp.com to learn the basics of C++ syntax.