#include #include #include #include #include using namespace std; int main() { string filename("djia.txt"); ifstream fin(filename); if (!fin.is_open()) { cerr << "Cannot open " << filename << endl; exit(1); } string outputfilename("djia_without_commas.txt"); ofstream fout(outputfilename); if (!fout) { cerr << "Cannot open " << outputfilename << endl; exit(2); } char ch; float open, close, gain; string dummy; while (!fin.eof()) { ch = fin.get(); if (ch != ',') fout << ch; } fin.close(); fout.close(); fin.open(outputfilename); if (!fin.is_open()) { cerr << "Cannot open " << outputfilename << endl; exit(4); } cout << setprecision(2) << fixed << boolalpha; while (fin.get() != '\n') ; float totalgain = 0; int count = 0; while (!fin.eof()) { fin >> dummy >> dummy >> dummy >> open >> dummy >> dummy >> close >> dummy >> dummy; if (fin.eof()) break; if (count == 5) break; gain = close - open; cout << open << ' ' << close << ' ' << gain << ' ' << fin.good() << endl; totalgain += gain; count++; } cout << "Average gain is $" << totalgain/count << endl; }