lundi 8 avril 2019

Can we use both character and int in if else statements? [duplicate]

This question already has an answer here:

I have a situation where I need to use both character and integer in if else statement. Situation is the possible values are 1,2,3,4,P. If int = 1, something happens... And finally if else char = P, something happens. How do I do that?

Code::

    int demo;
    char demo1;
    printf("Enter a demo: ");
    scanf_s("%d", &demo); /* scanf is depreciated */
    scanf_s("%c", &demo1);

   if(demo == 1) {
       printf("Do something");
   }
   else if(demo == 2) {
       printf("Do smth");
   }
   else if(demo1 == 'P') {
       printf("Do smth");
   }
   else {
       printf("Invalid input")
   }

Problem is when I input P in the output terminal, other condition is applied rather applying the condition provided to if else (demo1 = 'P').

enter image description hereenter image description here

1: Code screenshot

Aucun commentaire:

Enregistrer un commentaire