* FAQ    * Search  * Register * Login 
Active topics
Unanswered topics

All times are UTC-06:00



Post new topic  Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Converting an int to a char (C++)
PostPosted: Mon Feb 02, 2004 11:11 am 
Offline
DBB Admiral
DBB Admiral
User avatar

Joined: Sun Jun 03, 2001 2:01 am
Posts: 1273
Location: Washington (the state, not the city)
I am attempting to make descent.sng obsolete in D2X, and have most of the code for this done, but there is one problem: In checking for the game(level).mid files, I do the following:

<BLOCKQUOTE><font size="1" face="Arial">code:</font><HR><pre> j = 5;
while(j<10){
> if (cfexist(strcat("game0", strcat(i, ".mid")))){
sprintf(Songs[j] .filename, strcat("game0", strcat(i, ".mid")));
sprintf(Songs[j] .melodic_bank_file, "d2melod.bnk");
sprintf(Songs[j] .drum_bank_file, "d2drums.bnk");
j++;
} else {
break;
}
}
if (!(j<10)){
while(j>9){
if (cfexist(strcat("game", strcat(i, ".mid")))){
sprintf(Songs[j] .filename, strcat("game",strcat(i, ".mid")));
sprintf(Songs[j] .melodic_bank_file, "d2melod.bnk");
sprintf(Songs[j] .drum_bank_file, "d2drums.bnk");
j++;
} else {
break;
}
}
}
</pre><HR></BLOCKQUOTE>

The code compiles fine, but GPFs on the line with the >. Is there some way I can correct this?

And I do realize that there's a flaw in the implementation, where it will only check descent2.hog and not the loaded mission (this code is done at startup). This is not the problem I face, however, and I will correct that later.

(EDIT)What the hell, I thought UBB code wasn't supposed to trigger between [code] tags? All i instances changed to j to dodge this.


Top
   
 Post subject:
PostPosted: Mon Feb 02, 2004 11:51 am 
Offline
DBB Alumni
DBB Alumni
User avatar

Joined: Wed Mar 15, 2000 3:01 am
Posts: 8826
Location: Seattle
[edit]See below.[/edit]


Top
   
 Post subject:
PostPosted: Mon Feb 02, 2004 11:53 am 
Offline
DBB Alumni
DBB Alumni

Joined: Sat Mar 13, 1999 3:01 am
Posts: 2890
Location: Orlando, FL
I assume cfexist checks to see if a file exists.

First, you are using strcat incorrectly. It performs string concatenation, and it takes a pointer to a char, and a constant pointer to a char. Consider this:

Code:
char *strcat( char *strDestination, const char *strSource );


A valid use of this function would be as follows:

<BLOCKQUOTE><font size="1" face="Arial">code:</font><HR><pre>
char szBuffer[256]={0};

strcat(szBuffer, "hello");
strcat(szBuffer, " world");

printf(szBuffer); // displays "hello world"
</pre><HR></BLOCKQUOTE>

Because you are passing a constant as the first argument, say, "game0", the program automatically allocates enough memory to hold "game0" in memory and retrieves the address to give to strcat. However, strcat's first parameter is the szDestination parameter, and the memory allocated was for a constant. When strcat attempts to write to the address, that is when you get your GPF.

Now for your second call, which is strcat(i, ".mid"). What you are trying to do here is to write ".mid" to the memory address 0x00000005. This memory is not yours to touch.

Now I am going to imagine that you are trying to see if game01.mid, game02.mid, game03.mid and such exists. There is a simpler way to do it.

<BLOCKQUOTE><font size="1" face="Arial">code:</font><HR><pre>
j = 5;

while(j<10){
char szFilename[260]={0};
sprintf(szFileName, "game0%d.mid", j);
if (cfexist(szFileName)){
sprintf(Songs[j] .filename, szFileName);
sprintf(Songs[j] .melodic_bank_file, "d2melod.bnk");
sprintf(Songs[j] .drum_bank_file, "d2drums.bnk");
j++;
} else {
break;
}
}
if (!(j<10)){
while(j>9){
char szFilename[260]={0};
sprintf(szFileName, "game0%d.mid", j);
if (cfexist(szFileName)){
sprintf(Songs[j] .filename, strcat("game",strcat(i, ".mid")));
sprintf(Songs[j] .melodic_bank_file, "d2melod.bnk");
sprintf(Songs[j] .drum_bank_file, "d2drums.bnk");
j++;
}
else {
break;
}
}
}
</pre><HR></BLOCKQUOTE>
Keep in mind that this code will only write game00.mid to game09.mid. If you have 10, then it will write game010.mid. Alternatively, your call to sprintf could be like this:

<BLOCKQUOTE><font size="1" face="Arial">code:</font><HR><pre>
sprintf(szFileName,"game%d%d.mid",
( j >= 10 ? (int) ( j / 10 ) : '0' ),
( j >= 10 ? (int) ( j % 10 ) : j ) );
</pre><HR></BLOCKQUOTE>

Therefore if j = 27, j / 10 would be 2, and j % 10 would be 7.

Good luck.

Matt / SolidAir


Top
   
 Post subject:
PostPosted: Mon Feb 02, 2004 12:01 pm 
Offline
DBB Alumni
DBB Alumni

Joined: Sat Mar 13, 1999 3:01 am
Posts: 2890
Location: Orlando, FL
Also... to convert a number j to a character, when j >= 0 && j <= 9, you can do it like this:

<BLOCKQUOTE><font size="1" face="Arial">code:</font><HR><pre>

char number = j + '0';

</pre><HR></BLOCKQUOTE>

If j = 0, then 0 + '0', or, 0 + 48 (the number that '0' represents) would make the char number equal to '0'. If j were to equal 7, then 7 + '0' or, 7 + 48 would equal 55 which is the number that corresponds to '7' in the ascii table.

Keep in mind that this only works for numbers 0 through 9. If it were 10, it would point to ':'

matt


Top
   
 Post subject:
PostPosted: Tue Feb 03, 2004 8:44 am 
Offline
DBB Alumni
DBB Alumni
User avatar

Joined: Wed Mar 15, 2000 3:01 am
Posts: 8826
Location: Seattle
<tt>printf("game%02d.mid", i)</tt> will work for any number from 0 to 99. If i is from 0 through 9 it will be prefixed with a 0.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 5 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Descent'rs have piloted these pages
 
The layout and contents contained within this site are © DescentBB.net 1997-2006.
Descent, Descent II are © Parallax Software Corporation.
Descent III is Outrage Entertainment.
Descent is a Trademark of Interplay Productions.

Miner Wars™ is trademark of Keen Software House s. r. o.
.


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group