Message Edited by ernestsnaith on 05-03-200602:53 AM
You could also make an array of pointers to your functions, then use the error numbers as an index into the array.
However, this different just for the sake of being different. I would say that 9 out of 10 people would use a "switch."
I would
-Tomahawk
Message Edited by ernestsnaith on 05-03-200612:57 PM
ernestsnaith wrote:
I app ologise for how basic these questions are but am i understanding 'Switch' correctly? I have a number 0-10 in Thno, should it branch to corresponding ToothX?
switch (Thno) {
case 0: Error(); break;
case 1: Tooth1(); break;
case 2: Tooth2(); break;
case 3: Tooth3(); break;
case 4: Tooth4(); break;
case 5: Tooth5(); break;
case 6: Tooth6(); break;
case 7: Tooth7(); break;
case 8: Tooth8(); break;
case 9: Tooth9(); break;
case 10: Error(); break;
}This is what first put me off switch, I have lots of error such asimplicit parameter declaration for tooth1
default: Error();
as Peg suggested. This will pick up any value ot Thno other than 1 through 9.
Each function that is called will need to have a prototype declared prior to its first use. I suspect this may be the reason for your compile errors. The prototypes might take the form -
void Tooth1(void);
void Tooth2(void);
etc.
Normally these would be part of a header file, and the header file included near the beginning of the main file.
Regards,
Mac
I think Peg did mean something like this:
switch (scenario) {
case 0: func0(); break;
case 1: func1(); break;
...
}
An other way to do it would be using a array of pointers to functions:
void (*apf[10])(void) = {func0, func1, func2...};
(*apf[scenario])();
In both cases you should write the code for the different scenarios in the functions func0..func10.
An excellent 'C dictionary' book would be 'C, A Reference Manual' by Samuel P. Harbison and Guy L. Steele Jr.
Can you place a lot of code within a switch though? Ive only just started with C but i thought it just picked the closest value and returned it.
What i am after is a goto with a parameter if this is possible or wise. Is the following possible at all
goto ("Function" + ScenNo)
Message Edited by ernestsnaith on 05-03-200604:22 AM
Hi,
You can put in as much code as you please, but if you want to keep it readable, normally just a couple of function calls.
e.g.
switch (scenario){
case scenario1:
func_scen_1();
break;
case scenario2:
func_scen _2();
break;
default:
panic(); //there is more scenarios than I bargained on!
}
But also remember Rule No. 1
1. Avoid the use of goto!
only use it in phrases like "goto the fridge for a beer" and "goto the beach" etc.
Regards Peg
Hi,
How about "switch".
Regards Peg