how can i make a switch case option disable after one use

Not really sure how to phrase this properly but what i have now is a case switch when a key is pressed, something happens, how would i be able to make it so after it has been pressed ones, that case gets disable?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
eg of what i have now:
if(abc = 1){ 
key gets press = c
switch (c)
{
case f1 : cout << 1; break;
case f2 : cout << 2; break;
case f3 : cout << 3; break;
....
}
}
=============
So after pressing f1 for the first time, it would print "1" out and then you wont be able to press it again. but you can still press f2 and f3.

is there something like

if(abc = 1){ 
key gets press = c
switch (c)
{
case f1 : cout << 1;(DISABLE CASE F1); break;
case f2 : cout << 2; break;
case f3 : cout << 3; break;
....
}
}


cheers
closed account (Dy7SLyTq)
im not going to lie, upon reading that i jumped to duffs device... god the nightmares. anyway, switches are very easy going and flexible. just do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool pressed = false;

switch(whatev)
{
     case foo:
          //stuff
          break;

     case bar:
          //stuff
          break;

     if(!pressed)
     {
          pressed = true;

          case pressedonce:
               //stuff
               break;
     }
}
You need to keep track of the pressed keys, say in a container C.

Every time the switch statement is reached, you can check whether the key is in C.

Edit: I would recommend that C be a std::set, as it only stores unique elements, and its find method is logarithmic in size (fast).
Last edited on
closed account (Dy7SLyTq)
You need to keep track of the pressed keys, say in a container C.

Every time the switch statement is reached, you can check whether the key is in C.


thats what a switch does. he wants to know how to check to see if its pressed once
A switch statement is a construct, not a container.

Edit: here is an illustration:

1
2
3
4
5
6
7
8
9
10
11
12
13
Container C;

switch (something)
{
    case one:
        if (one is not in C)
        {
            do something;
            add one to C;
        }
        break;
    case two: // ...
}
Last edited on
closed account (Dy7SLyTq)
you dont understand what hes asking at all. no one said anything about containers and i definitly didnt. the part i quoted is you describing what a switch does. if you would actually read the post, you would see that he wants something to happen when he presses c, and then if he presses it again, he doesnt want that thing to happen again. so my example shows how to do that.
Got it working, thanks for the fast responses guys!
closed account (Dy7SLyTq)
no problem
You did not read line 13 of his code.

You are welcome, pekingduck.
closed account (Dy7SLyTq)
actually yes i did. he wants the ability to press any key. then when f1 is pressed, he can still press any key but f1 wont do anything. you are wrong. he said nothing of needing a container
DTSCode wrote:
he said nothing of needing a container
And he said nothing about needing another variable. not to mention that your code simply don't work.

Using a container is one of the means to achieve desired result: track used options. You could use a bunch of boolean variables, mutable function pointers and other ways. To say that one of them is better is wrong. For beginners a bunch of booleans will be more intuitive. I could use a map of char and function pointer to get rid of switch completely or bitset, depending on situation.
closed account (Dy7SLyTq)
... i will try to explain this in a way you can understand... containers are used for holding lots of data. there are specialized versions yes, but their core function is to hold data. thats not what he asked to do. he said he wants to press the f1 key and something happens, and press it again but nothing will happen. he said nothing of trying to keep track of what was pressed, only to make sure that it could only be pressed once. of course you could get rid of the switch, but thats what he was using. your map idea only works if a) functions were going to be used, b) they have the same return type, and c) take the same number and type of args, and none of that is guaranteed from his code. of course my code doesnt work, it was pseudo code because if i write the code for him he doesnt work. if i used real values then it would work
...he said nothing of trying to keep track of what was pressed, only to make sure that it could only be pressed once...


Well, of course not. The whole basis of Q/A on a forum is "I have this end I wish to acheive, but I know not the means by which to do it."

This is the end:
make sure that it could only be pressed once


This is the means:
keep track of what was pressed


... i will try to explain this in a way you can understand...

Now, now. No need for all that.
of course my code doesnt work, it was pseudo code because if i write the code for him he doesnt work. if i used real values then it would work


No, it wouldn't. The code structure you used is not valid C++.
Topic archived. No new replies allowed.