Modify csv/txt file to include only numbers?

I created the following program to calculate the average value of a list of numbers from a csv/txt file. The numbers below can be as numerous as necessary.

-533
-911
-121
-334
-546
791
-779
-28
509
795
465
220
612
-415
-141
-463
-243
67
-957
963
-35
-237
-315
-232
-430
210
557
506
-661
857
-917
585
978
-226
456
-803
-395
507
351
-589

#include <iostream>
#include <cmath>
#include <math.h>
#include <fstream>
#include <string>
#include <numeric>
using namespace std;

int main()
{
ifstream infile;
float num;
float total;
float x;
float aver;

x=0; total=0;
infile.open("gnumbers.csv");

while(!infile.eof())
{
infile>>num;
total=total+num;
x++;
}

aver=(total-num)/(x-1);

cout << "The last number in this range is: " <<num<< "\n";
cout<< "The sum of this range is: " <<(total-num)<<'\n';
cout<< "The number of items in this range is: "<<x-1<<'\n';
cout<< "The average of this range is: "<<aver<<'\n';
cout<< ""<<'\n';

infile.close();

getchar();
return 0;
}



However, I am having a problem in terms of specifying to the program what to calculate if there is irrelevant data in the csv file; e.g. suppose there is a heading above the list of numbers:

Title
-533
-911
-121
-334
-546
791
-779
-28
509
795
465
220
612
-415
-141
-463
-243
67
-957
963
-35
-237
-315
-232
-430
210
557
506
-661
857
-917
585
978
-226
456
-803
-395
507
351
-589

Or, let us suppose that there is more than one array of numbers and I only want to calculate the average for the second row down.

How do I specify what specifically I would like to calculate within the file? Please include a modified version of the above code in your answer. Thank you.
Last edited on
Topic archived. No new replies allowed.