1

I want to do something like this (showing what FPS im running at in my SDL game):

SDL_WM_SetCaption("FPS: " + GetTicks(&fps)/1000.f, NULL); 

But Visual Studio intellisens complains that expression must have integral or enum type.

What have i done wrong?

2
  • 1
    If it's C++, you should be looking at std::string. Commented Nov 21, 2011 at 10:33
  • removed SDL tag: this question is about string formatting in C++, not about SDL. Commented Nov 21, 2011 at 15:35

4 Answers 4

7

If this really is C++, consider streams;

std::ostringstream str; str << "FPS: " << GetTicks(&fps)/1000.; SDL_WM_SetCaption(str.str().c_str(), NULL); 
Sign up to request clarification or add additional context in comments.

Comments

5

C does not support conversion of simple types (like int or float) to more complex types (like strings).

You should check the sprintf function:

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

1 Comment

It is a C++ question, not a C one (edit: I realise the question has been edited)
1

In C, you can do this using sprintf.

Check out this link:

http://msdn.microsoft.com/en-us/library/ybk95axf(v=vs.71).aspx

Don't use + for adding char pointers (that's what they are in C, not strings).

EDIT:

If this is C++, as per the edit, use std::string, which has the + operator overloaded. You'll still need to convert the number to a string though.

Also, it's called concatenation.

Comments

0

You can either use strings, or if you are already settling for the new current C++ standard, there is also std::to_string:

#include <string> #include <iostream> int main() { const std::string str = "Foobar " + std::to_string(42); std::cout << str << '\n'; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.