Search code examples
c++maxaverageminifstream

Doesn't calculate min and max grades in program. (c++) (fstream)


The point of this program is to open and read a text file, with student's academic code -> aem and the grade he/she has overall. Then, if the grade of a particular student is greater than 5, it will write his/her academic code on the text named successful, as well as the grade etc. My problem is that it calculates the average of those 5 students grades correctly but it does not calculate the max and min grades. When I run the program, the window coming up, shows the correct average of the course, however max and min grades are always 0. Can anyone help me? Probably I don't compare them in the right way.

#include <iostream>
#include <fstream>

using namespace std;
const int arraySize = 5;

int main(int argc, char** argv)
{
    ifstream d;
    d.open("students.txt");
    ofstream b;
    b.open("succesful.txt");
    ofstream c;
    c.open("unsuccesful.txt");

    int aem;
    double a[arraySize];
    int min, max;
    double grades, average;
    grades = average = 0;
    min = max = 0;

    for (int i = 0; i < arraySize; i++)
    {
        d >> aem >> a[i];
        grades = grades + a[i];
        average = grades / arraySize;

        if (a[i] >= 5) b << aem << " " << a[i] << endl;
        else c << aem << " " << a[i] << endl;
    }

    for (int i = 0; i < arraySize; i++)
    {
        if (a[i] = max)
            max = a[i];
        break;
        if (a[i] = min)
            min = a[i];
        break;
    }


    cout << "The average is:" << average;
    cout << "Maximum is:" << max;
    cout << "Minimum is:" << min;
    d.close(); c.close(); b.close();
    system("pause");
    return 0;
}

Solution

  • int main(int argc, char** argv)
    

    There's no need argc and argv to be here. It can be just int main().

    int min, max;
    double grades, average;
    grades = average = 0;
    min = max = 0;
    

    Assigning value after declaration is unnecessary and inefficient. Also, 0 is an integer, not a floating point. You can just initialize them: int min = 0, max = 0; double grades = .0, average = .0;

    grades = grades + a[i];
    

    Can be shortened to grades += a[i];

    average = grades / arraySize;
    

    This statement is inside a for-loop pointlessly. You can do this after the loop.

    for (int i = 1; i < arraySize; i++) {
    

    You forgot the zeroth element of a. int i = 1; must be replaced to int i = 0;

    if (i >= max)
        max = i; 
    if (i <= min)
        min = i;
    

    You've mistaken a[i] for i. And if a[i] and max already compare equal, there is no need to assign a[i] to max. They can be just:

    if (a[i] > max)
        max = a[i]; 
    if (a[i] < min)
        min = a[i];
    

    And,

    system("pause");
    

    std::system is dependent to the system environment and can have unexpected behavior. It should be replaced to:

    std::cout << "Press enter key." << std::endl;
    std::cin.get();