problems using pow() in sprintf()
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]
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?
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement