[EDITED, related problem] increment string with a char at a time function

I have this implementation that increments a string with one character at a time, which let's me render a sentence one character at a time.

However, i've tried to move it to a function but can't make it work. have you any tips?


This works:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
char Line1[] 			        = "HELLO\0";
const int endofLine1 		    = sizeof(Line1);
char delayedText[endofLine1] 	= {};

int currentLetter 		        = 0;
int delay		 	            = 10;
int currTime 			        = delay


while(...)
{
				if(currentLetter <= endofLine1)
				{
					char nextChar = Line1[currentLetter]; 
					if(currTime == delay)
					{
							strcat(delayedText, &nextChar);
							currentLetter++;
							currTime = 0;
					}
					else
					{
						currTime++;
					}
				}

}




calling DelayTxt() doesn't work, it only prints the first char to many times:
1
	DelayTxt(Line1, endofLine1, &currentLetter, &currTime, delay, delayedText);



DelayTxt() :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
void DelayTxt(char line[], int lastLetter, int *currentLetter, int *currTime, int delay, char delayedText[])
{
	if(*currentLetter <= lastLetter)
	{
		char nextChar = Line1[*currentLetter]; 
		if(*currTime == delay)
		{
				strcat(delayedText, &nextChar);
				*currentLetter++;
				*currTime = 0;
		}
		else
		{
			*currTime++;
		}
	}
}

Edited by C_Worm on
Hard to tell without more context, but in this line:
1
char nextChar = Line1[*currentLetter];

did you mean to write:
1
char nextChar = line[*currentLetter];

?

Also, have you tried stepping into the function in a debugger and look at what's going on? (try to change a time delay with a "call count" delay if needed).
Probably not related to your issue, but this:
1
strcat(delayedText, &nextChar);

is a very incorrect code.
strcat will append string from second argument to string in first argument.
You are not passing string in second argument. You are passing pointer to one character. You need to pass pointer to array of characters, where last one in 0. Currently you are lucky that program does not simply crash. Because strcat will start reading characters from memory after nextChar - basically it will get some garbage values.

But this:
1
*currTime++;

will not do what you probably want. This code will increment currTime pointer, not the value it points to. Same with *currentLetter++. Use a debugger to verify this.

Edited by Mārtiņš Možeiko on
mmozeiko
Probably not related to your issue, but this:
1
strcat(delayedText, &nextChar);

is a very incorrect code.
strcat will append string from second argument to string in first argument.
You are not passing string in second argument. You are passing pointer to one character. You need to pass pointer to array of characters, where last one in 0. Currently you are lucky that program does not simply crash. Because strcat will start reading characters from memory after nextChar - basically it will get some garbage values.

But this:
1
*currTime++;

will not do what you probably want. This code will increment currTime pointer, not the value it points to. Same with *currentLetter++. Use a debugger to verify this.




Okay but now i reformated the code and came upon another problem. Now when i go into case: showtext, the program renders the delayed text.

however when i go back to case MainMenu: and then go back into case showText: once again.

the delayed text works like the first 7 char and the completes the rest of the sentence in like 1 frame. i've stepped through the code but can understand why this behavior occurs.


excerpt of main.cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
DelayedTXT dt1 = {};
dt1.init("HELLO YOU SON OF A MONKEY", 3);

while(running)
{
    switch(gameState)
    {
        case MainMenu:
        {
			dt1.currentLetter = 0;
			memset(dt1.textDest, 0, sizeof(dt1.textDest));
			memset(dt1.resultString, 0, sizeof(dt1.resultString));

        }break;

        case showtext:
        {

             DT(&dt1)

        }break;

    }
}


DelayTime struct
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct DelayedTXT
{
	char *textSrc;
	char *textDest;
	char *resultString;

	int stringLength	= 0;
	int currentLetter 	= 0;
	int delay		= 0;
	int currentTime		= 0;

	void init(char *in_text, int in_delay)
	{
		textSrc 	= (char*)calloc(256, sizeof(char));
		textDest 	= (char*)calloc(256, sizeof(char));
		resultString 	= (char*)calloc(256, sizeof(char));

		strcpy(textSrc, in_text);
		stringLength = strlen(in_text);

		delay = in_delay;
		currentTime = in_delay;
	}
};


DelayText Function():
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
void DT(DelayedTXT *dt)
{
	if((dt->currentLetter) < dt->stringLength)
	{
		if((dt->currentTime) == dt->delay)
		{
			dt->textDest[dt->currentLetter] = dt->textSrc[dt->currentLetter];
			strcpy(dt->resultString, dt->textDest);

			(dt->currentLetter) += 1;
			(dt->currentTime) = 0;
		}
		else
		{
			(dt->currentTime) += 1;
		}
	}
}

Edited by C_Worm on
Please use a debugger to verify variables and values you use.
Step line by line and check input arguments, and verify resulting variable value.
For example: memset(dt1.textDest, 0, sizeof(dt1.textDest)); Here the sizeof(dt1.texDest) is always 8. Always an 8. Is that what you want? No? Then what should be correct value?
Verify it line by line. That's how debugging works.