Converting an int to a char (C++)

For all coding issues - MODers and programmers, HTML and more.

Moderators: Jeff250, fliptw

Post Reply
User avatar
SSX-Thunderbird
DBB Admiral
DBB Admiral
Posts: 1275
Joined: Sun Jun 03, 2001 2:01 am
Location: Washington (the state, not the city)

Converting an int to a char (C++)

Post by SSX-Thunderbird »

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: Select all

 tags? All i instances changed to j to dodge this.
User avatar
DCrazy
DBB Alumni
DBB Alumni
Posts: 8826
Joined: Wed Mar 15, 2000 3:01 am
Location: Seattle

Post by DCrazy »

[edit]See below.[/edit]
SolidAir
DBB Alumni
DBB Alumni
Posts: 2890
Joined: Sat Mar 13, 1999 3:01 am
Location: Orlando, FL

Post by SolidAir »

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: Select all

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
SolidAir
DBB Alumni
DBB Alumni
Posts: 2890
Joined: Sat Mar 13, 1999 3:01 am
Location: Orlando, FL

Post by SolidAir »

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
User avatar
DCrazy
DBB Alumni
DBB Alumni
Posts: 8826
Joined: Wed Mar 15, 2000 3:01 am
Location: Seattle

Post by DCrazy »

<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.
Post Reply