Dance Zombie Dance Zombie -3 years ago 181
C++ Question

c++ the sum of getline() using stod

I would like to take the sum of all the iterations of getline() and output it to the file I'm reading. However, this current idea of code I have makes the program crash.

int main()
{
// usual read file stuff
while (!in.eof())
{

string total;
double balance = stod(total);
getline(in, total);
cout << "$";
cout << right << setw(10) << total << '.' << cout.precision(2) << endl;
//guessing a for loop that would += balance
}

Answer Source

The problem lies in

string total;
double balance = stod(total);

After declaring a string, you have no idea what the std::stod is about to return

fix:

string total;
getline(in, total);
double balance = stod(total);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download