如何在字符串中添加数字?

Jas*_*n94 1 c++

我想做这样的事情(显示我在SDL游戏中运行的FPS):

SDL_WM_SetCaption("FPS: " + GetTicks(&fps)/1000.f, NULL);
Run Code Online (Sandbox Code Playgroud)

但Visual Studio intellisens抱怨表达式必须具有整数或枚举类型.

我做错了什么?

Nim*_*Nim 7

如果这真的是C++,请考虑流;

std::ostringstream str;    
str << "FPS: " << GetTicks(&fps)/1000.;    
SDL_WM_SetCaption(str.str().c_str(), NULL);
Run Code Online (Sandbox Code Playgroud)


Som*_*ude 5

C不支持将简单类型(如intfloat)转换为更复杂的类型(如字符串).

你应该检查sprintf功能:

char buffer[64];
sprintf("FPS: %f", GetTicks(&fps)/1000.f);
SDL_WM_SetCaption(buffer, NULL);
Run Code Online (Sandbox Code Playgroud)