I'm making a program that processes values from a .txt file and then calculates the standard deviation. However, I am stuck in the beginning of the program because, what I assume is happening, the values from the input file are not being read. What I have so far:
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
void readArrayFromFile(istream& ins, ostream& out, double a[], int& nvalues, int maxsize);
//pre: ins is open and maxsize is the declared size of the array
//post: The values from ins have been read into a until the input
// is exhausted or the array filled. nvalues indicates the
// number of values read
int main()
{
double values[100];
int nvalues;
string filename;
ifstream ins;
ofstream out;
out.open("results.txt");
out << "Assignment 10 for Matt Smith\n\n";
out.setf(ios::fixed);
out.setf(ios::showpoint);
out.precision(2);
//Input the file name and open it
cout << "Please enter the name of the file to be read: ";
cin >> filename;
ins.open(filename.c_str());
if (ins.fail())
{
cout << "Error opening input file.\n";
exit(1);
}
//Input the values
readArrayFromFile(ins, out, values, nvalues, 100);
ins.close();
out.close();
return 0;
}
void readArrayFromFile(istream& ins, ostream& out, double a[], int& nvalues, int maxsize)
{
int i = 0;
while (i < maxsize && ins >> a[i])
{
i++;
}
nvalues = i;
out << nvalues << " values were processed.\n";
}
When you enter "values.txt" for the filename, it always returns "Error opening the input file." and I cannot seem to get it to work. Even if I were to take out the user-inputed filename and simply say "ins.open("values.txt")," it returns that 0 values were processed in the output file.