Thread: itoa

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    5

    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

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    5
    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!

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    5
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using itoa
    By g_p in forum C Programming
    Replies: 2
    Last Post: 05-03-2008, 06:38 AM
  2. Problem with itoa() perhaps?
    By TheSquid in forum C++ Programming
    Replies: 5
    Last Post: 05-08-2006, 02:04 AM
  3. Really Weird itoa Problem
    By Grantyt3 in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 12:44 AM
  4. itoa undeclared?
    By curlious in forum C++ Programming
    Replies: 16
    Last Post: 12-11-2004, 05:30 PM
  5. itoa
    By Thantos in forum C Programming
    Replies: 2
    Last Post: 09-18-2001, 02:23 PM