fork download
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. int main() {
  5. int total = 0;
  6. int count = 0;
  7. int col0,col1,col2; // Just using 3 columns
  8.  
  9. std::ifstream inFile("input.txt");
  10. if (!inFile) {
  11. std::cerr << "Couldn't open input file\n";
  12. return 1;
  13. }
  14.  
  15. while (inFile >> col0 >> col1 >> col2) {
  16. std::cout << col1 << '\n';
  17. total += col1;
  18. ++count;
  19. }
  20. double average = static_cast<double>(total)/count;
  21. std::cout
  22. << "\nTotal: " << total
  23. << "\nCount: " << count
  24. << "\nAverage: " << average
  25. << '\n';
  26. }
  27. /*
  28. $ cat input.txt
  29. 31544 10002 29012
  30. 21861 15788 29698
  31. 2202 12665 611
  32. 9745 8703 16891
  33. 19286 13571 23521
  34. 11796 32397 4005
  35. 8004 9233 15889
  36. 26117 16622 16538
  37. 7805 28468 6108
  38. 28608 21207 26347
  39. 11781 19984 3581
  40.  
  41. $ ./SO
  42. 10002
  43. 15788
  44. 12665
  45. 8703
  46. 13571
  47. 32397
  48. 9233
  49. 16622
  50. 28468
  51. 21207
  52. 19984
  53.  
  54. Total: 188640
  55. Count: 11
  56. Average: 17149.1
  57. */
Runtime error #stdin #stdout #stderr 0s 3268KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Couldn't open input file