I've searched the C++ forums and cannot find anything that relates to my question. I am writing a program (for class) that opens a txt file and reads doubles from the file, then computes the standard deviation of the numbers in the file. In order to do this, I am opening the file, reading the numbers in and computing the average, then closing the file, outputting the average to the screen, reopening the file (in order to pull the data from the beginning again) and then computing standard deviation. My code "LOOKS" 100% correct to me, but in running

`if (inStream.fail())`

the second time at lines 37 and 38, I get a failure opening the file after I have closed it at line 32. Can someone help me figure out what my error is? I appreciate it.

```
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
cout << "This program reads a list of numbers from a text file\n"
<< "and then displays the average and the standard deviation\n"
<< "of the numbers in the file. If any errors occur in the\n"
<< "reading of the file, you will be notified.\n\n"
<< "Starting data read now......\n\n"; // General text to describe program
ifstream inStream("numbersIN.txt"); //set input stream name and opens it
if (inStream.fail()) //test for file open failure
{
cout << "Opening input file failed.\n"; // output to screen if file does not open
exit(1);
}
double next; // variable to hold next number
int counter = 0; // counter to store number of numbers read
double total = 0; // variable to hold total of all numbers
while (inStream >> next) // while there is a next number to be read (not EOF)
{
total += next; // add numbers from file and store in total
counter++; // add 1 to counter
}
inStream.close( ); // close input file
double avg = (total/counter); // calculate the average
cout << "The Average is " << avg << endl;
inStream.open("numbersIN.txt"); // reopen file at the beginning
if (inStream.fail()) //test to ensure file opens correctly again
{
cout << "Opening input file second time failed.\n";
exit(1);
}
double stdDev; // variable to hold the standard deviation
double dev; // placeholding variable to store deviation of number being read from file
double devSqrd; // variable to hold squared deviations
double totalDevSqrd = 0; // variable to hold sum of all squared deviations
while (inStream >> next);
{
dev = next - avg; // calculate deviation of number by subtracting the average from the number
devSqrd = dev * dev; // square the result of the deviation
totalDevSqrd += devSqrd; // adds the result to totalDevSqrd
} // exit while loop
inStream.close(); // close the input file
stdDev = sqrt((totalDevSqrd / (avg - 1))); /* standard deviation equals the square root of
the total square deviations devided by the average minus 1 */
cout.setf(ios::fixed | ios::showpoint | ios::precision(6);
cout << "The average of the numbers in the file is " << avg << endl;
cout << "The standard deviation of the numbers in the file is " << stdDev << endl;
return (0);
}
```