hello, I have to write a program that takes floating point numbers from another file and prints out the average. The problem is the other file submits three columns so I don't know how to do the average for the three separate columns.

This code is terrible and I can't figure out how to do it. any help?


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ( ){
double number (0),sum(0);
int nCount(0);
string filename;
ifstream fin;

cerr << "Enter the File Name ";
cin >> filename;
fin.open( filename.c_str( ) );
if( fin.fail( ) ) {
cerr << "ERROR FILE COULD NOT BE OPENED " << filename << endl;
exit( -1 );

cout << number;
sum += number;

cout << "\nThe average for the sequence of numbers above is: " << sum/nCount << endl;
cout << endl;

return 0;

8 Years
Discussion Span
Last Post by csurfer

First of all your code here is incomplete and doesn't serve the purpose until further alterations. Second if you are sure of the three columns present then instead of ">>" getline with specific modifications to fetch the columns separately and store it in different arrays and then calculate their average.


how do you do that? I'm really sorry.

Well study about getline() here. After doing it successfully you can change the getline function as per the format of your file and get your work done... Try it sincerely and post your queries here.You'll definitely get all the help required as long as you show some effort.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.