I am learning how to program in C++. My program is supposed to read lines that consist of a string value and double values from an input file and then write the strings and the average of the double values of each line to an output file. The strings are id numbers and the quantity of the double values can vary from line to line.
example:
input file:
9875 1, 2, 3, 4
9324 5, 5, 7, 8, 9
7458 9, 10, 11
output file:
9875 2.5
9324 6.8
7458 10
I used a vector to hold the value of the double values and calculated the sum of the values. My issue is that when my program is reading through the input file it does know when a line ends and a new line begins and outputs the average all of the double values and the string values(which are id numbers). How would I go about telling the program when a line ends? Here is my code thus far:
#include <fstream>;
#include <iostream>;
#include <string>;
#include <vector>;
using namespace std;
int main()
{
ifstream inFile;
ofstream outFile;
string id;
double num;
double sum = 0;
string line;
vector<double> avgs;
inFile.open("in.txt");
outFile.open("out.txt");
if (inFile)
{
inFile >> id >> num;
//read the values from the input file into the vector
while(inFile >> num)
{
avgs.push_back(num);
}
for (int n : avgs)
{
sum += n;
}
outFile << " " << id << " " << avgs.size() << " " << (sum / avgs.size()) << endl;
}
else
{
cout << "Error" << endl;
}
inFile.close();
outFile.close();
return 0;
}