code help needed! again. Created 12 years ago2011-06-11 15:56:22 UTC by 2muchvideogames 2muchvideogames

Created 12 years ago2011-06-11 15:56:22 UTC by 2muchvideogames 2muchvideogames

Posted 12 years ago2011-06-11 15:57:04 UTC Post #295480
Hello TWHL, so in my attempts to make a stable codebase so that you all can see the something presentable, I need your help again.

This time we are looking at chat (client.cpp)

[quote]// make sure the text has content
[blue]for[/blue] ( [blue]char[/blue] *pc = p; pc != NULL && *pc != 0; pc++ )
{
	[blue]if[/blue] ( isprint( *pc ) && !isspace( *pc ) )
	{
		pc = NULL;	[green]// we've found an alphanumeric character,  so text is valid[/green]
		[blue]break[/blue];
	}
}
[blue]if[/blue] ( pc != NULL )
[blue]return[/blue];  [green]// no character found, so say nothing[/green][/quote]
It's not something complicated. I just need to declare the 'pc' outside of that for loop, because the bottom if statement occurs outside this loop. I tried putting "char pc;" in the front, but that appears to just silence chat. All this fun is due to the microsoft developing software not being backward compatible. (We already know microsoft didn't give a crap about backward compatibility when they made Windows 7)
Posted 12 years ago2011-06-11 16:52:04 UTC Post #295482
You must not leave the asterisk out. It would become:

char *pc;

for( *pc = p ; pc!=NULL && *pc != 0 ; pc++)
{
...
}
Posted 12 years ago2011-06-11 17:51:12 UTC Post #295484
ya, tried that. Then it says 'cannot convert char * to char'

Any suggestions?
Posted 12 years ago2011-06-11 18:25:47 UTC Post #295485
Call Batman Penguinboy.
Posted 12 years ago2011-06-11 18:59:54 UTC Post #295487
char *pc = NULL;

for( pc = p ; pc!=NULL && *pc != 0 ; pc++)
{
...
}
Posted 12 years ago2011-06-11 19:40:48 UTC Post #295488
char *pc = NULL; defines a pointer to data of type char, initially pointing to NULL (0 - nothing). pc = p sets pc's value to that of p - p and pc will point to the same data. *pc = p sets the data which pc is pointing at (which is of type char) to the value of p (which is a pointer to a char).
Oskar Potatis Oskar Potatis🦔
Posted 12 years ago2011-06-11 19:41:07 UTC Post #295489
I suggest you better read some tutorials, instead of blindly trying to guess what's right in a certain situation. Because that kind of approach will get you nowhere. Also, eventually you will just get even more frustrated and angry at yourself.
Go here: http://www.cprogramming.com/tutorial.html
Posted 12 years ago2011-06-11 19:55:01 UTC Post #295490
The = NULL at the first line in Ninja's code is unnecessary as pc's value is overwritten on the next line.
Oskar Potatis Oskar Potatis🦔
Posted 12 years ago2011-06-11 19:55:36 UTC Post #295491
I think the best question here is what exactly are you trying to do with this.
Posted 12 years ago2011-06-11 20:10:23 UTC Post #295493
I just need to declare the 'pc' outside of that for loop, because the bottom if statement occurs outside this loop.
Basically I would like to have the same thing only that the pc isn't declared inside the for loop.

Anyway ninja defuse's method freezes the game when trying to chat. I guess this isn't a big deal since no one chats in single player, I think. If no one can solve this then I'll just go back to the original 'char pc;' and chat will be permanently disabled.
Posted 12 years ago2011-06-12 00:33:49 UTC Post #295496
The question is why do you want THAT.
Posted 12 years ago2011-06-12 01:52:12 UTC Post #295497
Pointers are a bitch. Why not just use array indexing?

I'm pretty rusty with C++ but you should get the idea:

bool found = false;
int len = strlen(p);

for (int i = 0; i < len; i++) {
    char c = p[i];
    if (isprint(c) && !isspace(c)) {
        found = true;
        break;
    }
}

if (!found) return;
It compiles to the exact same thing, but this is far easier to decipher. Of course, being C++, there's bound to be some reason why this doesn't work the way it logically should.
Penguinboy PenguinboyHaha, I died again!
Posted 12 years ago2011-06-12 05:58:38 UTC Post #295500
Enabling this option in your project options should solve all of Half-Life's loop variable problems.
Posted 12 years ago2011-06-12 11:54:22 UTC Post #295506
Pointers have their place but there's no point using them when you don't have to. They make code messier and generally make things more confusing than they need to be.

The 'char c' would get reused by the compiler during each loop as it is declared locally within the loop (if not, it's a simple matter of declaring it outside the loop). Using a boolean to indicate that the string is not blank instead of reusing a variable for a dual purpose (setting the pointer to NULL) makes the code easier to read and maintain.

Memory usage and performance in this case are non-issues, as well. It's more straight-forward to use strlen to determine string lenth before starting your for-loop, rather than checking the *c != 0 condition. You also would not call 'delete[] pc;' as that is a local-scope variable and was never declared on the heap using new[]. Since my variation of the function allocates no additional memory on the heap, there's no difference between the two.

p[i] compiles down to *(p+i) anyway, so the only result of using the different syntax is that it's easier to understand. Then again, I work with higher levels of abstraction than C, so maybe it's different for people who use C++ all day.

If this were C# you could do something like this instead:
if (!p.Any(c => IsPrint(c) && !IsSpace(c))) return;
if (String.IsNullOrWhitespace(p)) return;
Penguinboy PenguinboyHaha, I died again!
Posted 12 years ago2011-06-12 17:38:30 UTC Post #295508
Port Half-Life to C#.
Posted 12 years ago2011-06-12 18:09:59 UTC Post #295509
Read tutorials.
Posted 12 years ago2011-06-12 19:18:51 UTC Post #295511
I was thinking... if Half-Life is written in C++, then wouldn't it be easy to port it to Linux?
Striker StrikerI forgot to check the oil pressure
Posted 12 years ago2011-06-12 20:51:33 UTC Post #295512
Not all of Half-Life's source code is available, only the code for the DLLs. I doubt Valve's going to port it or release the code. Unfortunately.
Oskar Potatis Oskar Potatis🦔
Posted 12 years ago2011-06-13 00:32:08 UTC Post #295513
Gabe talked about making Goldsource totally open-source in a very recent podcast 17 interview.
http://www.podcast17.com/interviews/audio/gabe-newell/
Archie ArchieGoodbye Moonmen
Posted 12 years ago2011-06-13 03:05:04 UTC Post #295515
That was the question I submitted to their requests website :>
Disappointing answer, though :(
Penguinboy PenguinboyHaha, I died again!
Posted 12 years ago2011-06-13 14:33:34 UTC Post #295516
me and vs49688 have already helped u good luck i suggest dont change things like that, just leave it alone if it works fine
You must be logged in to post a response.