other col

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2005
Posts: 29
Reputation: masa is an unknown quantity at this point 
Solved Threads: 1
masa masa is offline Offline
Light Poster

other col

 
0
  #1
Jan 9th, 2006
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.

  1. if ( input_file.fail ())
  2. {
  3. cout << "Error opening file.";
  4. }
  5. else
  6. {
  7. for(int i = 0; i < 7; i++) // Scan the first 7 lines, which is not required
  8. getline(input_file, line, '\n');
  9. count = 8; // We are in the eighth line
  10. while(count <= 1447)
  11. { // We have to add until 1447 lines..
  12.  
  13. for(int i = 0; i < 5; i++)
  14. getline(input_file, line, ','); // Get the first 5 columns.
  15. cout<<line<<endl; // Print the 5th column for verification.
  16. value = atof(line.c_str());// Convert the 5th column into float
  17. sum += value; //add value to sum
  18. getline(input_file, line, '\n'); // Go to the next line..
  19. count++; // Increment the count, to go to the next line
  20. }
  21. cout << count;
  22. cout<<endl<<"The sum of all values of column 5 is : "<<sum<<endl;
  23.  
  24. float avg = sum / count ;
  25. cout<<endl<<"The avg of all values of column 5 is : "<<avg<<endl;
  26. }

thank you
Last edited by Dave Sinkula; Jan 9th, 2006 at 10:54 am. Reason: Fixed [code][/code] tags.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: other col

 
0
  #2
Jan 9th, 2006
make the column number a variable so that you can change it as desired.
  1. int col = 7;
  2. for(int i = 0; i < col; i++)
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 29
Reputation: masa is an unknown quantity at this point 
Solved Threads: 1
masa masa is offline Offline
Light Poster

Re: other col

 
0
  #3
Jan 9th, 2006
Hi Anceint dragon
you are right about it.
I tried to do like that, but it giving me wrong result
  1. if ( input_file.fail ())
  2. {
  3. cout << "Error opening file.";
  4. }
  5. else
  6. {
  7. for(int i = 0; i < 7; i++) // Scan the first 7 lines, which is not required
  8. getline(input_file, line, '\n');
  9. count = 8; // We are in the eighth line
  10. while(count <= 1447)
  11. { // We have to add until 1447 lines..
  12. ++col;
  13. if (col =5)
  14. { for(int i = 0; i < col; i++)
  15.  
  16. getline(input_file, line, ','); // Get the first 5 columns.
  17. cout<<line<<endl; // Print the 5th column for verification.
  18. value = atof(line.c_str());// Convert the 5th column into float
  19. sum += value; //add value to sum
  20. getline(input_file, line, '\n'); // Go to the next line..*/
  21. }
  22. count++; // Increment the count, to go to the next line
  23.  
  24. if ( col =7)
  25. {for(int i = 0; i < col; i++)
  26.  
  27. getline(input_file, line, ','); // Get the first 7 columns.
  28. cout<<line<<endl; // Print the 7th column for verification.
  29. value1 = atof(line.c_str());// Convert the 7th column into float
  30. sum1 += value1; //add value to sum
  31. getline(input_file, line, '\n'); // Go to the next line..*/
  32. }
  33. count++; // Increment the count, to go to the next line
  34. }
  35. cout << count;
  36. cout<<endl<<"The sum of all values of column 5 is : "<<sum<<endl;
  37. cout<<endl<<"The sum of all values of column 7 is : "<<sum1<<endl;
  38. float avg = sum / count ;
  39. cout<<endl<<"The avg of all values of column 5 is : "<<avg<<endl;
  40. float avg1 = sum1 / count ;
  41. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: other col

 
0
  #4
Jan 9th, 2006
  1. 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 == ???)
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 29
Reputation: masa is an unknown quantity at this point 
Solved Threads: 1
masa masa is offline Offline
Light Poster

Re: other col

 
0
  #5
Jan 9th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: other col

 
0
  #6
Jan 9th, 2006
simply use the col variable number. You can also put that code in another function that takes the column number as a parameter.
  1. int main()
  2. {
  3. int main()
  4. {
  5. ifstream input_file("filename");
  6. if ( !input_file.is_open())
  7. {
  8. cout << "Error opening file.";
  9. }
  10. else
  11. {
  12. for(int i = 0; i < 7; i++) // Scan the first 7 lines, which is not required
  13. getline(input_file, line, '\n');
  14. count = 8; // We are in the eighth line
  15. int col = 5;
  16. while(count <= 1447)
  17. { // We have to add until 1447 lines..
  18. for(int i = 0; i < col; i++)
  19. getline(input_file, line, ','); // Get the first 5 columns.
  20. cout<<line<<endl; // Print the 5th column for verification.
  21. value = atof(line.c_str());// Convert the 5th column into float
  22. sum += value; //add value to sum
  23. getline(input_file, line, '\n'); // Go to the next line..
  24. count++; // Increment the count, to go to the next line
  25. }
  26. cout << count;
  27. cout<<endl<<"The sum of all values of column 5 is : "<<sum<<endl;
  28.  
  29. float avg = sum / count ;
  30. cout<<endl<<"The avg of all values of column 5 is : "<<avg<<endl;
  31. }
  32.  
  33. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 29
Reputation: masa is an unknown quantity at this point 
Solved Threads: 1
masa masa is offline Offline
Light Poster

Re: other col

 
0
  #7
Jan 10th, 2006
Hi
thanks I will do it in a function.
one think, in the previous post, it has the code
  1. int main()
  2. {
  3. int main()
  4. {
  5. ....
  6. }
  7. }
is a paste error, or ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,401
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: other col

 
0
  #8
Jan 10th, 2006
yes, that was a copy/paste error.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 29
Reputation: masa is an unknown quantity at this point 
Solved Threads: 1
masa masa is offline Offline
Light Poster

Re: other col

 
0
  #9
Jan 10th, 2006
Hi
I did this function, it return only one column value ( I need the rest as well)
  1. int pass_column(int column);
  2. int main()
  3. {
  4.  
  5. ifstream input_file("Log_05.csv");
  6. string line;
  7. int count,col=0;
  8. float sum = 0.0, value;
  9. float max = 0.0;
  10. if ( input_file.fail ())
  11. {
  12. cout << "Error opening file.";
  13. }
  14. else
  15. {
  16. for(int i = 0; i < 7; i++) // Scan the first 7 lines, which is not required
  17. getline(input_file, line, '\n');
  18. count = 8; // We are in the eighth line
  19. while(count <= 1447) // We have to add until 1447 lines..
  20. {
  21.  
  22. for(int i = 0; i < pass_column(col); i++)
  23.  
  24. getline(input_file, line, ','); // Get the first col columns.
  25. cout<<line<<endl; // Print the col column for verification.
  26. value = atof(line.c_str());// Convert the col column into float
  27. sum += value; //add value to sum
  28. getline(input_file, line, '\n'); // Go to the next line..
  29. if ( max < value)
  30. {
  31. max= value;
  32. }
  33. col++;
  34. count++; // Increment the count, to go to the next line
  35.  
  36. }
  37.  
  38. cout << count;
  39. cout<<endl<<"The max of all values of column 5 is : "<<max<<endl;
  40. cout<<endl<<"The sum of all values of column 5 is : "<<sum<<endl;
  41. float avg = sum / count ;
  42. cout<<endl<<"The avg of all values of column 5 is : "<<avg<<endl;
  43.  
  44.  
  45. }
  46.  
  47. return 0;
  48. }
  49.  
  50. int pass_column(int column)
  51. {
  52. int i=0;
  53. while ( i<196)
  54. {
  55. switch (i)
  56. {
  57. case 15:
  58. column=i;
  59. return (column);
  60.  
  61. break;
  62. case 193:
  63. column=i;
  64. return (column);
  65. break;
  66. case 195:
  67. column=i;
  68. return (column);
  69. break;
  70. }
  71. i++;
  72. }
  73.  
  74. }
I know function return only one value , what could I do ????????????????????
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 29
Reputation: masa is an unknown quantity at this point 
Solved Threads: 1
masa masa is offline Offline
Light Poster

Re: other col

 
0
  #10
Jan 11th, 2006
Hi ancient dragon

  1. float sumcol(int col,string line)
  2. {
  3. int pos;
  4. float sums[196] = {0.0};
  5. // find the (col-1)th comma in the string
  6. pos = 0;
  7. for(int k = 0;k < line.size() && pos < (col-1); k++)
  8. if(line[k] == ',')
  9. pos++;
  10. // remove the first (col-1) columns from the string
  11. line = line.substr(pos+1);
  12.  
  13. return atof(line.c_str());
  14. }
  15.  
  16. int main()
  17. {
  18.  
  19. ifstream input_file("Logos_05.csv");
  20. string line;
  21. int count;
  22. float sums[196] = {0.0};
  23. float value;
  24. float max = 0.0;
  25. if ( input_file.fail ())
  26. {
  27. cout << "Error opening file.";
  28. }
  29. else
  30. {
  31. for(int i = 0; i < 7; i++) // Scan the first 7 lines, which is not required
  32. getline(input_file, line, '\n');
  33. count = 8; // We are in the eighth line
  34. while(count <= 1447)
  35. { // We have to add until 1447 lines..
  36. getline(input_file,line);
  37. for(int col = 0; col < 196; col++)
  38. sums[col] += sumcol(col,line);
  39. count++; // Increment the count, to go to the next line
  40.  
  41. }
  42.  
  43. cout << count;
  44. for(i = 0; i < 196; i++)
  45. {
  46. cout<<endl<<"The sum of all values of column " << i+1 << " is : "<<sums[i]<<endl;
  47. float avg = sums[i] / count ;
  48. cout<<endl<<"The avg of all values of column " << i+1 << " is : "<<avg<<endl;
  49. }
  50.  
  51.  
  52. }
  53.  
  54. return 0;
  55. }
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC