Hey i am wondering if it is possible to have a switch statement in a function that is leading to another switch statement and another switch statement afterwards.

Question.

Write a program that will give the user the option to select a shape or a three dimensional object. The program should then prompt the user for the required dimensions and calculate the option the user selects, i.e. perimeter, area, volume, or surface area. Program should be Modular.

When i ran my program , It prompts the user to select shape or 3d objects then afterwards goes on to ask which shape they would like to calculate but unfortunately after i select it, it goes into that function for calculation but it terminates, i have no clue why though.

If anyone can reply to me and tells me it is possible that you can have a case in a case in a case function , i will go fix it again and again till it works.

This is my coding for the question:

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

int four()
  {
    float area, perimeter,l,w;
    char option;

       printf("What Would You like to Calculate: Area Or Perimeter?...Indicate by entering the Initial Letter of the word\n");
       scanf("%c", &option);

          switch(option)
          {
              case 'A':
              case 'a':
                    printf("Enter the length and width\n");
                    scanf("%f", &l);
                    scanf("%f", &w);
                    area = l*w;
                    printf("\nThe area of the shape is %.2f", &area);
                    getch();
             break;

             case 'P':
             case 'p':
                   printf("Enter the length and width\n");
                   scanf("%f", &l);
                   scanf("%f", &w);
                   perimeter = (2*l)+(2*w);
                   printf("\nThe perimeter of the shape is %.2f", &perimeter);
                   getch();
             break;

            default:
                  printf("You have entered the wrong choice...Terminating\n");
            break;
         }
    return 0;
  }

int circle()
  {
    float r,area,circumference;
    char option;

      printf("\tNote: If you have the diameter of the circle, convert it to its radius by using the formula --> diameter/2\n");
      getch();
      printf("What do you want to calculate: Circumference or the Area...Indicate by entering the Initial Letter of the word?\n");
      scanf("%c", &option);

    switch(option)
    {
        case 'C':
        case 'c':
               printf("What is the length of the radius?\n");
               scanf("%f", &r);
               circumference = 2*3.141592654*r;
               printf("\nThe circumference of the circle is %.2f", &circumference);
               getch();
        break;

        case 'A':
        case 'a':
               printf("What is the length of the radius?\n");
               scanf("%f", &r);
               area = 3.141592654*r*r;
               printf("\nThe area of the circle is %.2f", &area);
               getch();
        break;

        default:
                printf("That choice does not exist...\n");
        break;
    }
    return 0;
  }
int triangle()
{
      float h,b,perimeter,area,lg_1,lg_2,lg_3;
      char option;

         printf("What do you want to find, perimeter or area...Indicate by entering the Initial Letter of the word?\n");
         scanf("%c", &option);

           switch(option)
           {
               case 'A':
               case 'a':
                      printf("Enter the height and base of the triangle please..\n");
                      scanf("%f", &h);
                      scanf("%f", &b);
                      area = 0.5*b*h;
                      printf("The area of the triangle is %.2f", &area);
                      getch();
               break;

               case 'P':
               case 'p':
                      printf("\nEnter the length of the sides of the triangle");
                      scanf("%f", &lg_1);
                      scanf("%f", &lg_2);
                      scanf("%f", &lg_3);
                      perimeter = lg_1+lg_2+lg_3;
                      printf("\nThe perimeter of the triangle is %.2f", &perimeter);
                      getch();
              break;

              default:
                      printf("Option does not exist mate..\n");
              break;
           }
     return 0;
}

char shape()
{
  char option;

     printf("NOTE: The shapes that can be calculated involves circles,rectangles,triangles and squares..It is yet to be upgraded\n");
     getch();
     system("cls");
     printf("What shape would you like to find?Indicate by entering the Initial Letter of the word\n");
     scanf("%c", &option);

        switch(option)
        {
            case 'S':
            case 's':
                   four();
            break;

            case 'R':
            case 'r':
                   four();
            break;

            case 'C':
            case 'c':
                  circle();
            break;

            case 'T':
            case 't':
                  triangle();
            break;
       }
    return 0;
}

int cube()
{
   float volume,area,surface_area,h,l,w;
   char option;

      printf("What would you like to find out about the cube: volume or surface area..Indicate by entering the Initial Letter of the word\n");
      scanf("%c",&option);

          switch(option)
             {
                 case 'V':
                 case 'v':
                     printf("Enter the height, length and width of the cube please\n");
                     scanf("%f", &h);
                     scanf("%f", &l);
                     scanf("%f", &w);
                     volume = l*w*h;
                     printf("\nThe volume of the cube is %.2f", &volume);
                 break;

                case 'S':
                case 's':
                     printf("Enter the length and width  of the cube\n");
                     scanf("%f", &l);
                     scanf("%f", &w);
                     area = l*w;
                     surface_area = 6*(l*w);
                     printf("\nThe surface area of the cube is %.2f", &surface_area);
                break;
             }
    return 0;
}

int cuboid()
{
    float l,w,h,volume,surface_area;
    char option;

      printf("What would you like to find for the cuboid: surface area or volume?Indicate by entering the Initial Letter of the word\n");
      scanf("%c", &option);

        switch(option)
           {
               case 'S':
               case 's':
                  printf("Enter the length,width and height\n");
                  scanf("%f", &h);
                  scanf("%f", &l);
                  scanf("%f", &w);
                  surface_area = 2*((l*w)+(w*h)+(l*h));
                  printf("\nThe surface area of the cuboid is %.2f", &surface_area);
              break;

              case 'V':
              case 'v':
                  printf("Enter the length,width and height\n");
                  scanf("%f", &h);
                  scanf("%f", &l);
                  scanf("%f", &w);
                  volume = l*w*h;
                  printf("\nThe volume of the cuboid is %.2f", &volume);
              break;

              default:
                  printf("Wrong choice chosen..\n");
                  exit(EXIT_SUCCESS);
              break;
           }
     return 0;
}

int cylinder()
{
    float volume,r,surface_area,h;
    char option;

       printf("\tNote: If you have the diameter of the circle, convert it to its radius by using the formula --> diameter/2\n");
       getch();
       system("cls");
       printf("Do you want to find the volume or surface area of a cylinder?Indicate by entering the Initial Letter of the word\n");
       scanf("%c", &option);

        switch(option)
        {
            case 'V':
            case 'v':
               printf("Enter the radius and height of the cylinder\n");
               scanf("%f", &r);
               scanf("%f", &h);
               volume = 3.141592654*r*r*h;
               printf("\nThe volume of the cylinder was found to be %.2f", &volume);
            break;

            case 'S':
            case 's':
               printf("Enter the radius and height of the cylinder\n");
               scanf("%f", &r);
               scanf("%f", &h);
               surface_area = (2*3.141592654*r*r)+ (2*3.141592654*r*h);
               printf("\nThe surface area of the cylinder is %.2f", &surface_area);
            break;

            default:
                  printf("\nWrong choice selected..");
            break;
        }
    return 0;
}

int sphere()
{
   float r,volume,surface_area;
   char option;

       printf("\tNote: If you have the diameter of the sphere, convert it to its radius by using the formula --> diameter/2\n");
       getch();
       system("cls");
       printf("Do you want to find the volume or surface area of the sphere?Indicate by entering the Initial Letter of the word\n");
       scanf("%f", &option);

         switch(option)
         {
             case 'V':
             case 'v':
                    printf("Enter the radius of the sphere\n");
                    scanf("%f", &r);
                    volume = (4/3)*3.141592654*r*r*r;
                    printf("\nThe volume of the sphere is %.2f", &volume);
             break;

             case 'S':
             case 's':
                   printf("Enter the radius of the sphere\n");
                   scanf("%f", &r);
                   surface_area = 4*3.141592654*r*r;
                   printf("\nThe surface area of the sphere is %.2f");
            break;
         }
 return 0;
}

int tprism()
{
  float s,l,h,b,volume,surface_area;
  char option;

    printf("What do you want to find: volume or surface area of the triangular prism?Indicate by entering the Initial Letter of the word\n");
    scanf("%c", &option);

      switch(option)
      {
         case 'V':
         case 'v':
               printf("Enter the  length of the prism, height and base\n");
               scanf("%f", &l);
               scanf("%f", &h);
               scanf("%f", &b);
               volume = 0.5*(b*h)*l;
               printf("\nThe volume of the prism is %.2f", &volume);
         break;

         case 'S':
         case 's':
               printf("Enter the slope's length, length of the prism, height and base\n");
               scanf("%f", &s);
               scanf("%f", &l);
               scanf("%f", &h);
               scanf("%f", &b);
               surface_area = (b*h)+(2*l*s)+(l*b);
               printf("\nThe surface area of the prism is %.2f", &surface_area);
         break;

         default:
            printf("Wrong option selected...\n");
            exit(EXIT_SUCCESS);
         break;
      }

  return 0;
}


int main()
{
char dimensional()
{
    char choice;

    printf("WARNING:The 3D objects that can be calculated includes cube, cuboid, sphere, cylinder, triangular prism.....It is yet to be upgraded\n");
      printf("Enter y for cylinder, c for cube, s for sphere, b for cuboid, p for tprism\n\n");
      printf("What 3D object do you want to find: cube, cuboid, sphere, cylinder or triangular prism?\n");
      scanf("%c", &choice);

         switch(choice)
            {
             case 'Y':
             case 'y':
                   cylinder();
             break;

             case 'C':
             case 'c':
                   cube();
             break;

             case 'P':
             case 'p':
                   tprism();
             break;

             case 'B':
             case 'b':
                   cuboid();
             break;

             case 'S':
             case 's':
                  sphere();
             break;

             default:
                  printf("Wrong choice selected......Terminating\n");
                  exit(EXIT_SUCCESS);
            break;
            }
    return 0;
}

  char option,choice;

     printf("Would you like to find calculations for a shape or 3D objects?\n");
     printf("Input s for shape and d for 3D objects:");
     scanf("%c", &option);

      switch(option)
      {
          case 'S':
          case 's':
                  shape();
          break;

          case 'D':
          case 'd':
                dimensional();
          break;

          default:
                printf("Wrong option selected..\n");
                exit(EXIT_SUCCESS);
          break;
      }
    return 0;
}

Recommended Answers

All 8 Replies

Yes its possible to have a case within a case.

Did you compile your work cause I can see a lot of syntax error

scanf("%c", &option);

In lines 11,50,84,124,157,190,231,298,342,383
Use %s in receiving input

printf("Do you want to find the volume or surface area of the sphere?Indicate by entering the Initial Letter of the word\n");
scanf("%f", &option);

In line 268:
option is not a float

printf("\nThe surface area of the sphere is %.2f");

In line 285:
you forgot to put &surface_area

Yes its possible to have a case within a case.

Did you compile your work cause I can see a lot of syntax error


In lines 11,50,84,124,157,190,231,298,342,383
Use %s in receiving input


In line 268:
option is not a float


In line 285:
you forgot to put &surface_area

oo which compiler do you use?o.o

oo which compiler do you use?o.o

linux gcc

linux gcc

oh , i only got codeblocks don't know if it is good , since it didn't point much errors for me :$

On another note, how long have you been studying programming? i have only been studying for a month..

oh , i only got codeblocks don't know if it is good , since it didn't point much errors for me

On another note, how long have you been studying programming? i have only been studying for a month..

2 years.... most of the errors I pointed out are basic errors which can be fixed just by looking at your code

The compiler is not as important as the programmer

2 years.... most of the errors I pointed out are basic errors which can be fixed just by looking at your code

The compiler is not as important as the programmer

...

You are awesome:-O..true words, but it is just that my lazy teacher is good for nothing really. Well thanks again , hope you can help me if i am stuck on any more trouble

There is 1 more problem and most important at this moment; returning the values back,
i have used structure only in area for int four() function which is the only case function i have tested and still returns no value.

code now:

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

struct square
{
    float length;
    float width;
    float area;
} sq;

int four()
  {
    float area, perimeter,l,w;
    char option;

       printf("\n\nWhat Would You like to Calculate:");
       printf("\n\n<A>Area");
       printf("\n<P>Perimeter");
       printf("\n\nOption selected:");
       scanf("%s", &option);

          switch(option)
          {
              case 'A':
              case 'a':
                    printf("Enter the length\n");
                    scanf("%f", &sq.length);
                    printf("Enter the width\n");
                    scanf("%f", &sq.width);
                    sq.area = sq.length*sq.width;
                    printf("\nThe area of the shape is %.2f", &sq.area);
             break;

             case 'P':
             case 'p':
                   printf("Enter the length\n");
                   scanf("%f", &l);
                   printf("Enter the width\n");
                   scanf("%f", &w);
                   perimeter =((2*l)+(2*w));
                   printf("\nThe perimeter of the shape is %.2f", &perimeter);
                   return perimeter;
             break;

            default:
                  printf("You have entered the wrong choice...Terminating\n");
            break;
         }
  return sq.area; //Doesn't work when i put return sq.area here//
}

int circle()
  {
    float r,area,circumference;
    char option;

      printf("\n\nNote:If you have the diameter of the circle, convert to radius by using the formula:diameter/2");
      printf("\n\nWhat do you want to calculate:");
      printf("\n<A>Area");
      printf("\n<C>Circumference");
      printf("\n\nOption selected:");
      scanf("%s", &option);

    switch(option)
    {
        case 'C':
        case 'c':
               printf("What is the length of the radius?\n");
               scanf("%f", &r);
               circumference =(2*3.141592654*r);
               printf("\nThe circumference of the circle is %.2f", &circumference);
               return circumference;
        break;

        case 'A':
        case 'a':
               printf("What is the length of the radius?\n");
               scanf("%f", &r);
               area =(3.141592654*r*r);
               printf("\nThe area of the circle is %.2f", &area);
               return area;
        break;

        default:
                printf("That choice does not exist...\n");
        break;
    }
}

