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

How about you write the code, or at least part of it, and then we help you fixs bugs, give you tips, critics and suggestions?

Your first step will most likely be to create a class to represent each student.

void main
{
    sting ID,y;
    float GPA,C=0,sum=0;
    fstream infile;
    infile,open("StudentGPA.txt");
    ofstream onfile;
    onfile.open("StudentGPA".txt");
    While (!infile.ego())
        {
            if (C==0)
                cout <<"ID\t\tyear\tGPA"<<endl;
            infile>>ID>>y>>GPA;
            onfile<<GPA<<endl;
            cout<<ID<<"\t"<<y<<"\t"<<GPA<<endl;
            sum+=GPA;
            C++;
        }
        cout<<"\nGPA Avrg: "<<sum/c<<endl;
}

I solve it like this .. but I need another way ( I mean the basic one )

Please .. I need the answer as soon as possible

There are quite a few issues with your code... I will go through them one by one.

1) Invalid entry point

void main
{

Is not the correct way to create your main function. The correct way is:

int main()
{
    //code here
}

or

int main(int argc, char *argv[])//or char **argv or char argv[][]
{
    //code here
}

2) Use appropriate data structures.

This will make your code a lot easier to use and read and debug. Instead of having GPA,ID,etc just lying out in the open, you should wrap them up in a nice package, like so:

struct Student //a class would be better, but I feel like you probably haven't learnt about classes yet, no?
{
    string id;
    string year;
    float gpa;
};

3) Use Modularization.

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:

//load a student from the file
Student loadStudent(fstream file)
{
    Student ret;
    file>>ret.id>>ret.year>>ret.gpa;
    return ret;
}

4) Use arrays

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:

float calcAverage(float nums[], int size)
{
    float ret=0;
    for (int i=0; i<size; ++i)
        ret+=nums[i];
    return ret/(float)size;
}

5) Put some effort in

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.

Good luck.

commented: loved the sandwich analogy +14
Member Avatar for iamthwee

^^Great advice, additionally attending classes and not leaving it to the last minute is another piece of great advice.

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.