-1

我有以下内容:

char op; double x, y, z;
istringstream iss("v 1.0 2.0 3.0", istringstream::in);
iss>>op>>x>>y>>z;

但是在输出 x、y 和 z 的值时,它们都返回 0?

更新:

我想它正在工作,但我将其输出为:

int length=wsprintf(result," V is %d, %d, %d ", x, y, z);
TextOut(hdc,0,0,result,length);

它没有显示正确的值。

但是,如果值在 int 中,它可以正常工作,例如:

char op; int x, y, z;
istringstream iss("v 1 2 3", istringstream::in);
iss>>op>>x>>y>>z;
4

2 回答 2

1

%d格式说明符需要一个,int但是x,y并且z是 类型double。如果类型和格式说明符不匹配,则行为未定义。请注意,参考页面中wsprintf似乎没有任何格式说明符用于double.

建议使用std::wostringstreamandstd::wstring代替:

std::wostringstream ws;
ws << L" V is " << x << L"," << y << L"," << z;
const std::wstring result(ws.str());
TextOut(hdc,0,0,result.c_str(), result.length());
于 2013-01-03T12:15:30.860 回答
0

谢谢hmjd。

对于其他任何人,这是我为正确输出值所做的。

int length=sprintf_s(result," V is %0.1f, %0.1f, %0.1f ", x, y, z);
TextOut(hdc,0,0,result,length);

一般语法“%AB”表示小数点前A位和小数点后B位。

谢谢大家。

于 2013-01-03T12:26:37.197 回答