Thread: Trying to add a little extra to a "Switch Statement" assignment.

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    70

    Trying to add a little extra to a "Switch Statement" assignment.

    I pretty much got the assignment done. All it asked for was that you make a C program that displays which manufacturer owned a disk drive based on the code entered. Pretty simple. Just enter 1, 2, 3, or 4 and get the associated manufacturer. Though I am trying to implement an error messege to it for any interger that isn't 1-4.

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    int code;
    
    printf("Please enter the Disk Drives code: ");
    scanf("%d", &code);
    
    switch (code)
    {
    
        case 1:
            printf("The code entered was %d and the associated manufacturer is 3M Corporation", code);
            break;
        case 2:
            printf("The code entered was %d and the associated manufacturer is Maxwell Corporation", code);
            break;
        case 3:
            printf("The code entered was %d and the associated manufacturer is Sony Corporation", code);
            break;
        case 4:
            if (code = 4)
                printf("The code entered was %d and the associated manufacturer is Verbatim Corporation", code);
            else
                printf("The code entered was %d and is not related to any corporation.", code);
            break;
    }
    
    return 0;
    }
    I also tried this, but it didn't work either.

    Code:
        case 4:
            if (code < 1 || code > 4)
                printf("The code entered was %d and is not related to any corporation.", code);
            else
                printf("The code entered was %d and the associated manufacturer is Sony Corporation", code);
            break;
    }
    No errors are given and it works fine as long as you enter 1-4, but when you enter any other didgit it just stops the program without any messeges.

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For case 4, it means that code == 4, since there is no fall through from earlier cases. Therefore, it must be the case that (code < 1 || code > 4) evaluates to false. Note that (code = 4) in your earlier code snippet is wrong because it assigns to code. Changing it to (code == 4) would be better, but redundant because we already know that at that point.

    Quote Originally Posted by Cameron Taylor
    No errors are given and it works fine as long as you enter 1-4, but when you enter any other didgit it just stops the program without any messeges.
    There is no case to handle such a value for code. Perhaps you should have a default case.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Ahh, didn't think about using the default case. The ex. programs didn't use default at all lol. I had to dig back through the chapter to see it mentioned. lol

    using the default case worked though 100%. Thank you!

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    int code;
    
    printf("Please enter the Disk Drives code: ");
    scanf("%d", &code);
    
    switch (code)
    {
    
        case 1:
            printf("The code entered was %d and the associated manufacturer is 3M Corporation", code);
            break;
        case 2:
            printf("The code entered was %d and the associated manufacturer is Maxwell Corporation", code);
            break;
        case 3:
            printf("The code entered was %d and the associated manufacturer is Sony Corporation", code);
            break;
        case 4:
            printf("The code entered was %d and the associated manufacturer is Verbatim Corporation", code);
            break;
        default:
            printf("The code entered was %d and is not related to any corporation.", code);
            break;
    }
    
    return 0;
    }
    Had to use this to see an example of it. Pretty ironic.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    This is my favorite way to handle such tasks.
    Code:
    static const char *mfg[] = {
        "(none)", "3M Corporation", "Maxwell Corporation",
        "Sony Corporation", "Verbatim Corporation"
    };
    
    printf("The code entered was %d and the associated manufacturer is %s\n",
           code, code < sizeof(mfg)/sizeof(mfg[0]) ? mfg[code] : mfg[0]);
    BTW, I would declare code to be unsigned to avoid a problem with a negative number.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by KenJackson View Post
    This is my favorite way to handle such tasks.
    I guess the assignment's purpose was to practice the switch statement ;-).

    Bye, Andreas

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by KenJackson View Post

    BTW, I would declare code to be unsigned to avoid a problem with a negative number.
    In this case you will have to use %u instead of %d
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Extra tokens at end of #ifndef directive"
    By jerimo in forum C++ Programming
    Replies: 9
    Last Post: 12-17-2012, 11:41 AM
  2. Question about the "switch" statement
    By SensualCake in forum C# Programming
    Replies: 2
    Last Post: 11-29-2010, 08:44 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. help writing "switch" statement
    By 3MGFX in forum C Programming
    Replies: 7
    Last Post: 10-11-2007, 09:53 PM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM