Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

selection using switch?

Status
Not open for further replies.

vintl

Technical User
Jun 9, 2000
72
0
0
MY
Hi,
I want to do a selection of options, but have limitation on the selected options.
FOr examples,
---------------
List of options
---------------
* All
Select1
Select2

If All is selected (* denote selected), then Select1 and the rest is bar from being selected, unless All is unselect, and vice versa, if any of the Select1 options is selected, then All is not selectable.
Should i use Switch/Case stmt? or is there any way i can limit the selections? any examples for me?
thanks!
 
I don't think this implements all your rules, but should give you some idea of what to do
Code:
#include <stdio.h>
#include <string.h>

typedef enum { false, true } t_bool;

typedef struct {
    char    *name;
    t_bool   selected;
} menu_st;


void update_menu ( menu_st *m, int nelems, int selection ) {
    int i;
    if ( selection == 0 ) {
        /* special handling of the all */
        if ( !m[0].selected ) {
            for ( i = 1 ; i < nelems ; i++ ) {
                /* turn everything else off */
                m[i].selected = false;
            }
        }
        m[0].selected ^= 1;     /* toggle state */
    } else {
        if ( !m[0].selected ) {
            /* all is off, toggle selection */
            m[selection].selected ^= 1;
        }
    }
}
void print_menu ( menu_st *m, int nelems ) {
    int i;
    for ( i = 0 ; i < nelems ; i++ ) {
        printf( &quot;%c %s\n&quot;, m[i].selected ? '*' : ' ', m[i].name );
    }
    printf( &quot;\n&quot; );
}

int main ( ) {
    menu_st menu[] = {
        { &quot;All&quot;, 0 },
        { &quot;File&quot;, 0 },
        { &quot;Edit&quot;, 0 },
    };
    int menu_size = sizeof(menu)/sizeof(*menu);

    print_menu( menu, menu_size );
    update_menu ( menu, menu_size, 0 ); /* All ON */
    print_menu( menu, menu_size );
    update_menu ( menu, menu_size, 1 ); /* ignored, all is ON */
    print_menu( menu, menu_size );
    update_menu ( menu, menu_size, 0 ); /* All OFF */
    update_menu ( menu, menu_size, 1 ); /* File ON */
    update_menu ( menu, menu_size, 2 ); /* Edit ON */
    print_menu( menu, menu_size );
    update_menu ( menu, menu_size, 0 ); /* All ON, turns others OFF */
    print_menu( menu, menu_size );

	return 0;
}

Remember, this is just menu selection. What you do after pressing &quot;OK&quot; is up to you.
 
Hi
I do not understand how this stmt works -> m[0].selected ^= 1;

^= is assignment operator? how does it work?

This is my version:
//if selected is all, denote by 'S'
if(p_SELORD->selflg[0][0] == 'S') {
//loop the rest and set to empty
for(i=0; i<oputl_get_arycnt(tx212o->arycnt1)+1; i++) {
p_SELORD->selflg[0] = ' ';
}
} else {
//else the rest is selected, then set All to not selected
p_SELORD->selflg[0][0] = ' ';
}

Now, this code works, when i selected All, then i select the others; the 'S' on ALL will automatically disappeared. But then when i select All again, 'S' on the others will not automatically disappeared, unless i select All again.
Why is this so?
 
> I do not understand how this stmt works -> m[0].selected ^= 1;
It changes the state of a boolean variable
False becomes true
True becomes false

> Why is this so?
I said this
&quot;I don't think this implements all your rules, but should give you some idea of what to do&quot;

In particular, this requirement is not covered
&quot;if any of the Select1 options is selected, then All is not selectable.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top