c ++使用stod的getline()的总和
c++ the sum of getline() using stod
我想对 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
将要 return
修复:
string total;
getline(in, total);
double balance = stod(total);
您正在尝试将 total
转换为 double
,但它具有 null
值,而 null
字符串无法转换为任何数据类型。这就是它产生异常的原因。在为它分配一个 String 后,您必须将 total
转换为 double 。在使用 stod()
功能之前,请阅读 total
。
我想对 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
将要 return
修复:
string total;
getline(in, total);
double balance = stod(total);
您正在尝试将 total
转换为 double
,但它具有 null
值,而 null
字符串无法转换为任何数据类型。这就是它产生异常的原因。在为它分配一个 String 后,您必须将 total
转换为 double 。在使用 stod()
功能之前,请阅读 total
。