如何在不丢失第一位数字的情况下打印出双精度值
How to print out a double value without losing first digit
当我 运行 我的代码时,它只打印双精度的小数部分。在另一页上,我输入了一个双打并按照输入的方式打印出双打。
但是对于我下面的代码,它只打印出小数。例如,当我输入 1.95 时,它只打印出 0.95。为什么要删除第一个数字?我在我的代码中看不到任何指向此的内容。
我已经尝试过一种更简单的方法并且成功了。而且我在我的代码中没有发现任何会影响 double 的问题。
#include <iostream>
using namespace std;
int main()
{
double price;
char user_input;
do
{
cout << "Enter the purchase price (xx.xx) or `q' to quit: ";
cin >> user_input;
if (user_input == 'q')
{
return 0;
}
else
{
cin >> price;
int multiple = price * 100;
if (multiple % 5 == 0)
{
break;
}
else
{
cout << "Illegal price: Must be a non-negative multiple of 5 cents.\n" << endl;
}
}
} while (user_input != 'q');
cout << price << endl;
}
当我输入 1.95 时,我得到 0.95。但是输出应该是1.95.
当您在命令行中键入 1.95 时,变量 user_input
被分配为“1”,而 price
被分配为 .95。
其他答案中涉及的问题:读取 'q'
时会在第一个字符解析为 double
.
之前将其从流中移除
解决方法:先阅读double
。如果读取失败,请检查输入是否为 'q'
.
#include <iostream>
#include <limits>
using namespace std;
int main()
{
double price;
while (true)
{
cout << "Enter the purchase price (xx.xx) or `q' to quit: ";
if (cin >> price)
{
// use price
}
else // reading price failed. Find out why.
{
if (!cin.eof()) // didn't hit the end of the stream
{
// clear fail flag
cin.clear();
char user_input;
if (cin >> user_input && user_input == 'q') // test for q
{
break; // note: Not return. Cannot print price if the
// program returns
}
// Not a q or not readable. clean up whatever crap is still
// in the stream
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
else
{
// someone closed the stream. Not much you can do here but exit
cerr << "Stream closed or broken. Cannot continue.";
return -1;
}
}
}
cout << price << endl;// Undefined behaviour if price was never set.
}
另一个合理的选择是将所有输入读取为 std::string
。如果 string
不是 "q"
,请尝试将其转换为带有 std::stod
的 double
或 std::istringstream
.
当我 运行 我的代码时,它只打印双精度的小数部分。在另一页上,我输入了一个双打并按照输入的方式打印出双打。 但是对于我下面的代码,它只打印出小数。例如,当我输入 1.95 时,它只打印出 0.95。为什么要删除第一个数字?我在我的代码中看不到任何指向此的内容。
我已经尝试过一种更简单的方法并且成功了。而且我在我的代码中没有发现任何会影响 double 的问题。
#include <iostream>
using namespace std;
int main()
{
double price;
char user_input;
do
{
cout << "Enter the purchase price (xx.xx) or `q' to quit: ";
cin >> user_input;
if (user_input == 'q')
{
return 0;
}
else
{
cin >> price;
int multiple = price * 100;
if (multiple % 5 == 0)
{
break;
}
else
{
cout << "Illegal price: Must be a non-negative multiple of 5 cents.\n" << endl;
}
}
} while (user_input != 'q');
cout << price << endl;
}
当我输入 1.95 时,我得到 0.95。但是输出应该是1.95.
当您在命令行中键入 1.95 时,变量 user_input
被分配为“1”,而 price
被分配为 .95。
其他答案中涉及的问题:读取 'q'
时会在第一个字符解析为 double
.
解决方法:先阅读double
。如果读取失败,请检查输入是否为 'q'
.
#include <iostream>
#include <limits>
using namespace std;
int main()
{
double price;
while (true)
{
cout << "Enter the purchase price (xx.xx) or `q' to quit: ";
if (cin >> price)
{
// use price
}
else // reading price failed. Find out why.
{
if (!cin.eof()) // didn't hit the end of the stream
{
// clear fail flag
cin.clear();
char user_input;
if (cin >> user_input && user_input == 'q') // test for q
{
break; // note: Not return. Cannot print price if the
// program returns
}
// Not a q or not readable. clean up whatever crap is still
// in the stream
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
else
{
// someone closed the stream. Not much you can do here but exit
cerr << "Stream closed or broken. Cannot continue.";
return -1;
}
}
}
cout << price << endl;// Undefined behaviour if price was never set.
}
另一个合理的选择是将所有输入读取为 std::string
。如果 string
不是 "q"
,请尝试将其转换为带有 std::stod
的 double
或 std::istringstream
.