Page 1 of 1

Problems with ifs

PostPosted: Fri Mar 21, 2008 8:39 pm
by regine
Code: Select all
if{code.text == code2.text}
{
    strcpy(code.text, "OK");
}


Whats false in this code?

Re: Problems with ifs

PostPosted: Fri Mar 21, 2008 9:08 pm
by edh
You cannot compare arrays of characters with ==, if you try and they are not the same array, then that is false.

for example,
Code: Select all
char [] one = "a\0";
char [] two = "a\0";

if (one == two)
{
 // the address is the same
}

if (one == one)
{
 // the address is the same
}

if (strcmp(one, two) == 0)
{
  // the values are the same
}




You may be asking "why the f do I have to use strcmp(x, y) == 0?

Because strcmp is a lexical comparison. If x is < y then you will get a negative result from strcmp(x, y), zero for equality and a positive number for y > x.

Re: Problems with ifs

PostPosted: Fri Mar 21, 2008 9:11 pm
by regine
THX I will try if it is working.

€dit: Declaration Error...

Re: Problems with ifs

PostPosted: Fri Mar 21, 2008 10:40 pm
by edh
I wrote it quickly. I don't program in C normally, so I make a simple mistake.
You could change it to

Code: Select all
char * one = "a\0";


or

Code: Select all
char one[] = "a\0";


Your choice.

Re: Problems with ifs

PostPosted: Fri Mar 21, 2008 10:57 pm
by regine
With one it says Redeclaration of parameter one but when i use two and three it is working. Is that a problem?

€dit: I started the game and it closed itself. Then i reopened GE and renentered the code and it was working.
€dit2: I want to write a copy protection with this. I already wrote a tool that can save license keys with that you can start the game. Is it compatible with this?

I mean iam doing something like this:

Starting the tool -> Writing license keys -> saving them -> start the copy protection of the game -> it loads the codes -> if the user enters one of these codes the game is activated

Is there any way to do that with this code?