Function is not executing

Hello,

Last edited on
Since this seems to be your first question on this forum. Edit your code, alight all your codes and click on the code tag i.e. <>
The statement: inFile.open("E:\\Mileagelog.txt"); should not be inside the loop. Assuming it succeeds the first time, on the second pass, it will fail, and set the fail flag for the stream, that is inFile.fail() == true
However, the eof flag will not be set. In fact, testing for eof() is almost always erroneous and is best avoided.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
double averageMileage()
{
    int numberOfLines = 0;
    double currentValue;
    double runningTotal = 0;

    ifstream inFile;
    inFile.open("E:\\Mileagelog.txt");

    while (inFile >> currentValue)  // read from the file
    {
        numberOfLines++;
        runningTotal += currentValue;
    }

    inFile.close();

    // calculate & return the average
    return runningTotal / numberOfLines;
}


One more thing, many of the comments were superfluous and make the code harder to read. The use of suitable names for variables, proper indentation and so on should make most things clear without the need for comments.
Last edited on
Thank you so much! My professor advised I remove my code to prevent other students from copying. I appreciate your help. I'm sure I'll be back. Lol. :)
Topic archived. No new replies allowed.