Thread: Argh... I need help with strings = |

  1. #1
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Argh... I need help with strings = |

    While this question isn't exactly C++ specific...

    Code:
    while (c < (sizeof(entires) )) { 
    		c++;
    		achar = entires[c];
    		if (strcmp("^", achar)) {
    			DSPLINE+=8; }
    		if (strcmp("%", entires[c])) {
    			Rr = 220; Gg = 212; Bb = 8;
    		}
    		if (strcmp("$", entires[c])) {
    			Rr = Gg = Bb = 245;
    		}
    		if (strcmp("@", entires[c])) {
    			donet = true; break;
    		}
    		strcpy(tmptxt[c], entires[c]);
    		CLEV+=8;
    		displaytranstext(bitmd, tmptxt, X +CLEV+12, Y + DSPLINE+12, 244, 244, 244);
    	}
    Anyways, what I'm trying to do is a message system for an RPG I'm writing. When you display a message box, the system goes through the string passed to it. If it encounters a "^", it breaks and goes to the next line. If it encounters a "%" it changes the text color, a "$" changes it back, and a "@" denotes the end of one message box of text.

    The problem: I'm not sure how to do this, the code above is terrible, and everything I do seems to merit errors. Can someone help me out?
    Last edited by rmullen3; 01-26-2002 at 06:37 PM.

  2. #2
    first of all:

    achar = entires[c];

    Why? You only use achar to compare in the first strcmp and the rest of the time you use 'entires[c]'...

    Second:

    You can do this legally (instead of strcmp):

    if (entires[c] == '%')

    likewise:

    tmptxt[c] = entires[c];

    As you're dealing with type char here and not a pointer to an array of char, you can work it just like any other variable. (Even tmptxt[c] ++; if you wanted... not sure why though.)

    Also, i think you want strlen(entires) not sizeof(entires).

    See how that helps and post more if you ahve more problems.

    /* Edit */
    Also notice that you're incrementing your variable 'c' ("c ++;" which is amusing me btw) at the beggining of your while loop. That means that you're starting your checking on the second element of the string. Arrays start at 0 so 'entires[0]' is actually your first element. Unless 'c' is set to '-1' before this loop or you move the 'c++' to the end of the loop you're missing out on the first letter of your string.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    ; ) It is set to -1.

    Anyway, now the problem is that the display is screwed up...!

    Here's the total code:

    Code:
    void displaytranstext(BITMAP* out, char* TEXT, int X, int Y, int R, int G, int B)
    {
    	// cannot use for numbers. at all.
    	int TH = text_height(font), TW = text_length(font,TEXT);
    	BITMAP *textbuffer= create_bitmap(TW, TH);
    	text_mode(makecol(255, 0, 255));
    	textprintf(textbuffer, font, 0, 0, makecol(R, G, B), "%s", TEXT);
    	masked_blit(textbuffer, out, 0, 0, X, Y, textbuffer->w, textbuffer->h);
    	destroy_bitmap(textbuffer); 
    	text_mode(0x00000000);
    }
    
    // that's the function that displays the text.
    // here's the function that parses it:
    
    // display text
    	int c = -1,DSPLINE = 0, Rr, Gg, Bb, CLEV = 0 , LN = strlen(entires);
    	bool dispt = true; char *achar = new char[1];
    	while (c < LN) { 
    		++c;
    		achar[0] = entires[c];
    		dispt = true;
    		if (entires[c] == '^') { DSPLINE+=8; dispt = false; } 
    		if (entires[c] == '%') { dispt = false;
    			Rr = 220; Gg = 212; Bb = 8; }
    		if (entires[c] == '$') { Rr = Gg = Bb = 245; dispt = false; }		
    		if (entires[c] == '@') { dispt = false;}
    		CLEV+=8;
    		if (dispt)
    			displaytranstext(bitmd, achar, X +CLEV+12, Y + DSPLINE+12, 244, 244, 24);
    	}
    (Oh yes, I do want displaytranstext to be able to take arguments like " Blah ", etc)

    But the result is a garbled mess of ASCII characters. Can anyone help out? =)

  4. #4
    Couple of things:
    before: c++;
    after: ++c;

    I always thought c++ was a lot more intuitive, myself. I prefer it. Why the change? My comment about being ammused by it was solely on the fact that i've never actually seen the code 'c++' used in the 'c++' language before. Its obviously complety normal... i was just.. amused...

    Anyhow, some optomizations (I cant stop myself):

    1) I still dont understand 'achar'. It appears to be redundant. Instead try passing a pointer to entires[c] (example: '&entires[c]').

    2) Instead of using your 'dispt' boolean, try formatting your code like this: (just a suggestion, its not really all that important)
    Code:
    		CLEV+=8;
    		if (entires[c] == '^')
    		{
    			DSPLINE+=8;
    		}
    		else if (entires[c] == '%')
    		{
    			Rr = 220; Gg = 212; Bb = 8;
    		}
    		else if (entires[c] == '$')
    		{
    			Rr = Gg = Bb = 245;
    		}
    		else if (entires[c] == '@')
    		{
    			//not sure what this is supposed to be doing;
    		}
    		else
    			displaytranstext(bitmd, achar, X +CLEV+12, Y + DSPLINE+12, 244, 244, 24);
    3) Shouldn't you be passing you Rr, Gg, and Bb variables to displaytranstext instead of 244, 244, and 24? (Just being nosy, as you probably have your reasons for this.)

    But as for the garbled ASCII stuff, i really dont know. Is the function outputting an equal number of characters to the number of characters in the string? Are they always the same weird characters every time for the same string or are they always random characters? Are they in the right place on the screen? As I dont know what these (text_mode, masked_blit, etc) functions are, i cant test it out myself. What compiler? DJGPP by any chance?

    Could you give me an example of a string you passed in and the output on your screen?
    Could you show me how 'entires' is defined and how you use it? Is it just passed to this function that draws text (i.e. DrawText(char * entires) ) or is it declared and then assigned a value (i.e. char * entires; entires = new char[10]; strcpy(entires, "stuff"); )?
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Hehe, I didn't change it to ++c because you said anything =P

    Well, I fixed it all up. SORTA. It works, but only when I made a new function that takes a simple char as argument rather than char*. When I try to use the original function that takes the char*, I get the scrambled text.

    I say
    Code:
    char *achar = new char[1]; 
    // stuff...
    achar[0] = entires[c];
    // stuff...
    displaytranstext(bitmd, achar, X +CLEV+12, Y + DSPLINE+12, Rr, Gg, Bb); // this function takes a char* (at the achar place) argument
    Although it works, I don't want two functions... How can I pass the char into the char* without jarbling up the letters? =)

  6. #6
    As i said before, pass a pointer to the element of entires that you are currently set to. It would look like this:

    displaytranstext(bitmd, &entires[c], X +CLEV+12, ...

    A 'char * ' is really just a pointer to an area of memory. The call to new char [x] allocates as much memory as you want during run time.

    char * NewChar = new char[10];

    will give you a character (char) array with 10 elements and the memory for it will be allocated by your program at that point during execution.
    char NewChar[10];

    will allocate you a 10 element char array when your program starts. Its faster and less prone to memory leaks because you dont have to delete your memory later. Only use new if you dont know how much space you're going to need and you dont want to waste any.

    Keep in mind though (just thought i'd make myself clear so i cant ever be blamed for confusing you ) that you cant ever compare a 'char *' like i showed you you could compare the 'char': if (entires[c] == 'y'). char * has to be compared with strcmp, and manipulated with strcpy, strcat, etc, just the way you had it before.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  7. #7
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Okay ^_^

    I got it working but the problem now is that it doesn't skip characters... For example, the following code does not work (it displays the ^,%,&,# characters =O)

    Code:
    	while (c < LN) {
    		++c;
    		CLEV+=8;
    		if (entires[c] == '^') { DSPLINE+=8; CLEV=0; }
    		else if (entires[c] == '%') { Rr = 242; Gg = 216; Bb = 12;  }
    		else if (entires[c] == '$') { Rr = Gg = Bb = 245; }
    		else if (entires[c] == '#') { Rr = 24; Gg = 148; Bb = 250; }
    		else 
    			displaytranstext(bitmd, &entires[c], X +CLEV+12, Y + DSPLINE+12, Rr, Gg, Bb);
    
    	}
    But this works: (I have to use a different function though = /)

    Code:
    	while (c < LN) {
    		++c;
    		CLEV+=8;
    		if (entires[c] == '^') { DSPLINE+=8; CLEV=0; }
    		else if (entires[c] == '%') { Rr = 242; Gg = 216; Bb = 12;  }
    		else if (entires[c] == '$') { Rr = Gg = Bb = 245; }
    		else if (entires[c] == '#') { Rr = 24; Gg = 148; Bb = 250; }
    		else  
    			displaytranslet(bitmd, entires[c], X +CLEV+12, Y + DSPLINE+12, Rr, Gg, Bb);
    
    	}
    They both do what they should do (^ breaks the line, %/$/# changes the text color, I got rid of the @), but in the first one it displays the bad characters as well... Am I missing something? =)

    Thanks for the help ^^

  8. #8
    Thats not the code i'd need to see. displaytranstext() is obviously the culprit whereas whatever you've done in displaytranslet() works. So if you know what works... why worry about what doesnt?
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  9. #9
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Hm

    These are both functions.
    Code:
    void displaytranslet(BITMAP* out, char TEXT, int X, int Y, int R, int G, int B)
    {
    	int TH, TW; TH = TW = text_height(font);
    	BITMAP *textbuffer= create_bitmap(TW, TH);
    	text_mode(makecol(255, 0, 255));
    	textprintf(textbuffer, font, 0, 0, makecol(R, G, B), "%c", TEXT);
    	masked_blit(textbuffer, out, 0, 0, X, Y, textbuffer->w, textbuffer->h);
    	destroy_bitmap(textbuffer);
    	text_mode(0x00000000);
    }
    
    void displaytranstext(BITMAP* out, const char* TEXT, int X, int Y, int R, int G, int B)
    {
    	int TH = text_height(font), TW = text_length(font,TEXT);
    	BITMAP *textbuffer= create_bitmap(TW, TH);
    	text_mode(makecol(255, 0, 255));
    	textprintf(textbuffer, font, 0, 0, makecol(R, G, B), "%s", TEXT);
    	masked_blit(textbuffer, out, 0, 0, X, Y, textbuffer->w, textbuffer->h);
    	destroy_bitmap(textbuffer);
    	text_mode(0x00000000);
    }
    One takes a const char * (ie, a string), and displaytranslet takes a character and displays it. I want to have one function that will allow me to pass 1 or more letters to it and display it on screen... =) Gracias if you can help
    Last edited by rmullen3; 01-27-2002 at 10:56 PM.

  10. #10
    From the looks of how things are set up, this would work perfectly:

    void DrawFullTextString(short StartAtX, short StartAtY, char * entires)
    {
    //blah blah setup variables here
    while (c < LN) {
    ++c;
    CLEV+=8;
    if (entires[c] == '^') { DSPLINE+=8; CLEV=0; }
    else if (entires[c] == '%') { Rr = 242; Gg = 216; Bb = 12; }
    else if (entires[c] == '$') { Rr = Gg = Bb = 245; }
    else if (entires[c] == '#') { Rr = 24; Gg = 148; Bb = 250; }
    else
    displaytranslet(bitmd, entires[c], X +CLEV+12, Y + DSPLINE+12, Rr, Gg, Bb);

    }
    }

    If you just call displaytranstext() with one lump string then you dont get any of that character checking and stuff thats going on. This method is may be a little slow though if you're after speed performance... But if its for a text game or something (likely by the looks of things) then theres nothing to worry about.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  11. #11
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Okay, I'll just go with using the other function. Heh, it's not a text game =P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM