Thread: How to go back from switch menu

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    69

    How to go back from switch menu

    I want to make a menu that classifies animals into insects, birds, mammals and fishes with specific characteristics for each of these (using composite variables). I thought I'd use a nested switch case(one switch for the category of the animals and another switch for each category to perform the actions). The problem is, after I've entered a new entry for instance, I wanna have the option to go back and pick another action or even go back and pick another category. How can I do that? I tried with a do-while but I'm just getting an endless loop.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    typedef struct{
        unsigned int noLegs, lifeSpan;
        char *name;
    }insect;
    
    
    
    
    typedef struct{
        float speed, length;
        char *habits, *name;
    }bird;
    
    
    
    
    typedef struct{
        float weight, height;
        char *food, *name;
    }mammal;
    
    
    
    
    typedef struct{
        float weight, depth, salt;
        char *name;
    }fish;
    
    
    
    
    int main(int argc, char **argv)
    {
        char choice, action;
        insect *i = (insect *)malloc(sizeof(insect));
    
    
        if(!i)
        {
            printf("Failed to allocate memory.\n");
            return -1;
        }
    
    
        printf("Please choose a category:\n");
        printf("1. Insects\n2. Birds\n3. Mammals\n4. Fishes\n");
        scanf("%c", &choice);
    
    
        switch(choice)
        {
            case '1':
                printf("You've chosen: Insects\n");
                printf("Please choose an action:\n");
                printf("a. Add entry\nb. Delete entry\nc. Replace entry\nd. Lookup entries\ne. Back\n");
                scanf(" %c", &action);
    
    
                switch(action)
                {
                    case 'a':
                        printf("What entry would you like to add?\n");
                        i->name = (char *)malloc(sizeof(char));
                        scanf("%s", i->name);
                        printf("How many legs does a %s have?\n", i->name);
                        scanf("%u", &(i->noLegs));
                        printf("What is the lifespan of a %s?\n", i->name);
                        scanf("%u", &(i->lifeSpan));
                        break;
                    case 'b':
                        printf("Which entry would you like to delete?\n");
                        break;
                    case 'c':
                        printf("Which entry would you like to replace?\n");
                        break;
                    case 'd':
                        printf("The current entries:\n");
                        break;
                    case 'e':
                        printf("Back\n");
                        break;
                    default:
                        printf("Invalid action.\n");
                }
                break;
            case '2':
                printf("You've chosen: Birds\n");
                printf("Please choose an action:\n");
                printf("a. Add entry\nb. Delete entry\nc. Replace entry\nd. Lookup entries\n");
                scanf(" %c", &action);
                switch(action)
                {
                    case 'a':
                        printf("What entry would you like to add?\n");
                        break;
                    case 'b':
                        printf("Which entry would you like to delete?\n");
                        break;
                    case 'c':
                        printf("Which entry would you like to replace?\n");
                        break;
                    case 'd':
                        printf("The current entries:\n");
                        break;
                    default:
                        printf("Invalid action.\n");
                }
                break;
            case '3':
                printf("You've chosen: Mammals\n");
                printf("Please choose an action:\n");
                printf("a. Add entry\nb. Delete entry\nc. Replace entry\nd. Lookup entries\n");
                scanf(" %c", &action);
                switch(action)
                {
                    case 'a':
                        printf("What entry would you like to add?\n");
                        break;
                    case 'b':
                        printf("Which entry would you like to delete?\n");
                        break;
                    case 'c':
                        printf("Which entry would you like to replace?\n");
                        break;
                    case 'd':
                        printf("The current entries:\n");
                        break;
                    default:
                        printf("Invalid action.\n");
                }
                break;
            case '4':
                printf("You've chosen: Fishes\n");
                printf("Please choose an action:\n");
                printf("a. Add entry\nb. Delete entry\nc. Replace entry\nd. Lookup entries\n");
                scanf(" %c", &action);
                switch(action)
                {
                    case 'a':
                        printf("What entry would you like to add?\n");
                        break;
                    case 'b':
                        printf("Which entry would you like to delete?\n");
                        break;
                    case 'c':
                        printf("Which entry would you like to replace?\n");
                        break;
                    case 'd':
                        printf("The current entries:\n");
                        break;
                    default:
                        printf("Invalid action.\n");
                }
                break;
            default:
                printf("Invalid choice.\n");
        }
        return 0;
    }
    Last edited by rmmstn; 02-25-2021 at 09:37 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I would suggest you do
    Code:
    case '1':
      doInsects();
      break;
    case '2':
      doBirds();
      break;
    case '3':
      doMammals();
      break;
    case '4':
      doFishes();
      break;
    Likewise, your add / delete / replace would also be function calls within those inner switches.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch Menu help
    By tlxxxsracer in forum C Programming
    Replies: 14
    Last Post: 12-09-2015, 10:34 PM
  2. Menu Switch Problem
    By software tester in forum Game Programming
    Replies: 2
    Last Post: 02-02-2011, 09:20 AM
  3. How to go back to menu
    By sd03eee in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2005, 06:54 PM
  4. menu to switch between OS's
    By DavidP in forum Tech Board
    Replies: 3
    Last Post: 12-22-2002, 02:24 AM
  5. Going back to a main menu from anywhere
    By Gades in forum C Programming
    Replies: 5
    Last Post: 11-13-2001, 04:22 PM

Tags for this Thread