我想对 getline() 的所有迭代求和并将其输出到我正在读取的文件中。然而,我目前的代码想法使程序崩溃。
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
}
最佳答案
问题出在
string total;
double balance = stod(total);
声明一个字符串后,您不知道 std::stod
将返回什么
修复:
string total;
getline(in, total);
double balance = stod(total);
关于c++使用stod的getline()的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45705815/