Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Its possible use "if else" inside "case"?

+4 votes
asked Jun 27, 2020 by Bruno Rodrigues (190 points)
#include<stdio.h>

#include<stdlib.h>

#include<ctype.h>

int main ()

{

int comic,D,M,F;

int flash=3.55,superman=4.99,wonderwoman=2.85,batman=4.99,greenlantern=3.80;

int hulk=3.55,ironman=4.85,capitanamerica=4.15,capitanmarvel=2.75,avengers=4.99;

char op;

do

{

printf("\nMENU\n\nDC - DC Comics\nM - Marvel Comics");

        printf("\nC close sale\nOPTION:");

        scanf("%s",op);

        op = toupper(op);

        char op;

        switch (op)

        {

            case 'D':

                       if(comic == 1)

                           printf("\nYou bought a comic of flash");

                        else

                            if(comic == 2)

                                printf("\nYou bought a comic of superman");

                            else

                                if (comic == 3)

                                     printf("\nYou bought a comic of wonder woman");

                                else

                                    if (comic == 4)

                                         printf("\nYou bought a comic of batman");

                                    else

                                        if (comic == 5)

                                             printf("\nYou bought a comic of green lantern");

                                        else

                                            printf("\nNone Exist");

                                            break;

            case 'M':

                       if(comic==6)

                           printf("\nYou bought a comic of hulk");

                        else

                            if(comic==7)

                                printf("\nYou bought a comic of iron man");

                            else

                                if(comic==8)

                                    printf("\nYou bought a comic of capitan america");

                                else

                                    if(comic==9)

                                        printf("\nYou bought a comic of capitan marvel");

                                    else

                                        if(comic==10)

                                            printf("\nYou bought a comic of avengers");

                                        else

                                            printf("\nNone Exist");

                                            break;

            case 'F':

                       printf("\nGoing Out");

                       break;

            default:   

                       printf("\nNONEXISTENT OPTION");

        }

        fgetc(stdin);

}

while(op!='finish');

system("pause");

return 0;

}

2 Answers

0 votes
answered Jun 27, 2020 by xDELLx (10,500 points)
edited Jun 27, 2020 by xDELLx

printf("\nMENU\n\nDC - DC Comics\nM - Marvel Comics");

        printf("\nC close sale\nOPTION:");

        scanf("%s",op);


should be


printf("\nMENU\n\nDC - DC Comics\nM - Marvel Comics");

        printf("\nC close sale\nOPTION:");

        scanf("%c",&op);


Original code has new var "op"  before the switch ,which over-rides the input from user & is un wanted.

       op = toupper(op);

        char op;

        switch (op

Post that not sure where values of "comic" was read .. after which it matches in the inner case statements

0 votes
answered Jun 29, 2020 by Peter Minarik (86,180 points)

Problems with the code

There are various issues with the code. Some of them have a warning too, when you compile. You just have to pay attention to them.

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

int main() 
{
    int comic, D, M, F;
    int flash = 3.55, superman = 4.99, wonderwoman = 2.85, batman = 4.99, greenlantern = 3.80;
    int hulk = 3.55, ironman = 4.85, capitanamerica = 4.15, capitanmarvel = 2.75, avengers = 4.99;
    char op; // Problem #1: Definition of 'op'.
    do
    {
        printf ("\nMENU\n\nDC - DC Comics\nM - Marvel Comics");
        printf ("\nC close sale\nOPTION:");
        scanf ("%s", op); // Problem #2: reading string into char
        op = toupper (op);
        char op; // Problem #1: Redefinition of 'op'.
        switch (op)
        {
            case 'D':
                if (comic == 1)
                    printf ("\nYou bought a comic of flash");
                else
                    if (comic == 2)
                        printf ("\nYou bought a comic of superman");
                    else
                        if (comic == 3)
                            printf ("\nYou bought a comic of wonder woman");
                        else
                            if (comic == 4)
                                printf ("\nYou bought a comic of batman");
                            else
                                if (comic == 5)
                                    printf ("\nYou bought a comic of green lantern");
                                else
                                    printf ("\nNone Exist");
                break;

            case 'M':
                if (comic == 6)
                    printf ("\nYou bought a comic of hulk");
                else
                    if (comic == 7)
                        printf ("\nYou bought a comic of iron man");
                    else
                        if (comic == 8)
                            printf ("\nYou bought a comic of capitan america");
                        else
                            if (comic == 9)
                                printf ("\nYou bought a comic of capitan marvel");
                            else
                                if (comic == 10)
                                    printf ("\nYou bought a comic of avengers");
                                else
                                    printf ("\nNone Exist");
                break;

            case 'F':
                printf ("\nGoing Out");
                break;

            default:
                printf ("\nNONEXISTENT OPTION");
        }
        fgetc (stdin);
    } while (op != 'finish'); // Problem #3

    system ("pause");
    return 0;
}

Problem #1

The variable op has already been defined. Redefining it deletes its previous values. Fix: do not redefine it.

Problem #2

'op' is defined as a character, yet you read value to it as a C-string ("%s"). Also, scanf requires a memory pointer where it should write the read value, but you provide the variable (not the address of the variable, as it should be).
Fix: read just a single character from the input. (Alternatively, you could read a whole string, change the data type of op to char op[SOME_SIZE], where you pick the SOME_SIZE for your own buffer.)
scanf ("%c", &op);

Problem #3

You compare the character stored in op against the character 'f'. 'finish' is really just a single character 'f'. If you want to create a C-string, you have to do it in double quotes: "finish". Single quotes are for characters, that are really supposed to have a length of one, such as 'f'.
Furthermore. please remember that you made your op upper case so comparing it against a lower case 'f' will never yield true.
Fix: compare op against a single uppercase 'F' letter:
} while (op != 'F');

Problem #4

Well, the rest of the code is unfinished (e.g. comic is not set). Go ahead, and implement the rest.
Good luck! :)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...