[英]C++ Filling a Char Array with Multiple Strings and Floats
char msg[40];
string s = "The price is $";
float price = 120.00;
string input = " and the tax is $";
float tax = 5.00;
例如,我要用“价格为$ 120.00美元,税为$ 5.00”填充msg。 到目前为止,我无法正常工作的代码是:
msg = s + price + input + tax;
我现在陷入困境,无法解决问题,感谢您的帮助。 谢谢。
您应该将msg
声明为std::string
。
然后,您可以使用stringstream
至漂浮转换为字符串或C ++ 11,你可以使用函数std::to_string(arg)
与各种支持的类型arg
。
您可以使用c函数sprintf
(除非要使用c ++字符串,在这种情况下, stringstream
是一个简单的选择。
这可以通过
sprintf(msg, "The price is $%.2f and the tax is $%.2f", price, tax);
还有就是一个很好的说明printf
语法在这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.