I am trying to write a program that will read integers from file and display them along with a running average. I have written code to do this and it works when the file I am reading from is in the format of:
5 6 7 8 9 2 3 4 1 2 4 5 7 8 9 2 5 6 2 1 1 2 3 4 5
I am trying to make the same program read (and display the int and average as before) from a file containing:
3 4 5 3 5 7 8 9
but it doesn t display as I want it any help would be appreciated. here is my code:
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
int main()
{
double y = 0, x = 0, value1 = 0;
string myFileName, myString;
cout << "please enter the name of the file you wish to open" << "
";
cin >> myFileName;
ifstream inFile;
inFile.open(myFileName.c_str());
while (!inFile.eof())
{
double currentAv;
while (getline(inFile, myString, ( )))
{
y = y + 1;
value1 = atof(myString.c_str());
currentAv = (value1 + x) / y;
cout << myString << "," << currentAv << endl;
x = value1 + x;
}
}
inFile.close();
system("pause");
}
the output I get when I enter the first file to be opened is:
5,5
6,6.5
7,6
.......
I would like the second file when entered to display the same ie the integer and its running average but I get:
3 4 5 6 7 ,3