|
||
|
GP Mailing List
ATXGPSIG List
|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] pointers and text-based rpg problem
Hi,
I'm writing a text-based rpg in C++ right now and I've been writing some
code that will take in any text and will print it out in a text box and
delineate after every 50 characters. It compiles correctly, but every time
I run it, the text that is output is only a diamond (I don't know the exact
ASCII character it is but it is just a diamond). The code is like this:
void tbox(char *text) {
int totchars = strlen(text);
int numfields =(int)(totchars/50);
if(numfields<1){
numfields = 1;
}
//Please note that these notes below were written to myself, so when
//I say "you" I wrote that talking to myself
/*I get the number of characters and divide them by 50 (the length of
the text in the text box) and cast it to an int. I also check and make
sure that during the casting process, numfields didn't go to 0; I make
it at least one....I mean, there must be at least some characters in
the string!!! You must remember, I am dividing by 50, some strings
may not be that long so they would be 0.243 or whatever, which is not
and int value, so during the conversion, it might just be cast to 0,
which would be disastrous. :)*/
char buffer[numfields][51];//buffer to read text;50 +1 for null byte
int i,j;
for(i=0;i<3;i++) {
strncpy(buffer[i],text,50);
}//end for...i loop
for(j=0;j<20;j++);{
printf("%s \n",&buffer[j]);
}//end for....j loop
}//end function
That is the code for the actual function, but the function call
looks like this:
void main() {
char test[] ="The Lord is my Shepherd. I shall not want. He maketh me
to lie down in green pastures. He leadeth me beside the still waters.
He restoreth my soul. He leadeth me in the path of righteousness for
His names sake.";
tbox(test);
}
As I said, this code compiles correctly, but upon the test
output of the text in the buffer, it prints only a little ASCII diamond.
Anyone's help would be greatly appreciated.
Also, as I am working with C++, the idea of pointers and
encapsulation have come to my mind and they seem to step on each other's
foot. For example, if I were to create my own string class like so:
class newstring{
private:
strdata[345];
public:
char *get(void);
}
wouldn't the person using this class still be able to change strdata
directly, without having to use some function, because of the fact that they
have its address? What I'm saying is, couldn't they just write some
characters to that address? I ask this because the author of my book does
it like that, and I'm just wondering if, instead of returning an address to
strdata, returning strdata by value would follow the rules of class
encapsulation better. Any and all help and comments are greatly
appreciated. Thank you
Peace and may God
direct your life
Amen, Jonathan Smith
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com
=================================================================
The GameProgrammer.Com mailing list is for the open discussion
of any topic related to the art, science, and business of
programming games. This list is especially tolerant of beginners.
We were all beginners once
To SUBSCRIBE or UNSUBSCRIBE please visit:
http://gameprogrammer.com/mailinglist.html
|
|