[英]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.