switch-statement

How to store a list of Cases from Switch Statement?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 02:38:40
问题 In my program I have a listbox that when the user double clicks an object it looks to a switch statement to see what event should occur. As the list begins getting larger I'm curious if there is a way to avoid having to maintain the list of objects in 2 places (once in a list to Add to the listbox, and once in the switch statement. Is there a way to index/read/store the various Cases of my switch statement, then add them as objects to my listbox? Example: (doesn't work, just a theory) Switch

getchar not working in switch case (c)

非 Y 不嫁゛ 提交于 2019-12-23 02:18:09
问题 Using a very simple calculator program that prompts a user for an operation to perform, followed by a prompt for two integers on which to perform this operation. The program is supposed to loop after these operations, except in the case where the user enters the character 'q', at which point the program is supposed to quit. #include <stdio.h> int main (void) { char c; int number[2], num1, num2, result; double num1d, num2d, resultd; int done=1; while(done) { printf("\t What sort of operation

Why does this code output 1?

妖精的绣舞 提交于 2019-12-23 01:52:28
问题 $t = true; switch($t) { case 1*2: echo 1; } 回答1: The manual for switch/case says: Note that switch/case does loose comparison. And true == 2 is true. 回答2: A switch statement does loose comparison between the tested expression and the expressions in the case labels to the type. In this case, this means that it the compiler determines if true == 2 . Since any nonzero integer compares equal to true , the branch is taken and echo 1; is executed. Here's a less intuitive example where the exact

Perform an action after the animation has finished

大兔子大兔子 提交于 2019-12-22 18:33:09
问题 I'm performing an animation on an image where I rotate it 180 degrees. Then I have inserted a code to switch to another view. However, the app is switching views before the animation ends. How can I wait for the animation to finish in order to switch view afterwards? Here's my code: - (IBAction)buttonPressed:(id)sender { CABasicAnimation *imageRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; imageRotation.fromValue = [NSNumber numberWithFloat:0]; imageRotation.toValue

switch statement for an std::pair?

僤鯓⒐⒋嵵緔 提交于 2019-12-22 18:03:21
问题 I want to switch over possible values of two integers, or in another case two bools. For the sake of discussion, suppose I've done auto mypair = std::make_pair(foo, bar); How can I achieve the equivalent of switch(mypair) { case make_pair(true, false): cout << "true and false"; break; case make_pair(false, true) cout << "false and true"; break; case default: cout << "something else"; } with C++11? (C++14/17 also relevant if that helps)? 回答1: C++'s switch statement doesn't have the pattern

switch case programming practice

自古美人都是妖i 提交于 2019-12-22 12:58:56
问题 enum SQLErrorCode{ OK = 0, PARTIAL_OK = 1, SOMEWHAT_OK = 2, NOT_OK = 3, }; Code 1: int error = getErrorCode(); if((error == SQLErrorCode.PARTIAL_OK) || (error == SQLErrorCode.SOMEWHAT_OK) || (error == SQLErrorCode.NOT_OK) || (error < 0)) callFunction1(); else callFunction2(); Code 2: switch(error){ case SQLErrorCode.PARTIAL_OK: callFunction1(); break; case SQLErrorCode.SOMEWHAT_OK: callFunction1(); break; case SQLErrorCode.NOT_OK: callFunction1(); break; default: callFunction2(); break; }

create an object in switch-case

北慕城南 提交于 2019-12-22 12:26:02
问题 i use visual studi 2008. (c++) in my switch case a wanted to create an object, but i doens't work. is it right, that i can't create an object in a switch case? if that's right,whats the best way to work around it, a new method that's creates that object? edit the code: switch (causwahl){ case '1': cAccount *oAccount = new cAccount (ID); case '2' .... 回答1: I can't say for sure with such a vague question, but I'm guessing that you're doing something like this: switch(foo) { case 1: MyObject bar

Getting the number of cases in a switch-case in C

落花浮王杯 提交于 2019-12-22 11:37:12
问题 Is it possible to get the number of cases in a switch case in C without manually adding a counter variable which is incremented in each case? 回答1: As I commented earlier, I think you want a dispatch table rather than a switch statement. Here's a little example. Say you got this: int find_the_case(); void do_something(); void do_something_different(); void do_something_completly_different(); void do_default(); int main(int argc, char *argv[]) { int c = find_the_case(); switch(c){ case 0: do

C# Switch Between Two Numbers?

♀尐吖头ヾ 提交于 2019-12-22 11:33:19
问题 I am trying to make an intelligent switch statement instead of using 20+ if statements. I tried this private int num; switch(num) { case 1-10: Return "number is 1 through 10" break; default: Return "number is not 1 through 10" } It says cases cannot fall through each other. Thanks for any help! 回答1: Your syntax for trying to do a range with switch/case is wrong. case 1 - 10: will be translated to case -9: There are two ways you can attempt to cover ranges (multiple values): List the cases

Why are C++ switch statements limited to constant expressions?

孤街醉人 提交于 2019-12-22 11:13:53
问题 I wanted to use macro functions in switch statements before I realized the statements need to be constant. Example (does not compile): #define BAND_FIELD1(B) (10 * B + 1) ... #define BAND_FIELD7(B) (10 * B + 7) int B = myField % 10; switch (myField) { case BAND_FIELD1(B): variable1[B] = 123; break; case BAND_FIELD7(B): variable7[B] = 321; break; ... } I rather had to use if .. else: if (myField == BAND_FIELD1(B) variable1[B] = 123; else if (myField == BAND_FIELD7(B) variable7[B] = 321; Why