-
itoa
Hi
I am trying to get an allegro game/tutorial working on linux and found out itoa is not a good way to convert integer to ascii but I am having trouble implementing the substitution I found by searching the internet.
sprintf(buf, "%d", intval);
The lines in the code giving me the problem are
textout(buffer,pong_datafile[pong_text].dat,"Player 1 Score:",150,0,254);
textout(buffer,pong_datafile[pong_text].dat,itoa(score_p1,NULL,10),text_length(pong_dataf ile[pong_text].dat,"Player 1 Score:")+150,0,10);
textout(buffer,pong_datafile[pong_text].dat,"Player 2 Score:",350,0,254);
textout(buffer,pong_datafile[pong_text].dat,itoa(score_p2,NULL,10),text_length(pong_dataf ile[pong_text].dat,"Player 2 Score:")+350,0,10);
itoa is used (I suppose) to convert a number extracted from the pong.dat file to text. I can delete the two lines containing the itoa and it compiles fine of course.
How should sprintf be implemented in this context?
Thanks
-
Well, guessing that the 3rd parameter of textout() is a char*, you'd need to use sprintf() on a line of its own, and then put the buffer into textout().
Something like:
Code:
char buf[BUFSIZ];
sprintf (buf, "%d", num);
textout(buffer,pong_datafile[pong_text]. dat,buf,text_length(pong_datafile[pong_text].dat,"Player 2 Score:")+350,0,10);
But I don't have textout() or itoa(), so I can't test it for you.
-
Thanks!
sprintf (buf, "%d", score_p1);
textout(buffer,pong_datafile[pong_text].dat,"Player 1 Score:",150,0,254);
textout(buffer,pong_datafile[pong_text].dat,buf,text_length(pong_datafile[pong_text].dat,"Player 1 Score:")+150,0,10);
textout(buffer,pong_datafile[pong_text].dat,"Player 2 Score:",350,0,254);
sprintf (buf, "%d", score_p2);
textout(buffer,pong_datafile[pong_text].dat,buf,text_length(pong_datafile[pong_text].dat,"Player 2 Score:")+350,0,10);
and I am in business. For char buf[bufsize] I just a put large enough a number to work:-)
Thanks again. I learned something!
-
>>For char buf[bufsize] I just a put large enough a number to work
BUFSIZ (note the caps) is defined in stdio.h already. I can't guarantee what size it is set to, open your stdio.h file and have a look for it.
-
OOPS! I jumped to the conclusion that it was something I had to deal with since you included it in your example. There I go asuming again! I will look at it as soon as get back to my linux box. If it were not for Linux I would not have a stdio.h to look in to.
Thanks!