Home > Blockchain >  My switch case is not working when I enter 1 my case doesnt work
My switch case is not working when I enter 1 my case doesnt work

Time:12-02

When I run my code my switch cases are not working when I enter 1 it doesn't print case 1 and so on I try every thing but didn't work can someone help me please When I run my code my switch cases are not working when I enter 1 it doesn't print case 1 and so on I try every thing but didn't work can someone help me please

 #include <stdio.h>
  #include <math.h>
   int main ()
  {
       int choice;
       float area;


   printf("1 for area of Square\n");
    printf(" 2 for area of Circle\n");
   printf(" 3 for finding area of rectangle\n");

    switch(choice) {


     case 1: {
         float side,area;
          printf("Enter Sides of Square");
          scanf("%f",&side);
             area=(float)side*side;
             printf("Area of Square is %f",area);
            break;
           }
  
  
         case 2: {
            float radius,area;
          printf("Enter Radius of Circle");
           scanf("%f",&radius);
            area=(float)3.14159*radius*radius;
           printf("Area of Circle %f",area);
           break;
          }
           case 3: {
           float len,breadth,area;
           printf("Enter Length and Breadth of Rectangle");
            scanf("%f %f",&len,&breadth);
              area=(float)len*breadth;
           printf("Area of Rectangle is %f",area);
            break;
              }
 
          default: {
           printf("Invalid Choice");
           break;
          }

       }
       return 0;**strong text**
      }

CodePudding user response:

you are missing to take user input. I have added scanf below line and it works now

scanf("%d", &choice);

complete version of your code

#include <stdio.h>
#include <math.h>
int main ()
{
    int choice;
    float area;


    printf ("1 for area of Square\n");
    printf (" 2 for area of Circle\n");
    printf (" 3 for finding area of rectangle\n");
    scanf("%d", &choice);

    switch (choice)
    {
        case 1:
        {
            float side, area;
            printf ("Enter Sides of Square");
            scanf ("%f", &side);
            area = (float) side *side;
            printf ("Area of Square is %f", area);
            break;
        }
        case 2:
        {
            float radius, area;
            printf ("Enter Radius of Circle");
            scanf ("%f", &radius);
            area = (float) 3.14159 *radius * radius;
            printf ("Area of Circle %f", area);
            break;
        }
        case 3:
        {
            float len, breadth, area;
            printf ("Enter Length and Breadth of Rectangle");
            scanf ("%f %f", &len, &breadth);
            area = (float) len *breadth;
            printf ("Area of Rectangle is %f", area);
            break;
        }
        default:
        {
            printf ("Invalid Choice");
            break;
        }
    }
  return 0;
}

Output:

1 for area of Square
 2 for area of Circle
 3 for finding area of rectangle
1
Enter Sides of Square2
Area of Square is 4.000000
  •  Tags:  
  • c
  • Related