Thread: Really Weird itoa Problem

  1. #1
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55

    Question Really Weird itoa Problem

    For my breakout clone, I need to convert the integers for the score and the level number into arrays of characters so I can display them on the screen with Allegro's textout function. I searched on google for a function to convert, and I found "itoa". So, it seemed to work fine. I converted "level" to "leveltex" and "score" to "scoretex" and displayed them on the screen. But then I noticed something weird was happening. Whenever "score" hit 10,000, it caused "level" to jump to 45. This happens even if there is no command telling "level" to change it's quantity in the entire program. I noticed that if I didn't convert score using itoa, "level" did not jump to 45. So, whenever "score" hits 10,000 and is converted with itoa, "level" jumps to 45. Here is the itoa part of the code:
    Code:
    void DrawHUD()
    {
    	itoa(level,leveltex,10);
    	itoa(score,scoretex,10);
    	textout(buffer,font,"Level:",700,120,makecol(255,255,255));
    	textout(buffer,font,leveltex,700,130,makecol(255,255,255));
    	textout(buffer,font,"Score:",700,90,makecol(255,255,255));
    	textout(buffer,font,"10000",700,100,makecol(255,255,255));
    }
    Nowhere in the program does it tell level to change. What's going on? Is there another part of the code I should show you guys?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Survey says,
    textprintf_ex or textprintf
    http://www.allegro.cc/manual/api/tex.../textprintf_ex
    Woop?

  3. #3
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    But this doesn't seem to be a problem with the output of text, just the conversion.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    I know I was just making your life easier. Using a non-standard function is not always a good idea.
    Woop?

  5. #5
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    What do you mean non-standard? There both Allegro functions.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    itoa is a non-standard function. Like I said my first suggestion does the fix all. Just simply do this
    Code:
    textprintf_ex(screen, font, 700, 120, makecol(255,255,255), - 1, "Score: %d", score);
    Woop?

  7. #7
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    All it did is flicker "Score: -1". OH wait, it was flickering because I didn't draw it to the buffer first.

  8. #8
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    Nevermind, It works now. Thanks! I still don't see why itoa did what it did though, but I guess it doesn't matter.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Thanks! I still don't see why itoa did what it did though, but I guess it doesn't matter.
    Oh it still matters - all that you've done is sweep the problem under the carpet for it to reappear in another place at another time. Only next time, you might not have the repeatability of this bug.

    Your description suggests a string length overflow problem. I bet if you try the same thing in a very small program - just that function and enough main() to call it, you'll either see no problem at all, or it will be obvious where you're going wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird problem with sscanf
    By g4j31a5 in forum C++ Programming
    Replies: 17
    Last Post: 10-04-2006, 09:16 PM
  2. Weird problem on '02 3.4L V6 auto
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-12-2006, 12:05 AM
  3. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  4. Weird mouse problem in XP
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 06-16-2004, 11:47 AM
  5. Weird class problem!
    By aker_y3k in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2002, 06:12 AM