Advertisement

sprintf need to clarify

Started by April 27, 2002 06:13 AM
3 comments, last by edwinnie 22 years, 4 months ago
i modified this part of the code from TOTWGPG. char buffer[256]; int player_score=0;
  
void Draw_Info(void)
{

char score[6];

// build up score string

sprintf(score,"000000%d",player_score);

// build up final string

sprintf(buffer," %s",&score[strlen(score)-6]);
Draw_Text_GDI(buffer,510,310,RGB(0,0,255),lpddsback);

}//end draw_info

  
but applying in the game, seems to crash after the 1st increase in player_score. mebbe its the sprintf() thingy which i kinda not very clear abt it.
or is it possible to convert to I/O streams in c++ style?
Advertisement
You can''t store this score-string in an array consisting of 6 characters. Try score[100] for example, that''ll probably work.
Heh, dude.. you''re trying to stick SIX zero''s and the value of player_score into a char array of 6 chars. Firstly, why do you prepend 6 chars to a 6 char array (you need a 7 char array for this due to the null terminator).

There is a much better way to prepend the 0''s to score and display it.


  int player_score=0;void Draw_Info(void){ char score[7];//create our player_score with prepended 0''s of 6-digits sprintf(score,"%6d",player_score); Draw_Text_GDI(score,510,310,RGB(0,0,255),lpddsback);}//end draw_info  


Why would you try to pre-pend 0''s to a string, while it''s a built in function. The only thing you''ll want to check in my version, is that player_score isn''t >= 1000000, because if it is, it will jump to 7 chars (while his version would have just displayed the last 6 digits, while my version would display all 7.. so you could do something like this also..


  int player_score=0, display_score=0;void Add_Score(int Add){ player_score+=Add;//use a seperate variable so when we wrap around//we still have the actual score also! display_score=player_score%1000000;}void Draw_Info(void){ char score[7];//create our player_score with prepended 0''s of 6-digits sprintf(score,"%6d",display_score); Draw_Text_GDI(score,510,310,RGB(0,0,255),lpddsback);}//end draw_info  


Also notice how my version uses 7 chars to hold 6 digits... 6 digits + 1 terminating char = 7! I also don''t need/use a 256 byte buffer, save a call of strlen, and save one call to sprintf. Although, he could have saved a call to sprintf in his version to, the only thign he did in his second sprintf was to print a single string... while the argument for the Draw_Text_GDI looks for a string, so he could have just passed that orginally. Here''s an example of how his code can/would work properly:


  int player_score=0;void Draw_Info(void){ char score[17]; //size for at least 6 0''s + 10 digits+1 terminator.//create our player_score with prepended 0''s of 6-digits sprintf(score,"000000%d",player_score); Draw_Text_GDI(&score[strlen(score)-6],510,310,RGB(0,0,255),lpddsback);}//end draw_info  


Billy - BillyB@mrsnj.com
thx!! yours is so much easier to understand!

This topic is closed to new replies.

Advertisement