I have to wright a program that uses a file to find the average, Max and minimum numbers in the document.

I have been working on this for hours and just can not see why my min number is coming out to "0" There is not a "0" in my "Exam scores.txt" Every thing Else seems to be working just fine. Any tips, pointers or helps would be greatly appreciated! FYI the numbers in the .txt file are 67 64 83 81 72 75 85 81 56 88 71 80 90 58 78 74 84 64 72 69 78 87 84 72 83 68 62 88 70 75

// main.cpp
// Program4_Mac


#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
double number(0);
double average(0);
double sum(0);
int count(0);
int min_score(0);
int max_score(0);


ifstream inputFile;

//open the file.
inputFile.open("Exam scores.txt");

if (inputFile)
{
inputFile >> number;
}

while (inputFile)
{

if (number < min_score)
min_score = number;

if (number > max_score)
max_score = number;




sum = sum + number;
count++;
inputFile >> number;
}

if (count > 0)
{
average = sum / count;
cout << setprecision(4) << "The average of all the test scores are: " << average << endl;
cout << setprecision(2) << "The lowest score is: " << min_score << endl;
cout << setprecision(2) << "The highest score is: " << max_score << endl;

}
else
{
cout << "There were no numbers in the document.";
}

//close the file.
inputFile.close();


return (0);
}// End Code.
Last edited on
Line 34: You initialize min_score to zero. number will never be < min_score (unless there are negate numbers in the file).

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Topic archived. No new replies allowed.