#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Karachi, Pakistan
    Posts
    16
    Rep Power
    0

    Multiple Choice Code Help Needed


    Okay, I'm going to ask that other question I had in this new thread, then.

    I updated the source code to this:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int letter;
    
        int 'A';
        int 'B';
        int 'C';
    
        'A' = A;
        'B' = B;
        'C' = C;
    
        printf("Enter the error code (A-C): ");
        scanf("%c",&letter);
    
        switch(letter)
        {
            case 'A':
                puts("Drive Fault, not your fault.");
                break;
            case 'B':
                puts("Illegal format, call a lawyer.");
                break;
            case 'C':
                puts("Bad filename, spank it.");
                break;
            default:
                puts("That's not A, B, or C.");
        }
        return(0);
    }
    These are the error and warning messages I got:
    C:\Users\Osman\Documents\BegC4D\ex0817\main.c||In function 'main':|
    C:\Users\Osman\Documents\BegC4D\ex0817\main.c|7|error: expected identifier or '(' before 'A'|
    C:\Users\Osman\Documents\BegC4D\ex0817\main.c|8|error: expected identifier or '(' before 'B'|
    C:\Users\Osman\Documents\BegC4D\ex0817\main.c|9|error: expected identifier or '(' before 'C'|
    C:\Users\Osman\Documents\BegC4D\ex0817\main.c|11|error: 'A' undeclared (first use in this function)|
    C:\Users\Osman\Documents\BegC4D\ex0817\main.c|11|note: each undeclared identifier is reported only once for each function it appears in|
    C:\Users\Osman\Documents\BegC4D\ex0817\main.c|12|error: 'B' undeclared (first use in this function)|
    C:\Users\Osman\Documents\BegC4D\ex0817\main.c|13|error: 'C' undeclared (first use in this function)|
    C:\Users\Osman\Documents\BegC4D\ex0817\main.c|16|warning: format '%c' expects argument of type 'char *', but argument 2 has type 'int *' [-Wformat]|
    ||=== Build finished: 6 errors, 1 warnings (0 minutes, 0 seconds) ===|
    Jet tell me what I'm doing wrong here.
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,644
    Rep Power
    1945
    Remove the ' ' around the variable names A, B and C. (Wonder where you got that from).

    This part can be removed entirely:
    Code:
       'A' = A;
        'B' = B;
        'C' = C;
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Karachi, Pakistan
    Posts
    16
    Rep Power
    0
    Okay, I took out the three lines you specified, but I've got some problems. I changed the int into char.

    I've got this now, due to having done above-mentioned change:

    Code:
    #include <stdio.h>
    
    int main()
    {
        char letter;
    
        char A;
        char B;
        char C;
    
        printf("Enter the error code (A-C): ");
        scanf("%c",&letter);
    
        switch(letter)
        {
            case A:
                puts("Drive Fault, not your fault.");
                break;
            case B:
                puts("Illegal format, call a lawyer.");
                break;
            case C:
                puts("Bad filename, spank it.");
                break;
            default:
                puts("That's not A, B, or C.");
        }
        return(0);
    }
    These are the errors and warnings I'm getting:

    C:\Users\Osman\Documents\BegC4D\ex0817\main.c||In function 'main':|
    C:\Users\Osman\Documents\BegC4D\ex0817\main.c|16|error: case label does not reduce to an integer constant|
    C:\Users\Osman\Documents\BegC4D\ex0817\main.c|19|error: case label does not reduce to an integer constant|
    C:\Users\Osman\Documents\BegC4D\ex0817\main.c|22|error: case label does not reduce to an integer constant|
    C:\Users\Osman\Documents\BegC4D\ex0817\main.c|9|warning: variable 'C' set but not used [-Wunused-but-set-variable]|
    C:\Users\Osman\Documents\BegC4D\ex0817\main.c|8|warning: variable 'B' set but not used [-Wunused-but-set-variable]|
    C:\Users\Osman\Documents\BegC4D\ex0817\main.c|7|warning: variable 'A' set but not used [-Wunused-but-set-variable]|
    ||=== Build finished: 3 errors, 3 warnings (0 minutes, 0 seconds) ===|
    I'm declaring and using the variables, but it's saying I'm not using them. How do I fix that? I'm going to move on from this for now, and come back later. I'll just ask you guys to just help me out with this because I'll definitely try to come back to this later.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    1
    Originally Posted by DragonOsman2
    I'm declaring and using the variables, but it's saying I'm not using them. How do I fix that?
    Yes, you declare variable A, B and C for what purpose? Check the error message that you are not paying attention to.

    The Variable A - C that you declared are not used at all. In case you are thinking that is what you are using as the case label.

    The case label should be
    Code:
    case 'A':
    not
    Code:
    case A:
    Note the single quote.

    Then, loose all the
    Code:
        char A;
        char B;
        char C;
    They are not needed as far as I can see here.
    That should do.
  8. #5
  9. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,644
    Rep Power
    1945
    Using a variable should not have ' '.

    In the case statement you validate the content/text of the variable letter. This should be encapsulated with ' ' as you had in your first post. (case 'A':)

    As your code is now, you only need the variable letter.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Karachi, Pakistan
    Posts
    16
    Rep Power
    0
    @2teez: It's working now, after I did my last edit to it. I'm still got an "unused variable" warning, but other than that, the code works fine.

    Code:
    #include <stdio.h>
    
    int main()
    {
        char letter;
    
        char A, B, C;
    
        A = 'A';
        B = 'B';
        C = 'C';
    
        printf("Enter the error code (A-C): ");
        scanf("%c",&letter);
    
        switch(letter)
        {
            case 'A':
                puts("Drive Fault, not your fault.");
                break;
            case 'B':
                puts("Illegal format, call a lawyer.");
                break;
            case 'C':
                puts("Bad filename, spank it.");
                break;
            default:
                puts("That's not A, B, or C.");
        }
        return(0);
    }
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    30
    Rep Power
    1
    Originally Posted by DragonOsman2
    @2teez: It's working now, after I did my last edit to it.
    Good for you!! :)

    I'm still got an "unused variable" warning, but other than that, the code works fine.
    What are these doing?
    Code:
        char A, B, C;
    
        A = 'A';
        B = 'B';
        C = 'C';
    Until you are able to answer that, then you will stop having the warning message.
  14. #8
  15. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    These lines in your code:

    Code:
        char A, B, C;
    
        A = 'A';
        B = 'B';
        C = 'C';
    Serve absolutely no purpose whatsoever. Follow your code and see that they are redundant. THe character you enter is assigned to the variable "letter", and that variable is compared directly with the literal constants 'A', 'B' and 'C' in the switch-case, nowhere are the variables A, B or C used.

    You should set your compiler settings to maximise the warning level, in which case you will probably get a "variable A was assigned but never used" warning or similar for example.

    What compiler are you using? In GCC the options you need are "-Wall -Wformat -Werror", in VC++ "\W4 \WX"
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Karachi, Pakistan
    Posts
    16
    Rep Power
    0
    Originally Posted by clifford
    These lines in your code:

    Code:
        char A, B, C;
    
        A = 'A';
        B = 'B';
        C = 'C';
    Serve absolutely no purpose whatsoever. Follow your code and see that they are redundant. THe character you enter is assigned to the variable "letter", and that variable is compared directly with the literal constants 'A', 'B' and 'C' in the switch-case, nowhere are the variables A, B or C used.

    You should set your compiler settings to maximise the warning level, in which case you will probably get a "variable A was assigned but never used" warning or similar for example.

    What compiler are you using? In GCC the options you need are "-Wall -Wformat -Werror", in VC++ "\W4 \WX"
    I'm using CodeBlocks with a GNU GCC Compiler.

    So I don't need to declare those variables after specifying the variable "letter," then? But then, if I use the variables 'A,' 'B,' and 'C' in the case statements in that situation, won't it give me a "undeclared, first use in this function" error?

    Edit: Okay, I took those lines out and it still works perfectly fine, plus I'm not getting any errors or warnings. This is the code:

    Code:
    #include <stdio.h>
    
    int main()
    {
        char letter;
    
        printf("Enter the error code (A-C): ");
        scanf("%c",&letter);
    
        switch(letter)
        {
            case 'A':
                puts("Drive Fault, not your fault.");
                break;
            case 'B':
                puts("Illegal format, call a lawyer.");
                break;
            case 'C':
                puts("Bad filename, spank it.");
                break;
            default:
                puts("That's not A, B, or C.");
        }
        return(0);
    
    }
    But now the book says that if I understand the switch-case structure well enough, I should now try to modify the source code such that I can input either upper- or lower-case letters A, B, or C to get the output. The problem is that I don't understand the switch-case structure well enough for that yet. So I need to understand that.

    Would you guys help me here on that, or should I search on the internet for something I could watch or read so that I could understand it on my own? Which option do you guys prefer?
  18. #10
  19. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,644
    Rep Power
    1945
    Normally, a case statement is stopped with a break before the next case is started.

    If you don't add a break before the next case, it will continue to process that case too.

    This is a technique/functionality called Fallthrough
    Last edited by MrFujin; December 23rd, 2013 at 03:20 PM.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Karachi, Pakistan
    Posts
    16
    Rep Power
    0
    So I need to add another break after the "default" case and then make another switch-case structure with the lower-case letters a-c? And will the same variable "letter" work for that one as well, or do I have to declare it again?

    Edit: I tried doing it, but with what I've got now, the program now requires me to input 'Aa' when I want to input a lower-case 'a,' and I can't just input only the lower-case 'a' by itself. Same with B and C (I need to input 'Bb' or "Cc" to get the desired result when wanting to use the lower-case letters a-c).

    The source code looks like this:

    Code:
    #include <stdio.h>
    
    int main()
    {
        char letter;
    
        printf("Enter the error code (A-C or a-c): ");
        scanf("%c",&letter);
    
        switch(letter)
        {
            case 'A':
                puts("Drive Fault, not your fault.");
                break;
            case 'B':
                puts("Illegal format, call a lawyer.");
                break;
            case 'C':
                puts("Bad filename, spank it.");
                break;
            default:
                puts("That's not A, B, or C.");
                break;
        }
        return(0);
    
        switch(letter)
        {
        case 'a':
            puts("Drive Fault, not your fault.");
            break;
        case 'b':
            puts("Illegal format, call a lawyer.");
            break;
        case 'c':
            puts("Bad filename, spank it.");
            break;
        default:
            puts("That's not a, b, or c.");
            break;
        }
        return(1);
    }
  22. #12
  23. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    You can either do
    Code:
        switch(letter)
        {
            case 'A': case 'a':
                puts("Drive Fault, not your fault.");
                break;
    or
    Code:
        switch(toupper(letter))
        {
            case 'A':
                puts("Drive Fault, not your fault.");
                break;
    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
  24. #13
  25. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    Originally Posted by DragonOsman2
    I tried doing it, but with what I've got now, the program now requires me to input 'Aa' when I want to input a lower-case 'a,'
    Your diagnosis on the problem is flawed, and your work-around nonsense - it is only the first 'A' that is processed at all, the second "a" is ignored, and the second switch never runs because the return(0) (your parentheses not mine because it can't be taught) is unconditional.

    It is generally regarded as bad form to have a more than one return statement, and to return any where other then end of the function.

    That said of course the real problem is that you don't need two switches at all, simply convert any lower-case input to upper (or vice versa), or test for upper and lower in a single switch.

    At this stage (the stage at which you can actually compile and run code), you should really learn to use a source-level symbolic debugger. You can step through your code and observe variable states at each step. You'll learn a lot more a lot faster, and be able to solve your own problems more quickly rather than waiting for "forum turnaround".

    Comments on this post

    • 2teez agrees
    Last edited by clifford; December 25th, 2013 at 04:24 PM.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Karachi, Pakistan
    Posts
    16
    Rep Power
    0
    Originally Posted by salem
    You can either do
    Code:
        switch(letter)
        {
            case 'A': case 'a':
                puts("Drive Fault, not your fault.");
                break;
    or
    Code:
        switch(toupper(letter))
        {
            case 'A':
                puts("Drive Fault, not your fault.");
                break;
    I did what the first example suggested and now it works just like I want it to. Thanks.

    Here is the source code, as a reference:

    Code:
    #include <stdio.h>
    
    int main()
    {
        char letter;
    
        printf("Enter the error code (A-C or a-c): ");
        scanf("%c",&letter);
    
        switch(letter)
        {
            case 'A': case 'a':
                puts("Drive Fault, not your fault.");
                break;
            case 'B': case 'b':
                puts("Illegal format, call a lawyer.");
                break;
            case 'C': case 'c':
                puts("Bad filename, spank it.");
                break;
            default:
                puts("That's not A, B, or C.");
        }
        return(0);
    }

IMN logo majestic logo threadwatch logo seochat tools logo