Results 1 to 3 of 3

Thread: identify the error in switch code

  1. #1
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: identify the error in switch code

    Fixed this code for you:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int c, t;
        printf("Enter the number of subjects he has failed in: ");
        scanf("%d%d", &c, &t);
    
        switch(c)
        {
            case 1:
                switch(t>3)
                {
                    case 1:
                        printf("\nNo grace");
                        break;
                    case 0:
                        printf("\nGrace of 5 marks per subject");
                        break;
                }
                break;
    
            case 2:
                switch(t>2)
                {
                    case 1:
                        printf("\nNo grace");
                        break;
                    case 0:
                        printf("\nGrace of 5 marks per subject");
                        break;
                }
                break;
    
            case 3:
                switch(t>1)
                {
                    case 1:
                        printf("\nNo grace");
                        break;
                    case 0:
                        printf("\nGrace of 5 marks per subject");
                        break;
                }
                break;
        }
    }
    Now what exactly is the problem?
    「明後日の夕方には帰ってるからね。」


  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: identify the error in switch code

    A rather unusual use of the switch statement.

    Code:
    switch(t>2) {
        case 1:
            printf("\nNo grace");
            break;
        case 0:
            printf("\nGrace of 5 marks per subject");
            break;
    }
    is better written:
    Code:
    if (t>2) {
        printf("\nNo grace");
    } else {
        printf("\nGrace of 5 marks per subject");
    }

  3. #3
    Join Date
    Feb 2009
    Location
    USA
    Beans
    3,186

    Re: identify the error in switch code

    The code wouldn't even compile without replacing s with t. Then the only time one would use "switch" instead of "if" is for a homework.

    - Is this a homework question?
    - What is this code supposed to do?
    - You should really use "if" instead of "switch" for things like (t>3).
    - I would also add another "\n" in the end of all the printf statements as it will make the output look a bit better.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •