C++ getline 不适用于 double

C++ getline not working with double

出于某种原因,getline 不能与 double 一起使用,它给了我一条消息说 "getline no instance of overloaded function "getline" 匹配参数列表参数类型是:(std::istream, double)" 如果我​​更改双打字符串它有效所以我不确定问题是什么,如果有人可以提供帮助,将不胜感激

使用

cin >> athOneTime;

提取一个double。如果您想跳过该行的其余部分,请使用

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

那是因为std::getline is for reading strings. And only strings. If you want to get a floating point value read it as a string and convert为浮点值。或者使用输入运算符 >>.

出现此问题是因为 getline() 只读取字符串。要读取双精度数,您必须使用 cin.