Archived

This topic is now archived and is closed to further replies.

print(int x) and print(float x)

This topic is 5402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have two functions to output a number on a graphical SDL screen with C: print(int number, x,y,r,g,b) and print(float number, x,y,r,g,b) The int function prints the number as 4 and the float function as 4.000000 The problem is, C always uses the float function, even if I call it with an integer in the parameters! How can I make it to work so that it always uses the int function if I give it an int, and the float function if I give it a float? I'm looking for a solution that's better than the following solutions, because they all require a different way to call the print function or output a different result: *) if(x-int(x) == 0) print as int, otherwise as float (because if I give it a float that's 4, I still want it to look like 4.000000) *) add an extra parameter to the function to say if it should use %i or %f (more typing work) *) do something with pointers when calling the print function (doing it inside the function is OK as long as you can still call it as print(number,x,y,r,g,b) ) Is this possible in C? Please help, thanks. [edited by - Lode on March 2, 2003 6:42:25 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I reckon C doesn''t have overloading so you''re out of luck. Or switch to C++. Then you could pass a string to the function instead of different numbers:

void print(const std::string& text, int x, int y, int r = 255, int g = 255, int b = 255) {
//print it
}

The last =255s mean default argument, so the function can be called even with just print("yeah", 10, 10); and the text will be white. Or you can use boost::format to print numbers (slightly inconviently though)

print(boost::format("My age: %1%" % age, 10, 10);

(don''t lynch me if I didn''t remember syntax exactly right)

www.boost.org

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Argh.. Gay smileys
Of course it should''ve been

print(boost::format( "My age: %1%" ) % age, 10, 10);

(sorry for the expression that discriminates homosexuals unjustly. I just had to)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can get rid of the typing work, actually type even less than you''d have to with your C approach:

  
void print(const std::string& text, int x, int y, etc) {
//draw text on screen

}

template <typename T>
void print(T t, int x, int y, etc) {
stringstream ss;
ss << t;
print(ss.str(), x, y, etc);
}

Now it would work directly to all types that have operator<<(ostream& out, type t) overloaded.

print(5, 10, 10);
print(6.0, 10, 10);

both would work.

Share this post


Link to post
Share on other sites
Here's an approach where you create a function that behaves like printf, taking a variable number of any type of argument you want, and even lets you do formatting like %0.2f:


    
#include <stdarg.h>
#include <stdio.h>

int printtext(const char* text, int x, int y, int r, int g, int b);

int print(const char* fmt, int x, int y, int r, int g, int b, ...)
{
char buf[256];
va_list marker;
va_start(marker, b);
vsnprintf(buf, 255, fmt, marker);
va_end(marker);

return printtext(buf, x, y, r, g, b);
}

int printtext(const char* text, int x, int y, int r, int g, int b)
{
/*
* Handle printing plain text here
*/

}


Now you can call: print("Fuel Remaining: %0.2f Gallons", x, y, r, g, b, fFuelRemaining); or whatever you want.

Cheers, dorix


[edited by - dorix on March 2, 2003 11:05:56 AM]

Share this post


Link to post
Share on other sites