Chaos Project

Game Development => Sea of Code => Topic started by: nathmatt on August 01, 2011, 02:30:10 pm

Title: [c] non constant int
Post by: nathmatt on August 01, 2011, 02:30:10 pm
how do i return a non constant int when i just use int it gives me an error  case label does not reduce to an integer constant  here is the portion of the code im have issues with

DLLIMPORT int code (const char s)
{
   switch ( s )
   {
   case "string1":
   return 1;
   break;
   case "string2":
   return 2;
   break;
   default:
   return 0;
   break;
   }
}
Title: Re: [c] non constant int
Post by: Blizzard on August 01, 2011, 03:09:32 pm
If you mean something like a local variable, you just have to declare it first.

int result = 0;
/* your code here */
return result;
Title: Re: [c] non constant int
Post by: nathmatt on August 01, 2011, 03:44:14 pm
im trying to send it a string and have it return a number back using the switch statement but since the integer is non constant then it gives me an error

edit i just figured out my problem the switch statement only works on integers and i was trying to use it on a string will just use the if statement then 
Title: Re: [c] non constant int
Post by: Blizzard on August 01, 2011, 04:46:56 pm
Oh, right. ._. I forgot that, too. ._.;
Title: Re: [c] non constant int
Post by: nathmatt on August 01, 2011, 04:51:22 pm
ok now my dll will not accept a parameter here is the method

DLLIMPORT int code (char s[20])
{
   if (s == "string1"){return 1;}
   if (s == "string2"){return 2;}
   return 0;
}


and im calling it like this

s = Win32API.new('Project1','code','S','I')
s.call('string1')
Title: Re: [c] non constant int
Post by: Cremno on August 01, 2011, 05:18:22 pm
To compare two strings in C you have to use strcmp (string.h) or a similiar function. It returns zero when both strings are identical.
DLLIMPORT int code (char s[]) {
if (!strcmp(s, "string1")) return 1;
if (!strcmp(s, "string2")) return 2;
return 0;
}

Notice the removed 20. Strings in C aren't easy to handle as in other languages. And are you sure about the 'S'? If you use the RGSS it has to be 'P'.
Title: Re: [c] non constant int
Post by: nathmatt on August 01, 2011, 05:24:52 pm
it was p thx

edit it fully works now thx