[英]Getting different results using “cin” for Drill #7 in Chapter 4 of Programming: Principles and Practice using C++ (Stroustrup)
我正在忙於閱讀第二版《編程:使用C ++的原理和實踐》(Stroustrup),並且在讀取值之間是否有空格時遇到問題,然后再次顯示它們。
能否請您指出正確的方向,以便我找出結果為何不同?
代碼可以正常編譯:c ++ -std = c ++ 11 -o 7 7.cpp
輸入測試結果:
這是代碼,還有一些測試輸入/輸出,結果不同。
#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”)最終導致程序進入奇怪的循環。
輸出:給我一個浮點值:另外一個? (“ |”停止,還有其他一個字符):給我一個浮點值:另一個? (“ |”停止,還有其他一個字符):給我一個浮點值:另一個? (“ |”停止,還有其他一個字符):
然后,我必須按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 cm):10cm失敗! 您給了我以下內容:0
〜/ dev / cpp / ch4 / drill / ./test給我一個浮點值和一個單位(例如10 cm):10km成功,得到2個值您給我以下內容:10 km
〜/ dev / cpp / ch4 / drill / ./test給我一個浮點值和一個單位(例如10 cm):10 km成功,得到2個值您給我以下內容:10 km
〜/ dev / cpp / ch4 / drill / ./test給我一個浮點值和一個單位(例如10 cm):10 cm成功,得到2個值您給我以下內容:10 cm
為什么它僅以字符串以小於“ k”的字符開頭的字符串失敗?
輸入操作可能會失敗。 測試起來很容易: 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
}
我也遇到了這個錯誤,所以我問了這個問題的一個重復版本,希望能得到答案。 這是我發現的:
在clang使用的標准庫之一中,這似乎是一個實際的錯誤。 此確切錯誤的錯誤報告已鏈接到此處 。
該錯誤似乎與轉換說明符有關,該說明符解釋了為什么只有一些字母而不是其他字母受它影響。 庫可能會以不同的方式解析可能是轉換說明符的字母(例如,浮點數為10.0f),在這種情況下,它會影響cin正確拆分雙精度和字符串的能力。
已經2年了,但該錯誤仍未解決...
我使用-stdlib = libstdc ++重新編譯了自己同樣有問題的代碼,該庫與默認的libc ++庫不同。 它現在可以正常工作,盡管字母E仍然不能接受,因為它表示指數。
非常感謝stackoverflow用戶Petesh找到了該問題的真正答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.