Hi
is there anyone have an idea :idea: on how could i get the column 7 from the file, I did it for column 5. or how could I continue to make it take other columns.

``````if ( input_file.fail ())
{
cout << "Error opening file.";
}
else
{
for(int i = 0; i < 7; i++)    // Scan the first 7 lines, which is not required
getline(input_file, line, '\n');
count = 8;                  // We are in the eighth line
while(count <= 1447)
{      // We have to add until 1447 lines..

for(int i = 0; i < 5; i++)
getline(input_file, line, ',');    // Get the first 5 columns.
cout<<line<<endl;       // Print the 5th column for verification.
value = atof(line.c_str());// Convert the 5th column into float
sum += value;	//add value to sum
getline(input_file, line, '\n');    // Go to the next line..
count++;        // Increment the count, to go to the next line
}
cout << count;
cout<<endl<<"The sum of all values of column 5 is : "<<sum<<endl;

float avg = sum / count ;
cout<<endl<<"The avg of all values of column 5 is : "<<avg<<endl;
}``````

thank you

## All 18 Replies

make the column number a variable so that you can change it as desired.

``````int col = 7;
for(int i = 0; i < col; i++)``````

Hi Anceint dragon
I tried to do like that, but it giving me wrong result

``````if ( input_file.fail ())
{
cout << "Error opening file.";
}
else
{
for(int i = 0; i < 7; i++)    // Scan the first 7 lines, which is not required
getline(input_file, line, '\n');
count = 8;                  // We are in the eighth line
while(count <= 1447)
{      // We have to add until 1447 lines..
++col;
if (col =5)
{ for(int i = 0; i < col; i++)

getline(input_file, line, ',');    // Get the first 5 columns.
cout<<line<<endl;       // Print the 5th column for verification.
value = atof(line.c_str());// Convert the 5th column into float
sum += value;	//add value to sum
getline(input_file, line, '\n');    // Go to the next line..*/
}
count++;        // Increment the count, to go to the next line

if ( col =7)
{for(int i = 0; i < col; i++)

getline(input_file, line, ',');    // Get the first 7 columns.
cout<<line<<endl;       // Print the 7th column for verification.
value1 = atof(line.c_str());// Convert the 7th column into float
sum1 += value1;	//add value to sum
getline(input_file, line, '\n');    // Go to the next line..*/
}
count++;        // Increment the count, to go to the next line
}
cout << count;
cout<<endl<<"The sum of all values of column 5 is : "<<sum<<endl;
cout<<endl<<"The sum of all values of column 7 is : "<<sum1<<endl;
float avg = sum / count ;
cout<<endl<<"The avg of all values of column 5 is : "<<avg<<endl;
float avg1 = sum1 / count ;
cout<<endl<<"The avg of all values of column 7 is : "<<avg1<<endl;``````

even if I used swtich statement.
I am not sure can you check it please.

``if (col =5)``

you want the boolean == operator, not assignment = operator.

Why do you need those two if statements? With the variable you can combine them and remove if(col == ???)

Hi
I used if to get the sum of the 5 column and another if to get the sum for the 7 column, and I have another columns need to have the same operation .( that s why I tried as well using swtich statement).
I am not sure about the combine statement how it is going to look like in with many column need the to find sum and avg for them

simply use the col variable number. You can also put that code in another function that takes the column number as a parameter.

``````int main()
{
int main()
{
ifstream input_file("filename");
if ( !input_file.is_open())
{
cout << "Error opening file.";
}
else
{
for(int i = 0; i < 7; i++)    // Scan the first 7 lines, which is not required
getline(input_file, line, '\n');
count = 8;                  // We are in the eighth line
int col = 5;
while(count <= 1447)
{      // We have to add until 1447 lines..
for(int i = 0; i < col; i++)
getline(input_file, line, ',');    // Get the first 5 columns.
cout<<line<<endl;       // Print the 5th column for verification.
value = atof(line.c_str());// Convert the 5th column into float
sum += value;	//add value to sum
getline(input_file, line, '\n');    // Go to the next line..
count++;        // Increment the count, to go to the next line
}
cout << count;
cout<<endl<<"The sum of all values of column 5 is : "<<sum<<endl;

float avg = sum / count ;
cout<<endl<<"The avg of all values of column 5 is : "<<avg<<endl;
}

}``````

Hi
thanks I will do it in a function.
one think, in the previous post, it has the code

``````int main()
{
int main()
{
....
}
}``````

is a paste error, or ?

yes, that was a copy/paste error.

Hi
I did this function, it return only one column value ( I need the rest as well)

``````int pass_column(int column);
int main()
{

ifstream input_file("Log_05.csv");
string line;
int count,col=0;
float sum = 0.0, value;
float max = 0.0;
if ( input_file.fail ())
{
cout << "Error opening file.";
}
else
{
for(int i = 0; i < 7; i++)    // Scan the first 7 lines, which is not required
getline(input_file, line, '\n');
count = 8;                  // We are in the eighth line
while(count <= 1447) // We have to add until 1447 lines..
{

for(int i = 0; i < pass_column(col); i++)

getline(input_file, line, ',');    // Get the first col columns.
cout<<line<<endl;       // Print the col column for verification.
value = atof(line.c_str());// Convert the col column into float
sum += value;	//add value to sum
getline(input_file, line, '\n');    // Go to the next line..
if ( max < value)
{
max= value;
}
col++;
count++;        // Increment the count, to go to the next line

}

cout << count;
cout<<endl<<"The max of all values of column 5 is : "<<max<<endl;
cout<<endl<<"The sum of all values of column 5 is : "<<sum<<endl;
float avg = sum / count ;
cout<<endl<<"The avg of all values of column 5 is : "<<avg<<endl;

}

return 0;
}

int pass_column(int column)
{
int i=0;
while ( i<196)
{
switch (i)
{
case 15:
column=i;
return (column);

break;
case 193:
column=i;
return (column);
break;
case 195:
column=i;
return (column);
break;
}
i++;
}

}``````

I know function return only one value , what could I do ????????????????????

Hi ancient dragon

``````float sumcol(int col,string line)
{
int pos;
float sums = {0.0};
// find the (col-1)th comma in the string
pos = 0;
for(int k = 0;k < line.size() && pos < (col-1); k++)
if(line[k] == ',')
pos++;
// remove the first (col-1) columns from the string
line = line.substr(pos+1);

return atof(line.c_str());
}

int main()
{

ifstream input_file("Logos_05.csv");
string line;
int count;
float sums = {0.0};
float value;
float max = 0.0;
if ( input_file.fail ())
{
cout << "Error opening file.";
}
else
{
for(int i = 0; i < 7; i++)    // Scan the first 7 lines, which is not required
getline(input_file, line, '\n');
count = 8;                  // We are in the eighth line
while(count <= 1447)
{      // We have to add until 1447 lines..
getline(input_file,line);
for(int col = 0; col < 196; col++)
sums[col] += sumcol(col,line);
count++;        // Increment the count, to go to the next line

}

cout << count;
for(i = 0; i < 196; i++)
{
cout<<endl<<"The sum of all values of column " << i+1 << " is : "<<sums[i]<<endl;
float avg = sums[i] / count ;
cout<<endl<<"The avg of all values of column " << i+1 << " is : "<<avg<<endl;
}

}

return 0;
}``````

I did this function that get me the sum of all the column, but the problem I do not need all the column I need sum,avg for only 5,6,15,16,17,22,23,28,29,30,37,38,39,44,45,46,50,51,52,61,71,81,91,101,111,121,131,141,151,161,171,181,191 and some of these column are int and some float. so can you check it please.

create an array of ints that contain the column numbers you want, then use that in main().

``````int array[] = {5,6,15,16,17,22,23,28,29,30,37,38,39,44,45,46,50,51,52,61,71,81,91,101,111,121,131,141,151,161,171,181,191 };

// number of elements in the above array
int arraysize = sizeof(array) / sizeof(array);

for(int col = 0; col < arraysize; col++)
sums[col] += sumcol(array[col],line);``````

all columns can be sumed as floats whether the data contains integers of floats. atof() will work correctly with both strings.

Hi Ancient dragon
I did that

``````float sumcol(int col,string line)
{
int pos;
float sums = {0.0}; // we need 33 column
// find the (col-1)th comma in the string
pos = 0;
for(int k = 0;k < line.size() && pos < (col-1); k++)
if(line[k] == ',')
pos++;
// remove the first (col-1) columns from the string
line = line.substr(pos+1);

return atof(line.c_str());
}

int main()
{

ifstream input_file("Log_05.csv");
string line;
int count;
float sums = {0.0};
float max = 0.0;
int arraysize;
int array[] = {5,6,15,16,17,22,23,28,29,30,37,38,39,44,45,46,50,51,52,61,71,81,91,101,111,121,131,141,151,161,171,181,191 };

if ( input_file.fail ())
{
cout << "Error opening file.";
}
else
{
for(int i = 0; i < 7; i++)    // Scan the first 7 lines, which is not required
getline(input_file, line, '\n');
count = 8;                  // We are in the eighth line
while(count <= 1447) // We have to add until 1447 lines..
{
getline(input_file,line);
// number of elements in the above array
arraysize = sizeof(array) / sizeof(array);
for(int col = 0; col < arraysize; col++)
{
sums[col] += sumcol(array[col],line);
}
count++;        // Increment the count, to go to the next line

}
for(i = 0; i < arraysize; i++)
{
cout<<endl<<"The sum of all values of column " << i+1 << " is : "<<sums[i]<<endl;
float avg = sums[i] / count ;
cout<<endl<<"The avg of all values of column " << i+1 << " is : "<<avg<<endl;
}

}

return 0;
}``````

but it gives unordinary output

``````The sum of all values of column 1 is : 0

The avg of all values of column 1 is : 0

The sum of all values of column 2 is : 2.88576e

The avg of all values of column 2 is : 1992.93

The sum of all values of column 3 is : 6480

The avg of all values of column 3 is : 4.47514

The sum of all values of column 4 is : 0

The avg of all values of column 4 is : 0

The sum of all values of column 5 is : 64800

The avg of all values of column 5 is : 44.7514

The sum of all values of column 6 is : 0

The avg of all values of column 6 is : 0

The sum of all values of column 7 is : 0

The avg of all values of column 7 is : 0

The sum of all values of column 8 is : 1353.58

The avg of all values of column 8 is : 0.934794

The sum of all values of column 9 is : 135360

The avg of all values of column 9 is : 93.4807

The sum of all values of column 10 is : 5760

The avg of all values of column 10 is : 3.9779

The sum of all values of column 11 is : 6849

The avg of all values of column 11 is : 4.72997

The sum of all values of column 12 is : 3241.1

The avg of all values of column 12 is : 2.23833

The sum of all values of column 13 is : 288733

The avg of all values of column 13 is : 199.401

The sum of all values of column 14 is : 6079.57

The avg of all values of column 14 is : 4.1986

The sum of all values of column 15 is : 424.099

The avg of all values of column 15 is : 0.29288

The sum of all values of column 16 is : 30613.4

The avg of all values of column 16 is : 21.1418

The sum of all values of column 17 is : 7860

The avg of all values of column 17 is : 5.42818

The sum of all values of column 18 is : 454

The avg of all values of column 18 is : 0.31353

The sum of all values of column 19 is : 40637

The avg of all values of column 19 is : 28.0642

The sum of all values of column 20 is : 21771

The avg of all values of column 20 is : 15.0352

The sum of all values of column 21 is : 4154

The avg of all values of column 21 is : 2.86878

The sum of all values of column 22 is : 90420

The avg of all values of column 22 is : 62.4448

The sum of all values of column 23 is : 81680

The avg of all values of column 23 is : 56.4088

The sum of all values of column 24 is : 6109

The avg of all values of column 24 is : 4.21892

The sum of all values of column 25 is : 1226

The avg of all values of column 25 is : 0.84668

The sum of all values of column 26 is : 666

The avg of all values of column 26 is : 0.45994

The sum of all values of column 27 is : 266235

The avg of all values of column 27 is : 183.864

The sum of all values of column 28 is : 23976

The avg of all values of column 28 is : 16.558

The sum of all values of column 29 is : 15954

The avg of all values of column 29 is : 11.018

The sum of all values of column 30 is : 3683

The avg of all values of column 30 is : 2.54351

The sum of all values of column 31 is : 34887

The avg of all values of column 31 is : 24.0932

The sum of all values of column 32 is : 2397

The avg of all values of column 32 is : 1.65539

The sum of all values of column 33 is : 22584.9

The avg of all values of column 33 is : 15.5973
Press any key to continue``````

I am not sure why

because the cout line is printing the value of the loop counter -- i -- not the value of the array, which stores the real column number.

Hi
I change the cout and got the column number, but still all the calculation is giving wrong result.

``````float sumcol(int col,string line)
{
int pos;
float sums = {0.0};
// find the (col-1)th comma in the string
pos = 0;
for(int k = 0;k < line.size() && pos < (col-1); k++)
if(line[k] == ',')
pos++;
// remove the first (col-1) columns from the string
line = line.substr(pos+1);

return atof(line.c_str());
}

int main()
{

ifstream input_file("Log_05.csv");
string line;
int count;
float sums = {0.0}, avg={0.0};
float max = 0.0;
int arraysize;
int array[] = {6,15,16,17,22,23,28,24,29,30,37,38,39,44,45,46,50,51,52,61,71,81,91,101,111,121,131,141,151,161,171,181,191 };

if ( input_file.fail ())
{
cout << "Error opening file.";
}
else
{
for(int i = 0; i < 7; i++)    // Scan the first 7 lines, which is not required
getline(input_file, line, '\n');
count = 8;                  // We are in the eighth line
while(count <= 1447) // We have to add until 1447 lines..
{
getline(input_file,line);
// number of elements in the above array
arraysize = sizeof(array) / sizeof(array);
for(int col = 0; col < arraysize; col++)
{
sums[col] += sumcol(array[col],line);
}
count++;        // Increment the count, to go to the next line

}
for(i = 0; i < arraysize; i++)
{
cout<<endl<<"The sum of all values of column " << array[i] << " is : "<<sums[i]<<endl;
avg[i] = sums[i] / count ;
cout<<endl<<"The avg of all values of column " << array[i] << " is : "<<avg[i]<<endl;
}

}

return 0;
}``````

the result is not right. :cry:

``````The sum of all values of column 6 is : 2.88576e+006

The avg of all values of column 6 is : 1992.93

The sum of all values of column 15 is : 6480

The avg of all values of column 15 is : 4.47514

The sum of all values of column 16 is : 0

The avg of all values of column 16 is : 0

The sum of all values of column 17 is : 64800

The avg of all values of column 17 is : 44.7514

The sum of all values of column 22 is : 0

The avg of all values of column 22 is : 0

The sum of all values of column 23 is : 0

The avg of all values of column 23 is : 0

The sum of all values of column 28 is : 1353.58

The avg of all values of column 28 is : 0.934794

The sum of all values of column 24 is : 0

The avg of all values of column 24 is : 0

The sum of all values of column 29 is : 135360

The avg of all values of column 29 is : 93.4807

The sum of all values of column 30 is : 5760

The avg of all values of column 30 is : 3.9779

The sum of all values of column 37 is : 6849

The avg of all values of column 37 is : 4.72997

The sum of all values of column 38 is : 3241.1

The avg of all values of column 38 is : 2.23833

The sum of all values of column 39 is : 288733

The avg of all values of column 39 is : 199.401

The sum of all values of column 44 is : 6079.57

The avg of all values of column 44 is : 4.1986

The sum of all values of column 45 is : 424.099

The avg of all values of column 45 is : 0.292886

The sum of all values of column 46 is : 30613.4

The avg of all values of column 46 is : 21.1418

The sum of all values of column 50 is : 7860

The avg of all values of column 50 is : 5.42818

The sum of all values of column 51 is : 454

The avg of all values of column 51 is : 0.313536

The sum of all values of column 52 is : 40637

The avg of all values of column 52 is : 28.0642

The sum of all values of column 61 is : 21771

The avg of all values of column 61 is : 15.0352

The sum of all values of column 71 is : 4154

The avg of all values of column 71 is : 2.86878

The sum of all values of column 81 is : 90420

The avg of all values of column 81 is : 62.4448

The sum of all values of column 91 is : 81680

The avg of all values of column 91 is : 56.4088

The sum of all values of column 101 is : 6109

The avg of all values of column 101 is : 4.21892

The sum of all values of column 111 is : 1226

The avg of all values of column 111 is : 0.846685

The sum of all values of column 121 is : 666

The avg of all values of column 121 is : 0.459945

The sum of all values of column 131 is : 266235

The avg of all values of column 131 is : 183.864

The sum of all values of column 141 is : 23976

The avg of all values of column 141 is : 16.558

The sum of all values of column 151 is : 15954

The avg of all values of column 151 is : 11.018

The sum of all values of column 161 is : 3683

The avg of all values of column 161 is : 2.54351

The sum of all values of column 171 is : 34887

The avg of all values of column 171 is : 24.0932

The sum of all values of column 181 is : 2397

The avg of all values of column 181 is : 1.65539

The sum of all values of column 191 is : 22584.9

The avg of all values of column 191 is : 15.5973
Press any key to continue``````

I will attach my csv file.
the calculation suppose to be right.

the calculation suppose to be right.

Then I would suggest YOU find out what is wrong. Use (or learn to use) you compiler's debugger.

Hi
I worked on it with another file called book.csv, all the calculation was right.
the problem is with the original file this require to be read Log_05.csv as it contain dat,time different format.

``````float sumcol(int col,string line)
{
int pos;
float sums = {0.0};
// find the (col-1)th comma in the string
pos = 0;
for(int k = 0;k < line.size() && pos < (col-1); k++)
if(line[k] == ',')
pos++;
// remove the first (col-1) columns from the string
line = line.substr(pos+1);

return atof(line.c_str());
}

int main()
{

ifstream input_file("Book1.csv");
string line;
int count;
float sums = {0.0}, avg[]= {0.0};
float max = 0.0;
int arraysize;
int array[] = {2,4,6,8,10};//6,15,16,17,22,23,24,28,29,30,37,38,39,44,45,46,50,51,52,61,71,81,91,101,111,121,131,141,151,161,171,181,191 };

if ( input_file.fail ())
{
cout << "Error opening file.";
}
else
{
for(int i = 0; i < 7; i++)    // Scan the first 7 lines, which is not required
getline(input_file, line, '\n');
count = 8;                  // We are in the eighth line
while(count <= 15) // We have to add until 1447 lines..
{
getline(input_file,line);
// number of elements in the above array
arraysize = sizeof(array) / sizeof(array);
for(int col = 0; col < arraysize; col++)
{
sums[col] += sumcol(array[col],line);
}
count++;        // Increment the count, to go to the next line

}
count-=8 ;
cout << count;
for(i = 0; i < arraysize; i++)
{
cout<<endl<<"The sum of all values of column " << array[i] << " is : "<<sums[i]<<endl;
avg[i] = sums[i] / count ;
cout<<endl<<"The avg of all values of column " << array[i] << " is : "<<avg[i]<<endl;
}

}

return 0;
}``````

it work perfectly with the book.csv, I will attach the file.
but when apply it for my file Log_05.csv it makes error.
I am using my debugger to check all my result, not only run the code. for my Log file when it suppose to take column 6 first thing it take 2004 as it is consider 6th character. so it calculate the year for the first column rather than going to the 6th column where the first value 345.28, I do not know how to fix that error.

05/10/2004,00:00:45,190,0,13.94,346.81,206.67,18.46,32,3,47,6, ...

The above is taken from log_05.csv. How is that different from book.csv? The file you posted looks like just trash data, not real data. When your program first opens the file, look at the first line of data. If the first comma-separated field contains a '/' then its a date and the first column number that contains data is #2 (0 is first). If the '/' is not in the first field, then #0 is the first column you want, and subtract 2 from each of the column numbers in array "array".

Hi
I really sorry for all this mess, but could you please check what my mistake ( I really confuse). I am going to post the whole code and the result.

``````float sumcol(int col,string line)
{
int pos;
float sums = {0.0};
// find the (col-1)th comma in the string
pos = 0;
for(int k = 0;k < line.size() && pos < (col-1); k++)
if(line[k] == ',')
pos++;
// remove the first (col-1) columns from the string
line = line.substr(pos+1);

return atof(line.c_str());
}

int main()
{

ifstream input_file("Log_05.csv");
string line;
int count;
float sums = {0.0}, avg[]= {0.0};
float max = 0.0;
int arraysize;
int array[] = {6,15,16,17,22,23,24,28,29,30,37,38,39,44,45,46,50,51,52,61,71,81,91,101,111,121,131,141,151,161,171,181,191 };

if ( input_file.fail ())
{
cout << "Error opening file.";
}
else
{
for(int i = 0; i < 7; i++)    // Scan the first 7 lines, which is not required
getline(input_file, line);
count = 8;                  // We are in the eighth line
while(count <= 1447) // We have to add until 1447 lines..
{
getline(input_file,line);
// number of elements in the above array
arraysize = sizeof(array) / sizeof(array);
for(int col = 0; col < arraysize; col++)
{
sums[col] += sumcol(array[col],line);
}
count++;        // Increment the count, to go to the next line

}
count-=8 ;
cout << count;
for(i = 0; i < arraysize; i++)
{
cout<<endl<<"The sum of all values of column " << array[i] << " is : "<<sums[i]<<endl;
avg[i] = sums[i] / count ;
cout<<endl<<"The avg of all values of column " << array[i] << " is : "<<avg[i]<<endl;
}

}

return 0;
}``````

the result

``````The sum of all values of column 6 is : 2.88576e+006

The avg of all values of column 6 is : 2004

The sum of all values of column 15 is : 6480

The avg of all values of column 15 is : 4.5

The sum of all values of column 16 is : 0

The avg of all values of column 16 is : 0

The sum of all values of column 17 is : 64800

The avg of all values of column 17 is : 45

The sum of all values of column 22 is : 0

The avg of all values of column 22 is : 0

The sum of all values of column 23 is : 0

The avg of all values of column 23 is : 0

The sum of all values of column 24 is : 0

The avg of all values of column 24 is : 0

The sum of all values of column 28 is : 1353.58

The avg of all values of column 28 is : 0.939987

The sum of all values of column 29 is : 135360

The avg of all values of column 29 is : 94

The sum of all values of column 30 is : 5760

The avg of all values of column 30 is : 4

The sum of all values of column 37 is : 6849

The avg of all values of column 37 is : 4.75625

The sum of all values of column 38 is : 3241.1

The avg of all values of column 38 is : 2.25076

The sum of all values of column 39 is : 288733

The avg of all values of column 39 is : 200.509

The sum of all values of column 44 is : 6079.57

The avg of all values of column 44 is : 4.22192

The sum of all values of column 45 is : 424.099

The avg of all values of column 45 is : 0.294513

The sum of all values of column 46 is : 30613.4

The avg of all values of column 46 is : 21.2593

The sum of all values of column 50 is : 7860

The avg of all values of column 50 is : 5.45833

The sum of all values of column 51 is : 454

The avg of all values of column 51 is : 0.315278

The sum of all values of column 52 is : 40637

The avg of all values of column 52 is : 28.2201

The sum of all values of column 61 is : 21771

The avg of all values of column 61 is : 15.1187

The sum of all values of column 71 is : 4154

The avg of all values of column 71 is : 2.88472

The sum of all values of column 81 is : 90420

The avg of all values of column 81 is : 62.7917

The sum of all values of column 91 is : 81680

The avg of all values of column 91 is : 56.7222

The sum of all values of column 101 is : 6109

The avg of all values of column 101 is : 4.24236

The sum of all values of column 111 is : 1226

The avg of all values of column 111 is : 0.851389

The sum of all values of column 121 is : 666

The avg of all values of column 121 is : 0.4625

The sum of all values of column 131 is : 266235

The avg of all values of column 131 is : 184.885

The sum of all values of column 141 is : 23976

The avg of all values of column 141 is : 16.65

The sum of all values of column 151 is : 15954

The avg of all values of column 151 is : 11.0792

The sum of all values of column 161 is : 3683

The avg of all values of column 161 is : 2.55764

The sum of all values of column 171 is : 34887

The avg of all values of column 171 is : 24.2271

The sum of all values of column 181 is : 2397

The avg of all values of column 181 is : 1.66458

The sum of all values of column 191 is : 22584.9

The avg of all values of column 191 is : 15.684
Press any key to continue``````

the Log file is already attached previously.
again I really sorry for that, but I do not know what is my mistake.
first column 6 start with this value345.28 this is my first column needed, the sum for all that 6column is 509695.59, avg =353.955

thank you I made a new function and now I am getting the right result.
thak you for you support.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.