Jump to content
  • Advertisement
Sign in to follow this  
raptorstrike

problems using pow() in sprintf()

This topic is 5064 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

ok i have the following lines (well at least thats all you have to worry about) and for some reason this inserts a 0 after the | not pow(temp_left,temp_right) here it is: char Temp_Char[50]; sprintf(Temp_Char, "%d\0", pow(temp_left,temp_right)); reader.insert(reader.find("|",0)+1,Temp_Char); couple other things you migh want to know, temp_left ^ temp_right = 4 reader is a valid string, and i have include the math.h thanks for the help[smile]

Share this post


Link to post
Share on other sites
Advertisement
thats alot the problem was the \0 i dont know what it was doing i dont know much about sprintf but i changed the "%d" to "%f" and now i get the right answer (4) but i also get six zeros after that. Is there a way to trim the zeros?

Share this post


Link to post
Share on other sites
Quote:
Original post by raptorstrike
thats alot the problem was the \0 i dont know what it was doing i dont know much about sprintf but i changed the "%d" to "%f" and now i get the right answer (4) but i also get six zeros after that. Is there a way to trim the zeros?


No, the \0 was simply redundant, as "%d" already is {'%', 'd', '\0'}. The problem really was that you used %d instead of %f, so printf expected an int and not a float. Variadic functions do not do type conversions, so it just took the bits of the 4.0 float value and reinterpreted them as an int

And, yes, there is a way to trim the zeros, but no, I am not going to tell you here, because you'll be much, much better served by reading the printf format string documentation and by figuring out the logic of it yourself.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!