int triangle()
{
      float h,b,perimeter,area,lg_1,lg_2,lg_3;
      char option;

         printf("\n\nWhat do you want to calculate:");
         printf("\n<P>Perimeter");
         printf("\n<A>Area");
         printf("\n\nOption selected:");
         scanf("%s", &option);

           switch(option)
           {
               case 'A':
               case 'a':
                      printf("Enter the height\n");
                      scanf("%f", &h);
                      printf("Enter the base\n");
                      scanf("%f", &b);
                      area =(0.5*b*h);
                      printf("The area of the triangle is %.2f", &area);
                      return area;
               break;

               case 'P':
               case 'p':
                      printf("Enter the 1st length of the triangle\n");
                      scanf("%f", &lg_1);
                      printf("Enter the 2nd length\n");
                      scanf("%f", &lg_2);
                      printf("Enter the 3rd length\n");
                      scanf("%f", &lg_3);
                      perimeter =(lg_1+lg_2+lg_3);
                      printf("\nThe perimeter of the triangle is %.2f", &perimeter);
                      return perimeter;
              break;

              default:
                      printf("Option does not exist mate..\n");
              break;
           }
}

char shape()
{
  char option;

     printf("n\nWhat shape would you like to find:");
     printf("\n<S>Square");
     printf("\n<R>Rectangle");
     printf("\n<C>Circle");
     printf("\n<T>Triangle");
     printf("\n\nOption selected:");
     scanf("%s", &option);

        switch(option)
        {
            case 'S':
            case 's':
                   four();
            break;

            case 'R':
            case 'r':
                   four();
            break;

            case 'C':
            case 'c':
                  circle();
            break;

            case 'T':
            case 't':
                  triangle();
            break;
       }
}

int cube()
{
   float volume,area,surface_area,h,l,w;
   char option;

      printf("\n\nWhat would you like to calculate:");
      printf("\n<V>Volume");
      printf("\n<S>Surface Area");
      printf("\n\nOption selected:");
      scanf("%s",&option);

          switch(option)
             {
                 case 'V':
                 case 'v':
                     printf("Enter the height\n");
                     scanf("%f", &h);
                     printf("Enter the length\n");
                     scanf("%f", &l);
                     printf("Enter the width\n");
                     scanf("%f", &w);
                     volume =(l*w*h);
                     printf("\nThe volume of the cube is %.2f", &volume);
                     return volume;
                 break;

                case 'S':
                case 's':
                     printf("Enter the length\n");
                     scanf("%f", &l);
                     printf("Enter the width\n");
                     scanf("%f", &w);
                     area =(l*w);
                     surface_area =(6*(l*w));
                     printf("\nThe surface area of the cube is %.2f", &surface_area);
                     return surface_area;
                break;
             }
}

int cuboid()
{
    float l,w,h,volume,surface_area;
    char option;

      printf("\n\nWhat would you like to calculate:");
      printf("\n<S>Surface Area");
      printf("\n<V>Volume");
      printf("\n\nOption selected:");
      scanf("%s", &option);

        switch(option)
           {
               case 'S':
               case 's':
                  printf("Enter the height\n");
                  scanf("%f", &h);
                  printf("Enter the length\n");
                  scanf("%f", &l);
                  printf("Enter the width\n");
                  scanf("%f", &w);
                  surface_area =(2*((l*w)+(w*h)+(l*h)));
                  printf("\nThe surface area of the cuboid is %.2f", &surface_area);
                  return surface_area;
              break;

              case 'V':
              case 'v':
                  printf("Enter the height\n");
                  scanf("%f", &h);
                  printf("Enter the length\n");
                  scanf("%f", &l);
                  printf("Enter the width\n");
                  scanf("%f", &w);
                  volume =(l*w*h);
                  printf("\nThe volume of the cuboid is %.2f", &volume);
                  return volume;
              break;

              default:
                  printf("Wrong choice chosen..\n");
                  exit(EXIT_SUCCESS);
              break;
           }
}

