vendredi 27 mars 2015

How to add values in STL:: List container?


Im writing a program to add values of money within a stl::list container. How could I parse through and add the values together? Thanks. Code i want to use this in is below... I added the accumulate at the bottom.



void readFile()
{
string line, nuMoney, munney, cents;
unsigned long dollarSign, period;


ifstream myfile("MONEY.txt");

int numLines = countLines("MONEY.txt");
//cout << "NUMLINES: " << numLines << endl;

if (!myfile.is_open())
{
cout << "ERROR: File could not be opened." << endl;
}

for (int i=0; i < numLines-1; i++)
{
getline(myfile, line, '\n');
dollarSign = line.find('$');
period = line.find('.');

line.erase (remove(line.begin(), line.end(), ','), line.end()); //remove commas

munney = line.substr(dollarSign+1);
//cout << munney << endl;

double MONEYS = atof(munney.c_str());
//cout << fixed << setprecision(2) << MONEYS << '\n';

list<double> acct;
acct.push_back(MONEYS);

}

int sum = accumulate(acct.begin(), acct.end(), 0);
cout << "SUM: " << sum << endl;

}

}



Aucun commentaire:

Enregistrer un commentaire