Help with a C++ code! It's just not working properly!!

karonu's Avatar, Join Date: May 2008
Newbie Member
Hi, i have to write a program that will search a file of numbers of type double and write the largest and the smallest numbers to the screen. The program should also output the average of the numbers in the file to the screen. The file contains nothing but numbers of type int separated by blanks or line breaks.

This is my code! I created a file containing only 7 numbers from 1-7. When I run the program the 7 shows as the highest number but 3 shows as the lowest number??? and the average as -9.3!!!! I can't figure what's wrong with it! can someone please help me?

#include <fstream>
#include <iostream>

#include <cstdlib>
using namespace std;
int main()

ifstream in_stream;

double  min, max=0;
double number=0;
double  sum=0, count=0, average;"D:\numbers.txt");

in_stream >> min;
in_stream >> max;

while (number != 0)

    in_stream >> number;    
	sum += number;
    count ++;

if (count > 0)

average = sum/count;

if (in_stream >> number)

	min = max = number;

	while (in_stream >> number)
		if(number< min)
			min = number;

		if (number> max)
			max = number;


cout << "The largest value in the file is " << max << "\n";
cout << "and the smallest value in the file is " << min << "\n";
cout <<  "Average is  "<< average << endl;
cout << "\n";

return 0;

Last edited by shabbir; 1May2008 at 11:33.. Reason: Code block
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Moved to C-C++ forum