int cylinder()
{
    float volume,r,surface_area,h;
    char option;

       printf("Note: If you have the diameter of the circle, convert it to its radius by using the formula --> diameter/2\n");
       printf("\n\nDo you want to calculate:");
       printf("\n<V>Volume");
       printf("\n<S>Surface Area");
       printf("\n\nOption selected:");
       scanf("%s", &option);

        switch(option)
        {
            case 'V':
            case 'v':
               printf("Enter the radius of the cylinder\n");
               scanf("%f", &r);
               printf("Enter the height\n");
               scanf("%f", &h);
               volume =(3.141592654*r*r*h);
               printf("\nThe volume of the cylinder was found to be %.2f", &volume);
               return volume;
            break;

            case 'S':
            case 's':
               printf("Enter the radius of the cylinder\n");
               scanf("%f", &r);
               printf("Enter the height\n");
               scanf("%f", &h);
               surface_area =((2*3.141592654*r*r)+ (2*3.141592654*r*h));
               printf("\nThe surface area of the cylinder is %.2f", &surface_area);
               return surface_area;
            break;

            default:
                  printf("\nWrong choice selected..");
            break;
        }
}

int sphere()
{
   float r,volume,surface_area;
   char option;

       printf("Note: If you have the diameter of the sphere, convert it to its radius by using the formula --> diameter/2\n");
       printf("\n\nDo you want to calculate:");
       printf("\n<V>Volume");
       printf("\n<S>Surface Area");
       printf("\n\nOption selected:");
       scanf("%s", &option);

         switch(option)
         {
             case 'V':
             case 'v':
                    printf("Enter the radius of the sphere\n");
                    scanf("%f", &r);
                    volume =((4/3)*3.141592654*r*r*r);
                    printf("\nThe volume of the sphere is %.2f", &volume);
                    return volume;
             break;

             case 'S':
             case 's':
                   printf("Enter the radius of the sphere\n");
                   scanf("%f", &r);
                   surface_area =(4*3.141592654*r*r);
                   printf("\nThe surface area of the sphere is %.2f", &surface_area);
                   return surface_area;
            break;
         }
}

int tprism()
{
  float s,l,h,b,volume,surface_area;
  char option;

    printf("\n\nWhat do you want to calculate:");
    printf("\n<V>Volume");
    printf("\n<S>Surface Area");
    printf("\n\nOption selected:");
    scanf("%s", &option);

      switch(option)
      {
         case 'V':
         case 'v':
               printf("Enter the  length of the prism\n");
               scanf("%f", &l);
               printf("Enter the height\n");
               scanf("%f", &h);
               printf("Enter the base\n");
               scanf("%f", &b);
               volume =(0.5*(b*h)*l);
               printf("\nThe volume of the prism is %.2f", &volume);
               return volume;
         break;

         case 'S':
         case 's':
               printf("Enter the slope's length of the prism\n");
               scanf("%f", &s);
               printf("Enter the  length\n");
               scanf("%f", &l);
               printf("Enter the height\n");
               scanf("%f", &h);
               printf("Enter the base\n");
               scanf("%f", &b);
               surface_area =((b*h)+(2*l*s)+(l*b));
               printf("\nThe surface area of the prism is %.2f", &surface_area);
               return surface_area;
         break;

         default:
            printf("Wrong option selected...\n");
            exit(EXIT_SUCCESS);
         break;
      }
}


int main()
{
char dimensional()
{
    char choice;

      printf("\n\nWhat 3D object do you want to find:");
      printf("\n<C>Cube");
      printf("\n<B>Cuboid");
      printf("\n<S>Sphere");
      printf("\n<Y>Cylinder");
      printf("\n<P>Triangular Prism");
      printf("\n\nYour choice:");
      scanf("%s", &choice);

         switch(choice)
            {
             case 'Y':
             case 'y':
                   cylinder();
             break;

             case 'C':
             case 'c':
                   cube();
             break;

             case 'P':
             case 'p':
                   tprism();
             break;

             case 'B':
             case 'b':
                   cuboid();
             break;

             case 'S':
             case 's':
                  sphere();
             break;

             default:
                  printf("Wrong choice selected......Terminating\n");
                  exit(EXIT_SUCCESS);
            break;
            }
}

  char option;

     system("cls");
     printf("Welcome User");
     printf("\n\nWhat Would You like to Find the Calculations For:");
     printf("\n\n<S>Shape(2D)");
     printf("\n<D>3D Objects");
     printf("\n<E>Exit Program");
     printf("\n\nOption selected:");
     scanf("%s", &option);

      switch(option)
      {
          case 'S':
          case 's':
                  shape();
          break;

          case 'D':
          case 'd':
                dimensional();
          break;

          case 'E':
          case 'e':
          exit(EXIT_SUCCESS);
          break;

          default:
                printf("Wrong option selected..\n");
                exit(EXIT_SUCCESS);
          break;
      }
    return sq.area; //neither here//
}

never mind, i have learnt my lesson.
i implemented structures along with this program and it returns values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.