| | |
other col
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2005
Posts: 29
Reputation:
Solved Threads: 1
Hi
is there anyone have an 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.
thank you
is there anyone have an 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. C++ Syntax (Toggle Plain Text)
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
Last edited by Dave Sinkula; Jan 9th, 2006 at 10:54 am. Reason: Fixed [code][/code] tags.
make the column number a variable so that you can change it as desired.
C++ Syntax (Toggle Plain Text)
int col = 7; for(int i = 0; i < col; i++)
•
•
Join Date: Dec 2005
Posts: 29
Reputation:
Solved Threads: 1
Hi Anceint dragon
you are right about it.
I tried to do like that, but it giving me wrong result
even if I used swtich statement.
I am not sure can you check it please.
you are right about it.
I tried to do like that, but it giving me wrong result
C++ Syntax (Toggle Plain Text)
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;
I am not sure can you check it please.
C++ Syntax (Toggle Plain Text)
if (col =5)
Why do you need those two if statements? With the variable you can combine them and remove if(col == ???)
•
•
Join Date: Dec 2005
Posts: 29
Reputation:
Solved Threads: 1
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
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.
C++ Syntax (Toggle Plain Text)
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; } }
•
•
Join Date: Dec 2005
Posts: 29
Reputation:
Solved Threads: 1
Hi
thanks I will do it in a function.
one think, in the previous post, it has the code
is a paste error, or ?
thanks I will do it in a function.
one think, in the previous post, it has the code
C++ Syntax (Toggle Plain Text)
int main() { int main() { .... } }
•
•
Join Date: Dec 2005
Posts: 29
Reputation:
Solved Threads: 1
Hi
I did this function, it return only one column value ( I need the rest as well)
I know function return only one value , what could I do ????????????????????
I did this function, it return only one column value ( I need the rest as well)
C++ Syntax (Toggle Plain Text)
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++; } }
•
•
Join Date: Dec 2005
Posts: 29
Reputation:
Solved Threads: 1
Hi ancient dragon
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.
C++ Syntax (Toggle Plain Text)
float sumcol(int col,string line) { int pos; float sums[196] = {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[196] = {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; }
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Problem compiling with Dev-C++
- Next Thread: Rounding floating points
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






