Thread: Switch-case statement jumps to default instead of a case

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Argyle View Post
    Yes ,that's the problem.
    Unless you are using a comletely new compiler that no one else is using, I'm 99.99% sure that the problem is in your code, and not in the compiler. Switch statements are used quite often in code, so a compiler that isn't able to correctly compile switch statements would not get very far.

    Perhaps "wordToShort" isn't returning what you exect, or some such? Add a temporary variable, assign it with wordToShort, and switch on the variable - then modify the code to print the variable before the switch.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    Dino, if by non-working example, you mean what it outputs:
    when the default case is commented it outputs:

    In main chunk 0
    In edit chunk
    but when default case is uncommented it outputs:

    In main chunk 0
    In default
    Now, AFAIK the existance of default case in the code shouldn't affect the values of 'chunk'.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Argyle View Post
    Dino, if by non-working example, you mean what it outputs:
    when the default case is commented it outputs:



    but when default case is uncommented it outputs:



    Now, AFAIK the existance of default case in the code shouldn't affect the values of 'chunk'.
    What we're saying is: "it's a while loop, it's going to go through a bunch of different cases in a row." Are you not expecting wordToShort(chunk) to change, for some reason (such as, your file consists of the same two bytes over and over)?

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    My bad. I explained the problem badly . I don't mind the change in the 'chunk's value. It's just that the CHUNK_MAIN and CHUNK_EDIT cases should execute and not the CHUNK_MAIN and the default case. Things go well if there is no default case but as soon as the default case is added, it executes instead of the CHUNK_EDIT case.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Argyle View Post
    My bad. I explained the problem badly . I don't mind the change in the 'chunk's value. It's just that the CHUNK_MAIN and CHUNK_EDIT cases should execute and not the CHUNK_MAIN and the default case. Things go well if there is no default case but as soon as the default case is added, it executes instead of the CHUNK_EDIT case.
    What is the input and what do you get as output if you follow my suggestion?

    I'm still 99.99% sure that the problem is in your code, not the compilers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    It seems the file I was reading was different kind from what I thought it was. I'm sorry I wasted your time.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Argyle View Post
    It seems the file I was reading was different kind from what I thought it was. I'm sorry I wasted your time.
    Yes, that's another alternative...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 05:06 AM
  2. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  3. A simple array question
    By frenchfry164 in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2001, 04:13 PM
  4. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM