我正忙于使用C++(Stroustrup)完成《编程:原理与实践》的第二版,在读取值之间有空格和没有空格的值时遇到问题,然后再次显示它们。
你能告诉我正确的方向吗?这样我就能找出结果不同的原因?
代码编译良好:c++-std=c++11-o7.cpp
输入测试结果:
- 10km(工程)
- 10公里(工程)
- 10厘米(作品)
- 10厘米(无效)
这是代码以及一些具有不同结果的测试输入/输出。
#include "../../std_lib_facilities.h"
/*
Add a unit to each double entered; that is, enter values such as 10cm, 2.5in, 5ft, or 3.33m.
Accept the four units: cm, m, in, ft. Assume conversion factors 1m == 100cm, 1in ==
2.54cm, 1ft == 12in. Read the unit indicator into a string. You may consider 12 m (with a
space between the number and the unit) equivalent to 12m (without a space).
*/
int main()
{
double value = 0.0;
string unit = "";
double largest_so_far = 0.0;
double smallest_so_far = 0.0;
char resp = 'y';
// Conversions
constexpr double m = 100.0; // 1m = 100cm
constexpr double in = 2.5; // 1in = 2.54cm
constexpr double ft = 12.0; // 1ft = 12in
cout << "nnnGive me a floating point value and a unit (ex. 10 cm): ";
cin >> value >> unit;
largest_so_far = value;
smallest_so_far = value;
cout << "nYou gave me the following: " << value << " " << unit << 'n';
cout << "Thank you. The largest and smallest so far is now " << value << "nn";
while (resp != '|') {
cout << "nnnGive me a floating point value: ";
cin >> value;
if (value > largest_so_far)
{
largest_so_far = value;
cout << "You gave me " << largest_so_far << ", the largest so farn";
}
if (value < smallest_so_far)
{
smallest_so_far = value;
cout << "You gave me " << smallest_so_far << ", the smallest so farn";
}
cout << "Another one? ('|' to stop, any other char for one more) : ";
cin >> resp;
}
}
导致故障的值(如"10cm")最终会在程序中进入一个奇怪的循环。
OUTPUT:给我一个浮点值:另一个?('|'停止,任意另一个字符表示一个):给我一个浮点值:另一个一('|'表示停止,任何其他字符表示一个):给我一个浮动点值:另一个?('|'表示停止,任何其他字符表示一个以上):
然后我必须按CTRL-C退出。
一个较小的测试,感谢微软的改变。。。
#include "../../std_lib_facilities.h"
/*
Add a unit to each double entered; that is, enter values such as 10cm, 2.5in, 5ft, or 3.33m.
Accept the four units: cm, m, in, ft. Assume conversion factors 1m == 100cm, 1in ==
2.54cm, 1ft == 12in. Read the unit indicator into a string. You may consider 12 m (with a
space between the number and the unit) equivalent to 12m (without a space).
*/
int main()
{
double value = 0.0;
string unit = "";
cout << "nnnGive me a floating point value and a unit (ex. 10 cm): ";
if (cin >> value >> unit)
cout << "Success, got 2 valuesn";
else
cout << "Failure!n";
cout << "nYou gave me the following: " << value << " " << unit << 'n';
}
~/dev/cpp/ch4/drill//测验给我一个浮点值和一个单位(例如10厘米):10厘米失败你给了我以下信息:0
~/dev/cpp/ch4/drill//测验给我一个浮点值和一个单位(例如10厘米):10公里成功,获得2个值你给了我以下信息:10公里
~/dev/cpp/ch4/drill//测验给我一个浮点值和一个单位(例如10厘米):10公里成功,获得2个值你给了我以下信息:10公里
~/dev/cpp/ch4/drill//测验给我一个浮点值和一个单位(例如10厘米):10厘米成功,获得2个值你给了我以下:10厘米
为什么它只在字符串以小于"k"的字符开头时失败?
我也遇到过这个错误,所以我问了这个问题的重复版本,希望能得到答案。以下是我的发现:
-
这似乎是clang使用的一个标准库中的一个实际错误。此处链接了此错误的错误报告。
-
这个错误似乎与转换说明符有关,这可以解释为什么只有一些字母而没有其他字母受到它的影响。一个可能是转换说明符的字母(例如,浮点数为10.0f)会被库以不同的方式解析,在这种情况下,它会影响cin正确拆分双精度和字符串的能力。
-
两年过去了,这个错误仍未解决。。。
我用-stdlib=libstdc++重新编译了我自己的类似问题代码,这是一个与默认libc++库不同的库。它现在正常工作,尽管字母E仍然不可接受,因为它表示指数。
非常感谢stackoverflow用户Petersh找到了这个问题的真正答案。
输入操作可能会失败。它很容易测试:if(cin) /* All went right so far */
或if (!cin) /* something went wrong */
。
现在的问题是,当出现问题时,必须先清除故障状态,然后才能接受新的输入:cin.clear()
。此外,在您的代码中,您应该在尝试使用value
和unit
之前测试提取是否有效。
专业提示:你可以把这个组合起来。
if (cin >> value >> unit)
{
// Success - value and unit now hold the users input
}
else
{
// uh oh, that's not good
}