-
June 18th, 2009, 01:11 PM
#1
Adding switches to function
Hey again...
This time I want to make the function paramaters operate a switch from a function variable, how is this done?
eg
Code:
int 123(string 456)
{
switch(456)
case 'command1': cout << timeStr << ": command1"; break;
case 'command2': cout << timeStr << ": command2"; break;
default: cout << "invalid input."; break;
}
The output need to be like this...
I have a variable called timeStr which needs to be before the function paramater.
[variable]: "whatever was in the function paramater".
Thanks
-
June 18th, 2009, 01:16 PM
#2
Re: Adding switches to function
switch only works with numeric expressions.
-
June 18th, 2009, 01:20 PM
#3
Re: Adding switches to function
oh ok thanks, is there a way to use the string and compare it to a variable?
so it compares the "456" string and if it is command1 then number 1. If it is command2 then it would interpret it as number 2.
Code:
int 123(string 456)
{
if 456 = ("command1")
operatorForSwitch = 1;
if 456 = ("command2")
operatorForSwitch = 2;
switch(456)
case 'command1': cout << timeStr << ": command1"; break;
case 'command2': cout << timeStr << ": command2"; break;
default: cout << "invalid input."; break;
}
-
June 18th, 2009, 01:25 PM
#4
Re: Adding switches to function
Giving your string variable a numeric name is confusing. Why don't you use real compilable syntax to show what you're trying to do, or at least unambiguous pseudocode.
You could use the if statements to come up with a numeric value for your switch, but what's the point? As long as you're evaluating the value in your string with if, you may as well use those statements to execute the appropriate code.
-
June 18th, 2009, 01:27 PM
#5
Re: Adding switches to function
Okay, I was only using switchs becuase that's what I learnt instead of multiple if statements
Thanks.
-
June 18th, 2009, 01:28 PM
#6
Re: Adding switches to function
Variable, function and class names in C++ must start with a letter.
There are really a lot of basic syntax errors in your code. You should study a beginners' C++ book. See the FAQ for recommendations.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
June 18th, 2009, 01:40 PM
#7
Re: Adding switches to function
oh, The 123 and 456 where just like xyz, not actual filenames but examples :P
-
June 18th, 2009, 01:58 PM
#8
Re: Adding switches to function
You want something like this
Code:
//This macro is easier to understand than negating a compare function
#define AreSame(p1, p2)\
!p1.compare(p2)
if (AreSame(xyz, "SomeValue")){
do something
} else if (AreSame(xyz, "Some Other Value")){
do something else
}...
-
June 18th, 2009, 01:59 PM
#9
Re: Adding switches to function
Originally Posted by ninja9578
You want something like this
Code:
//This macro is easier to understand than negating a compare function
#define AreSame(p1, p2)\
!p1.compare(p2)
if (AreSame(xyz, "SomeValue")){
do something
} else if (AreSame(xyz, "Some Other Value")){
do something else
}...
What's wrong with just using == to compare the string to the literal?
-
June 18th, 2009, 02:00 PM
#10
Re: Adding switches to function
ok.
Is AreSame a real function and what does this do?
!p1.compare(p2)?
-
June 18th, 2009, 02:05 PM
#11
Re: Adding switches to function
Originally Posted by CatFace
Is AreSame a real function and what does this do?
No, it is a macro. It basically replaces AreSame(x, y) for some expressions x, y with !x.compare(y).
Originally Posted by CatFace
!p1.compare(p2)?
That is effectively the same as p1.compare(p2) == 0, but the latter expresses the idea that this is a test of equality better, thus addressing ninja9578's objection. However, even better yet would be: p1 == p2, e.g., xyz == "SomeValue".
Last edited by laserlight; June 18th, 2009 at 02:15 PM.
-
June 19th, 2009, 01:29 AM
#12
Re: Adding switches to function
Have a look at this, it uses strcmp the syntax is
Code:
int strcmp ( const char * str1, const char * str2 );
so u can have something like
Code:
#include stdio.h
#include string.h
...
...
if (strcmp(string1, string2) = = 0) //both sting are equal
do something;
else if (strcmp(string1, string3) < 0) //1st string < than second
do something else;
else do another thing;
strcmp in my opinion is better to use for comparing strings, or array of char.
If you must use a switch, I believe you can only switch int or a char. ie 1, 2, 40, 'a' , ' n', 'M'....
Last edited by servalsoft; June 19th, 2009 at 01:51 AM.
...Don't be afraid, only believe.
-
June 19th, 2009, 04:02 AM
#13
Re: Adding switches to function
Originally Posted by servalsoft
strcmp in my opinion is better to use for comparing strings, or array of char.
strcmp() would be appropriate for comparing null terminated strings, but I hope and believe that CatFace is using std::string objects.
-
June 19th, 2009, 08:58 AM
#14
Re: Adding switches to function
If the string is treated as command (as in shell), you can write a function which takes string (the command), and return numeric constant for the command. Like:
Code:
int GetCommandId(const string& cmd)
{
if(cmd=="list")
return 0;
if(cmd=="delete")
return 1;
if(cmd=="find")
return 2;
....
}
Then use the return value of 'GetCommandId' in switch:
Code:
switch(GetCommandId(str))
{
case 1:
...
}
You may also use #define or enum constants for better readability.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|