How to display (printf) selective parameter based on conditions
Hi,
I am working on messaging quite a large amount of parameters. I would like to simplify it and only display those parameters that are in active status. For example, I have 10 switches. (A1, A2, A3, ... ..., A9, A10) In the message, I only display the label of the switches that are active and its status code (0-inactive, 1-active) e.g. If only switch 1 and switch 8 is active. The message I would like to display is as following: A1_1, A8_1 The other will not appear until they are activated. How can I do this in the function that is specially created to display these parameters. Other function will pass the parameter value to this function. Currently, I only manage to display all the parameters which is a large amount of parameters and is not good to display all. Thank you in advance. |
So, which lang is this? Also, please show us the relevant code you've done so far.
|
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 My example display code for this is strcat(buffer,",S3_"); sprintf(cS,"%d",S3); strcat(buffer,cS); and this is repeated for every switch. I thought of using switch and case. However, I am stucked halfway. |
Personally i feel have 10 switches and 10 variables makes it more complicated than it needs to be.
my advice would be instead have 2 variables: one a switch to be bit wised and'ed on. the other either a *char[] or **char depending on what you need Then you can loop through find out if a switch is set by doing bitwise and, then concatenate the string if it is set. Is there any particular reason why you can't do this |
I am still too new to Linux
I do not know how to do advanced code. |
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
Code:
char* joinString(int size, char **data, char delimiter[]){ |
Use an array of switches:
Code:
unsigned char swtich[10]; Code:
#define CLOSED 0 --- rod. |
Thank you all.
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: if (S1==1) { strcat(buffer,",S1_"); sprintf(cS,"%d",S1); strcat(buffer,cS); } and repeat for all others. Anyway, thanks for all your help. |
I would urge you to at least adopt the idea of the #defines that theNbomr had in the sample code, that is:
Code:
#define INACTIVE 0 Code:
if (S1==ACTIVE) |
Code:
#include <stdio.h> Note: it's better to use snprintf to prevent buffer overflows |
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) Code:
if (S1==1) Your code is simply not maintainable. So you have to suffer once to get it maintainable. How many source files are we talking about? |
All times are GMT -5. The time now is 03:32 PM. |