Switch-case statement jumps to default instead of a case

This is a discussion on Switch-case statement jumps to default instead of a case within the C++ Programming forums, part of the General Programming Boards category; For some reason the following code jumps from the first case to the default case : Code: while(file.read(chunk,2)) { switch(wordToshort(chunk)) ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    17

    Switch-case statement jumps to default instead of a case

    For some reason the following code jumps from the first case to the default case :
    Code:
        while(file.read(chunk,2))
        {
            switch(wordToshort(chunk))
            {
                case CHUNK_MAIN:
    
                    cout<<"In main chunk "<< (int(file.tellg())-2) <<endl;
                    success=true;
                    file.read(length,4);
                    break;
                case CHUNK_EDIT:
    
                    cout<<"In edit chunk"<< (int(file.tellg())-2) <<endl;
    
                    file.read(length,4);
                    break;
                case CHUNK_TRIMESH_VERTS:
    
                    cout<<"In object chunk"<<(int(file.tellg())-2) <<endl;
                    file.read(length,4);
                    break;
                default:
                    cout<<"In default"<<endl;
                    file.read(length,4);
    
                    file.seekg(wordToshort(length),ios::beg);
                    break;
            }
        }
    This jumps from CHUNK_MAIN case to the default case ,although this code:
    Code:
            switch(wordToshort(chunk))
            {
                case CHUNK_MAIN:
    
                    cout<<"In main chunk "<< (int(file.tellg())-2) <<endl;
                    success=true;
                    file.read(length,4);
                    break;
                case CHUNK_EDIT:
    
                    cout<<"In edit chunk"<< (int(file.tellg())-2) <<endl;
    
                    file.read(length,4);
                    break;
                case CHUNK_TRIMESH_VERTS:
    
                    cout<<"In object chunk"<<(int(file.tellg())-2) <<endl;
                    file.read(length,4);
                    break;
            }
        }
    jumps from CHUNK_MAIN case to the CHUNK_EDIT case. Is this an error related to C++ syntax or is this possible something else?

    cheers

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    You're saying it does this without going through another
    while(file.read(chunk,2))
    ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    Yes ,that's the problem.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Did you put in a cout message after the while and before the switch to verify the while loop was not occurring twice? How do you know? Can you post a non-working complete example?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    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.

  6. #6
    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'.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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)?

  8. #8
    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.

  9. #9
    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.

  10. #10
    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.

  11. #11
    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.

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    There you go - you've found a complete non-working example.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

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, 04:06 AM
  2. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 09:42 AM
  3. A simple array question
    By frenchfry164 in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2001, 03: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, 03:49 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM

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