View previous topic :: View next topic |
Author |
Message |
JohnKennedy
Joined: 12 May 2004 Posts: 38
|
help with switch statement |
Posted: Mon Jul 18, 2005 9:08 am |
|
|
Hi
I need to define some constants and the value of these constants depends on the voltage on my A/D pin, so I thought use a switch statement but I can't seem to get it right, I have the following
Code: |
switch (select)
{
case <68:
#define vmin 52
#define vconnect 104
}
...14 more cases hear
|
If I remove the '<' symbol it compiles fine.
None of the examples in the manual show using the select statement if the case value is in a range but I'm sure I have seen this being used before.
thanks
JFK |
|
![](templates/subSilver/images/spacer.gif) |
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Mon Jul 18, 2005 9:32 am |
|
|
Wrong language and code does not make sense. In C this has to be a constant or multiple statements such as :
Code: |
switch (select)
{
case 65:
case 68:
// do something here
break;
} |
Also the #define is used by the pre-processor during compilation. But you are trying the do this based on run time code execution. Instead you need to be using variables _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
![](templates/subSilver/images/spacer.gif) |
JohnKennedy
Joined: 12 May 2004 Posts: 38
|
|
Posted: Mon Jul 18, 2005 9:41 am |
|
|
Ok
That might have been in another lifetime when I was doing something in Visual Basic, guess the only way to achieve what I want is multiple IF statements.
Thanks for pointing out the error on the defines, though curiously it did compile without even a warning, though I hadn't tested it to see if it worked. The defines were defined before being used by the main body of the program so maybe thats why it compiled ok.
Thanks again
JFK |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Tue Jul 19, 2005 2:23 am |
|
|
There will be no syntax error on the defines. It is perfectly acceptable to put them anywhere in the code, _but_ they will all be executed before the program runs. Hence the compiler won't complain.
In C++ (but not in C), the switch statement can be used with 'ranges', and it may be this version you have seen.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|