Raised This Month: $51 Target: $400
 12% 

Mix the string in switch-case


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
gilmon
Senior Member
Join Date: Feb 2011
Location: China
Old 09-17-2014 , 05:10   Mix the string in switch-case
Reply With Quote #1

How to mix multi switch-case result print in one string?

For example:

PHP Code:
new String:item1[64];

for(new 
1;<= 3;i++)
{
   new 
result GetRandomInt(15)
   
   switch(
result)
  {
    case 
1:
   {
      
item1 "123";
   }
    case 
2:
   {
      
item1 "456";
   }
   case 
3:
   {
      
item1 "789";
   }
    case 
4:
   {
      
item1 "135";
   }
    case 
5:
   {
      
item1 "246";
   }
}

//Print the result 
I don't know the right way to do this, thanks.

Last edited by gilmon; 09-17-2014 at 05:10.
gilmon is offline
Send a message via MSN to gilmon
Drixevel
AlliedModders Donor
Join Date: Sep 2009
Location: Somewhere headbangin'
Old 09-17-2014 , 05:15   Re: Mix the string in switch-case
Reply With Quote #2

Code:
stock String_GetRandom(String:buffer[], size, length=32, const String:chrs[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234556789")
{
	new random, len;
	size--;
	
	if (chrs[0] != '\0') {
		len = strlen(chrs) - 1;
	}

	new n = 0;
	while (n < length && n < size) {

		if (chrs[0] == '\0') {
			random = Math_GetRandomInt(33, 126);
			buffer[n] = random;
		}
		else {
			random = Math_GetRandomInt(0, len);
			buffer[n] = chrs[random];
		}
		
		n++;
	}

	buffer[length] = '\0';
}

stock Math_GetRandomInt(min, max)
{
	new random = GetURandomInt();
	
	if (random == 0) {
		random++;
	}

	return RoundToCeil(float(random) / (float(SIZE_OF_INT) / float(max - min + 1))) + min - 1;
}
Taken from SMLIB.

Edit: Read your post again, what I posted isn't related but I'll leave it anyways.

Last edited by Drixevel; 09-17-2014 at 05:17.
Drixevel is offline
gilmon
Senior Member
Join Date: Feb 2011
Location: China
Old 09-17-2014 , 05:18   Re: Mix the string in switch-case
Reply With Quote #3

ok nvm i know how to do this now, thanks.

Last edited by gilmon; 09-17-2014 at 05:25.
gilmon is offline
Send a message via MSN to gilmon
gilmon
Senior Member
Join Date: Feb 2011
Location: China
Old 09-17-2014 , 22:04   Re: Mix the string in switch-case
Reply With Quote #4

Still have problem with it, don't know how to mix the result, i just understand how to do the random
gilmon is offline
Send a message via MSN to gilmon
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 09-18-2014 , 06:53   Re: Mix the string in switch-case
Reply With Quote #5

What exactly do you want to do?

1) You want to create and return random string
2) You already have a few strings and want to return random one

Last edited by KissLick; 09-18-2014 at 06:54.
KissLick is offline
gilmon
Senior Member
Join Date: Feb 2011
Location: China
Old 09-18-2014 , 11:45   Re: Mix the string in switch-case
Reply With Quote #6

Quote:
Originally Posted by KissLick View Post
What exactly do you want to do?

1) You want to create and return random string
2) You already have a few strings and want to return random one
All i need is like this:

Not show as
Code:
Result is "123"
Result is "456"
Result is "135"
I want
Code:
Result is "123,456,134"
Thanks.
gilmon is offline
Send a message via MSN to gilmon
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 09-18-2014 , 15:48   Re: Mix the string in switch-case
Reply With Quote #7

How random?

PHP Code:
new String:g_szData[4][5] = { "123""456""789""135""246" };

decl String:szBuffer[12];
FormatEx(szBuffersizeof(szBuffer), "%s,%s,%s"g_szData[GetRandomInt(04)], g_szData[GetRandomInt(04)], g_szData[GetRandomInt(04)); 
__________________

Last edited by 11530; 09-18-2014 at 15:48.
11530 is offline
gilmon
Senior Member
Join Date: Feb 2011
Location: China
Old 09-19-2014 , 02:53   Re: Mix the string in switch-case
Reply With Quote #8

Quote:
Originally Posted by 11530 View Post
How random?

PHP Code:
new String:g_szData[4][5] = { "123""456""789""135""246" };

decl String:szBuffer[12];
FormatEx(szBuffersizeof(szBuffer), "%s,%s,%s"g_szData[GetRandomInt(04)], g_szData[GetRandomInt(04)], g_szData[GetRandomInt(04)); 
Ok thanks, my primary problem is i need this function do the random, then do the function i need when difference result.

for example
Code:
result 123 do the function A
result 456 do the function B

and print the result to player.
this is why i use switch case.

Last edited by gilmon; 09-19-2014 at 02:54.
gilmon is offline
Send a message via MSN to gilmon
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 09-19-2014 , 20:31   Re: Mix the string in switch-case
Reply With Quote #9

Still unsure of what you want, but are we getting closer?

PHP Code:
new totalrand;
for (new 
03i++) //Get 3 numbers (3 different messages)
{
    
rand = (<< GetRandomInt(04)); //Chose Message A, B, C, D, or E
    
if (total rand == rand//We already have this one, try again
    
{
        
i--;
        continue;
    }
    
    
total += rand;
}

//Got 3 distinct numbers/messages
switch (total)
{
    case 
8//Print Messages C, D and E.
    
case 12//Print Messages B, D and E.
    
case 14//Print Messages B, C and E.
    
case 15//Print Messages B, C and D.
    
case 19//Print Messages A, D and E.
    
case 21//Print Messages A, C and E.
    
case 22//Print Messages A, C and D.
    
case 25//Print Messages A, B and E.
    
case 26//Print Messages A, B and D.
    
case 28//Print Messages A, B and C.

I feel I'm at that point where I'm so tired, I'm probably doing something stupid.
__________________
11530 is offline
gilmon
Senior Member
Join Date: Feb 2011
Location: China
Old 09-20-2014 , 06:51   Re: Mix the string in switch-case
Reply With Quote #10

Quote:
Originally Posted by 11530 View Post
Still unsure of what you want, but are we getting closer?

I feel I'm at that point where I'm so tired, I'm probably doing something stupid.
Sorry for my bad description, here's my code now.

PHP Code:
new String:pName[MAX_NAME_LENGTH];
GetClientName(clientpNamesizeof(pName));
decl String:addon[64];

for(new 
1<= 10++)
{
    new 
result GetRandomInt(110);
            
    switch(
result)
    {
        case 
1:
        {
            
addon "Fully Health Regen";
        }
        case 
2:
        {
            
addon "First Aid for all teammate";
        }
        case 
3:
        {
            
addon "Adrenaline";
        }
        case 
4:
        {
            
addon "Knife";
        }
        case 
5:
        {
            
addon "M16";
        }
        case 
6:
        {
            
addon "Ammo";
        }
        case 
7:
        {
            
addon "M16";
        }
        case 
8:
        {
            
addon "AK47";
        }
        default:
        {
            
addon "You got Nothing";
        }
    }
    
CPrintToChatAll("{blue}%s: {default} Get: {olive}%s{default}"pNameaddon);

This will flood too much sting like
Code:
xxx Get AK47
xxx Get Nothing
xxx Get M16
xxx Get Nothing
xxx Get Nothing
xxx Get Nothing
xxx Get Ammo
...
My request is i want all of then mix in one like:
Code:
xxx Get AK47, Nothing, M16, Nothing, Nothing, Nothing, Ammo ...
To stop the flood. my problem is don' know how to mix all then in one
gilmon is offline
Send a message via MSN to gilmon
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:42.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode