0

I don't know what is it that I am getting wrong here. Maybe a concept or two.My code proceeds with the rest of the block inside the while loop even when I am trying to redirect my default case to the switch statement when the user inputs any key other than A,S,D or W. The code written is as follows :

while(<condition>)
{
    char ch;
    switch(ch=getch())
    {
        case 'W' : case 'w' :
            ..event
            break;
        case 'S' : case 's' :
            ..event
            break;
        case 'A' : case 'a' :
            ..event
            break;
        case 'D' : case 'd' :
            ..event
            break;
        default :
            continue;
    }
   ...actions here with the condition in while using
}
fuz
  • 88,405
  • 25
  • 200
  • 352
Naman
  • 27,789
  • 26
  • 218
  • 353
  • 1
    What is the behavior you are getting? – wolfPack88 Mar 20 '15 at 15:30
  • 2
    Move the `...actions here...` into the default case, otherwise it will never execute (the default `continue` skips over it). Meaning it is only hit now if `w,s,a,d` executes when switch avoids the continue. – David C. Rankin Mar 20 '15 at 15:32
  • Please pick either C or C++. – fuz Mar 20 '15 at 15:35
  • Considering just a small variable in count ++; The counter increments for any other key press as well. – Naman Mar 20 '15 at 15:36
  • 2
    @FUZxxl why ? This question applies to both languages. – chmike Mar 20 '15 at 15:37
  • @chmike Because C and C++ are distinct languages. – fuz Mar 20 '15 at 15:37
  • 1
    @FUZxxl , but this is applicable to both c and c++ ( except for that `getch()` ) – Arun A S Mar 20 '15 at 15:38
  • @NamanNigam It would be helpful if you post a real compilable code in which you are getting this problem. – Arun A S Mar 20 '15 at 15:41
  • It looks like the `continue` applies to the `switch` and not the `while`, as for `break`. What compiler are you using @Naman Nigam ? – chmike Mar 20 '15 at 15:43
  • 1
    Related [Using continue in a switch statement](http://stackoverflow.com/questions/2146763/using-continue-in-a-switch-statement) – Arun A S Mar 20 '15 at 15:45
  • According to the answer referenced by @Arun A.S, the above code should work as expected. Note that `getch()` is not standard C. – chmike Mar 20 '15 at 15:51
  • The continue will for sure cause a jump to "while". The explanation must be somewhere else. Provide the whole code block. – Support Ukraine Mar 20 '15 at 15:57
  • This comment is not related to your problem, but do not use "char ch" when processing input character by character, use "int ch" instead. This allows to deal with the EOF value as distinct; to call the macros without problems; etc. – AntoineL Mar 20 '15 at 16:27
  • 1
    got it resolved,was missing a break somewhere in a long comment after 'default : ' – Naman Mar 20 '15 at 17:14
  • @FUZxxl : it was in C++ and the approach is general in both chmike : compiler independent,i have tried few – Naman Mar 20 '15 at 17:21
  • @AntoineL : the use of EOF is not reqd for me,but a point noted for sure. nielsen : thanx – Naman Mar 20 '15 at 17:22
  • @NamanNigam Ah, it's great that you managed to figure out what the problem is on your own. For the next time, please pick either C or C++. These two languages are not the same thing and “applies to both” is almost never the case. Furthermore, there are subtle differences in semantics between C and C++. – fuz Mar 20 '15 at 17:40

2 Answers2

0

If an event is handled each time you had a value of (W|S|A|D), then wouldn't you want to put a continue before each (W|S|A|D) break?

I think then, doing the above, the default would be empty, to keep to your current structure of having actions to do if nothing matches your (W|S|A|D).

mrflash818
  • 930
  • 13
  • 24
  • that is not what the requirement is,the (W|S|A|D) are the only console input i want to read and perform actions on for which I am kind of blocking the other keys during console input using the 'continue' in default – Naman Mar 24 '15 at 13:53
0

Thanks to all the comments, figured out that the problem was a break statement included within the default block for switch statement which was the reason for not continuing with the operations.

Naman
  • 27,789
  • 26
  • 218
  • 353