How to display (printf) selective parameter based on conditions
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I am programming for limit switches.
By default, value of limit switches is 0. When it is activated, the value will change to 1.
Let say I have 10 limit switches right now. I denoted them as following:
Switch 1 S1_
Switch 2 S2_
Switch 3 S3_
Switch 4 S4_
Switch 5 S5_
Switch 6 S6_
Switch 7 S7_
Switch 8 S8_
Switch 9 S9_
Switch 10 S10_
After the labeling is to show the status 0 or 1.
However, I only want the whole thing appear when that switch's status is 1.
I means that when Switch 1, 5 and 8 is activated, the display screen will only display as following:
S1_1, S5_1, S8_1
Currently, all the switches will appear no matter what is the status that is
S1_1, S2_0, S3_0, S4_0, S5_1, S6_0, S7_0, S8_1, S9_0, S10_0
Ok the below code is probably not perfect but it should give you an idea. That what you do is have you have an char** and an integer value. If you want to know wether a switch you do bitwise and. So first value is 2**0, second is 2**1 and so forth. If its set the bitwise and returns 1 therefore you can operate on it. I decided for this to format the string so I copied it into a new char** holding just the values I need. Then i could process it at the end making the code simpler and easier to reuse. That would be my advice anyway if you can do it as then you wouldn't have 20 variables to worry about
To pgpython, the code is really advanced. I cannot understand how it works.
To theNbomr, we tried to think to put the switches as array before. But if we would do so, we need to change many parts in many source code files and it would be rather much more works and easily miss out something. Thus, we used the most fundamental one.
In the function that is to display the result after checking the switches, we put all switches one by one and add a condition for its display.
An example of the code is as follow:
With regards to arrays. What will happen if you ever have to add an additional switch (or two or three or ...). All over the show you have to check the code and copy blocks of code. Forget one and the compiler will probably not warn you because the code is valid.
The simple example that you gave (modified to add a few more switches)
As you can see, I already forgot to change the second S1 to S2 in an if statement during a copy/paste operation.
Your code is simply not maintainable. So you have to suffer once to get it maintainable. How many source files are we talking about?