Hi all,

I am trying to read double numbers from a txt file and calculate average and standard deviation. It builds successfully but gives an error when it is run. The error I see on the screen is -1.#IND. Do you have any idea what it is and how can I correct it ?
I appreciate for helps,

My code is :

double avg_file(ifstream& source_file)
{
	double number_in_file;
	double total = 0;
	int count = 0;
	source_file >> number_in_file;
	while (! source_file.eof())
	{
		total = number_in_file + total;
		source_file >> number_in_file;
		count ++;
	}
	//average of numbers in file
	return (total/count);
}

double std_dev (ifstream& in_file, double average)
{
	double number_in_file;
	double std_dev_f;
	double sum_diff = 0;
	int count = 0;
	in_file >> number_in_file;
	while (! in_file.eof())
	{
		sum_diff = pow((average-number_in_file),2)+ sum_diff;
		count++;
	}
	std_dev_f =sqrt(sum_diff/count); 
	return (std_dev_f);
}
int main()
{
	ifstream fin;
	//ofstream fout;
	fin.open("numbers.txt");
	if (fin.fail())
	{
		cout << "erorr on opening the file \n";
		exit (1);
	}
	cout << "the calculated average is  " << avg_file(fin) << endl;	
	cout << "the std devitation is     "  << std_dev(fin,avg_file(fin)) << endl;
}

Recommended Answers

All 2 Replies

Hi all,

I am trying to read double numbers from a txt file and calculate average and standard deviation. It builds successfully but gives an error when it is run. The error I see on the screen is -1.#IND. Do you have any idea what it is and how can I correct it ?
I appreciate for helps,

My code is :

double avg_file(ifstream& source_file)
{
	double number_in_file;
	double total = 0;
	int count = 0;
	source_file >> number_in_file;
	while (! source_file.eof())
	{
		total = number_in_file + total;
		source_file >> number_in_file;
		count ++;
	}
	//average of numbers in file
	return (total/count);
}

double std_dev (ifstream& in_file, double average)
{
	double number_in_file;
	double std_dev_f;
	double sum_diff = 0;
	int count = 0;
	in_file >> number_in_file;
	while (! in_file.eof())
	{
		sum_diff = pow((average-number_in_file),2)+ sum_diff;
		count++;
	}
	std_dev_f =sqrt(sum_diff/count); 
	return (std_dev_f);
}
int main()
{
	ifstream fin;
	//ofstream fout;
	fin.open("numbers.txt");
	if (fin.fail())
	{
		cout << "erorr on opening the file \n";
		exit (1);
	}
	cout << "the calculated average is  " << avg_file(fin) << endl;	
	cout << "the std devitation is     "  << std_dev(fin,avg_file(fin)) << endl;
}

I think -1#IND is "Not a number" or infinity or something like this. Maybe you're using an uninitialized variable or overflowing or dividing by 0.

My guess is that it has to do with the way you are reading the file in. Check to make sure you have good data. You are calling two separate functions, each of which reads from the file till EOF. By the time the first function is done, the entire file's been read, then you pass that stream to the second function without resetting the stream in any way, so that second function is probably not going to read anything in. Stick some debugging statements in the second function inside that while loop and see if it ever gets in there.

Its because you open your file multiple times without reseting
its position.

Here is your program. I don't know if the output is correct, but it runs:

#include<iostream>
#include<fstream>
#include<cmath>

using namespace std;

double avg_file(ifstream& source_file)
{
	
	double number_in_file;
	double total = 0;
	int count = 0;
	source_file >> number_in_file;
	while (! source_file.eof())
	{
		source_file >> number_in_file;//SWITCHED
		total = number_in_file + total;
		count ++;
	}

	source_file.seekg(0,ios::beg); //NEW -- reset cursor position in file

	//average of numbers in file
	return (total/count);
}

double std_dev (ifstream& in_file, double average)
{
	double number_in_file = 0;
	double std_dev_f;
	double sum_diff = 0;
	int count = 0;

	while (! in_file.eof())
	{
		in_file >> number_in_file; //ADDED
		sum_diff = pow((average-number_in_file),2)+ sum_diff;
		count++;
	}
	std_dev_f =sqrt(sum_diff/count); 
	return (std_dev_f);
}
int main()
{
	ifstream fin;
	//ofstream fout;
	fin.open("numbers.txt");
	if (fin.fail())
	{
		cout << "erorr on opening the file \n";
		exit (1);
	}
	cout << "the calculated average is  " << avg_file(fin) << endl;	

	cout << "the std devitation is     "  << std_dev(fin,avg_file(fin)) << endl;
}
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.