gpt4 book ai didi

c++ - 在编程 : Principles and Practice using C++ (Stroustrup) 的第 4 章中使用 "cin"钻 #7 获得不同的结果

转载 作者:行者123 更新时间:2023-11-30 01:49:06 28 4
gpt4 key购买 nike

我正忙于学习第 2 版《编程:使用 C++ 的原理与实践》(Stroustrup),并且在读取值时遇到了有无空格的问题,然后再次显示它们。

能否请您指出正确的方向,以便我找出结果不同的原因?

代码编译正常:c++ -std=c++11 -o 7 7.cpp

输入测试结果:

  • 10 公里(有效)
  • 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 << "\n\n\nGive 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 << "\n\n";

while (resp != '|') {
cout << "\n\n\nGive 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 far\n";
}
if (value < smallest_so_far)
{
smallest_so_far = value;
cout << "You gave me " << smallest_so_far << ", the smallest so far\n";
}


cout << "Another one? ('|' to stop, any other char for one more) : ";
cin >> resp;
}
}

导致失败的值(例如“10cm”)最终会导致程序进入一个奇怪的循环。

OUTPUT: Give me a floating point value: Another one? ('|' to stop, any other char for one more) : Give me a floating point value: Another one? ('|' to stop, any other char for one more) : Give me a floating point value: Another one? ('|' to stop, any other char for one more) :

然后我必须按 CTRL-C 退出。

一个更小的测试,多亏了 MSalters...

#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 << "\n\n\nGive me a floating point value and a unit (ex. 10 cm): ";
if (cin >> value >> unit)
cout << "Success, got 2 values\n";
else
cout << "Failure!\n";

cout << "\nYou gave me the following: " << value << " " << unit << '\n';

}

~/dev/cpp/ch4/drill/./test给我一个浮点值和一个单位(例如 10 厘米):10 厘米失败!你给了我以下内容:0

~/dev/cpp/ch4/drill/./test给我一个浮点值和一个单位(例如 10 厘米):10 公里成功,得到2个值你给了我以下信息:10 公里

~/dev/cpp/ch4/drill/./test给我一个浮点值和一个单位(例如 10 厘米):10 公里成功,得到2个值你给了我以下信息:10 公里

~/dev/cpp/ch4/drill/./test给我一个浮点值和一个单位(例如 10 厘米):10 厘米成功,得到2个值你给了我以下:10 厘米

为什么只有当字符串以小于“k”的字符开头时才会失败?

最佳答案

我也遇到过这个bug,所以我问了这个问题的重复版本,希望得到答案。这是我的发现:

  • 这似乎是 clang 使用的标准库之一中的实际错误。这个确切错误的错误报告是 linked here .

  • 该错误似乎与转换说明符有关,这可以解释为什么只有某些字母而不是其他字母受其影响。可以作为转换说明符的字母(例如,10.0f 表示 float )被库以不同方式解析,在这种情况下,它会影响 cin 正确拆分 double 和字符串的能力。

  • 2年过去了,bug还是没有解决...

我用 -stdlib=libstdc++ 重新编译了我自己有类似问题的代码,这是一个不同于默认 libc++ 库的库。它现在可以正常工作,尽管字母 E 仍然不能接受,因为它表示指数。

非常感谢 stackoverflow 用户 Petesh 找到了这个问题的真正答案。

关于c++ - 在编程 : Principles and Practice using C++ (Stroustrup) 的第 4 章中使用 "cin"钻 #7 获得不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29561466/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com