getline(inFile, st[i].name)
while (inFile){
        inFile >> st[i].id
        >> st[i].eng101
        >> st[i].hist201;

        i++;

        getline(inFile, st[i].name);

The above code only reads the first line of my input file, and the rest prints out zeros.

My input file is set up like:

John Wall
345505050
95
66

The output looks like:
Student Name ID Eng101-Grade Hist201
John Wall 345.. 95 66
0 0 0
0 0 0


Any suggestions on why my loop only outputs the first struct correctly from my input file, and the rest are zeros?

put inFile.ignore() after line 8 to remove the '\n' from the input buffer.

alright thanks a lot for the help that worked..

my other only problem is calculating the average for each grade in the file.

like i said the input file is set up such as:

Name
id #
English Grade (number value)
Math Grade (number value)

Im not sure if i have to make 2 seperate functions to find the average for each or do it all in one function. Right now the output is 16 for the class average for what i think is the English grade (first grade in file).

Here is my code for the average function:

int getAvg1 (float avg)
{
    int i = 0;
    float sum = 0;
    int num;
    StudentGrade st[MAX_STDS];

    while (i < MAX_STDS)
    {
       sum += sum + st[i].eng101;

       i++;
    }

     avg = sum / i;

     return (avg);
}

This is the main:

int main()
{

    StudentGrade studList[MAX_STDS];
    int std_num;
    float avgEng;
    float avgHist;

    init_students(studList);
    read_data(studList, std_num);
    print_results(studList, std_num);

    cout << "Class Average: " << getAvg1(avgEng) <<  endl;


    return 0;

}

Just one function is fine, now just call the function again and change avgEng to avgHist .

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.