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.

I was trying nested switch but after choosing ice cream the scanf is not asking input.

0 votes
asked Oct 31, 2021 by Isheta Aggarwal (120 points)
//I did try without scanf() and it worked but why scanf() not working

//here's the code

#include <stdio.h>  
int main () {  
  
   char i,j;
   printf("Enter a for za\nEnter b for ice cream\n ");
   scanf("%c",&i);
   
   switch(i) {  
     
      case 'a':   
         printf("You ordered veggie pizza\n");break;
      case 'b':
      {
                printf("Enter c for choco\nEnter v for vanilla\n");
                scanf("%c",&j);
                 switch(j) {  
                     case 'c':  
                     printf("You ordered choco yumyum\n");break;
                 case 'v':
                  printf("You ordered plain vanilla\n ");break;
         }  
      }
      break;
      default: printf("invisible food\n"); break;    
   }  
     
  
   return 0;  
}

1 Answer

0 votes
answered Oct 31, 2021 by Peter Minarik (55,660 points)

After you input 'b' the standard input also has the newline character. The next time you scan for a character, it's automatically taken from the standard input. If you want to ignore these whitespace characters, add a space before your character token in your format string (everywhere). E.g.:

scanf(" %c", &i);
// ...
scanf(" %c", &j);
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.
...