In my program im trying to read in a file with money like this "$876,725.38" I was able to remove the comma and dollar sign and I am now trying to convert the string to a double using atof but the issue im having now is after printing MONEYS
after the conversion I get everything before the period on majority of these, but some with less than 6 digits before the period do print either one or two digits after the period. If anyone has a solution to this it would be greatly appreciated. I'm new to C++ and need help. Thanks!
MAIN ISSUE: Converting from string to double, the data after decimal does not print.
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;
//cents = line.substr(period);
//cout << "Cents: " << cents << endl;
//double CENTUS = atof(cents.c_str());
//cout << "CENTUS: " << CENTUS << endl;
double MONEYS = atof(munney.c_str());
cout << MONEYS << endl;
list<double> acct;
acct.push_back(MONEYS);
}
}
sample output:
937380
404261
814158
30218.1
69340.1
479891
517241
7203.55
975619
59219.4
900052
539774
336799
347700
532466
83496.8