Andrei0408
Andrei0408

Reputation: 197

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.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.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;
    bool running = true;
    bool returnBack = true;
    insect *i = (insect *)malloc(sizeof(insect));
    if(!i)
    {
        printf("Failed to allocate memory.\n");
        return -1;
    }
    while(running)
    {
        printf("Please choose a category:\n");
        printf("1. Insects\n2. Birds\n3. Mammals\n4. Fishes\n5. Exit\n");
        scanf(" %c", &choice);
        switch(choice)
        {
            case '1':
                printf("You've chosen: Insects\n");
                while(returnBack)
                {
                    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("Which entry would you like to replace?\n");
                            break;
                        case 'e':
                            returnBack = false;
                            break;
                        default:
                            printf("Invalid action.\n");
                    }
                }
                break;
            case '2':
                printf("You've chosen: Birds\n");
                break;
            case '3':
                printf("You've chosen: Mammals\n");
                break;
            case '4':
                printf("You've chosen: Fishes\n");
                break;
            case '5':
                running = false;
                break;
            default:
                printf("Invalid choice.\n");
        }
    }
    return 0;
}

Upvotes: 1

Views: 81

Answers (1)

Jack Lilhammers
Jack Lilhammers

Reputation: 1247

As said by alex01011, you just need a while over the whole switch
When the user selects 5 running becomes FALSE and the loop exits.

One thing though, you forgot to clear the whitespace in the first scanf()

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

#define TRUE  (1==1)
#define FALSE (!TRUE)

/* ...
 * ALL THE STRUCTS
 */

int main(int argc, char **argv)
{
    char choice, action, running = TRUE;
    insect *i = (insect *)malloc(sizeof(insect));

    // CHECK ON MALLOC RETURN

    while(running)
    {
        printf("Please choose a category:\n");
        printf("1. Insects\n2. Birds\n3. Mammals\n4. Fishes\n5. Exit\n\n");
        scanf(" %c", &choice); // <-- The leading space was missing
        
    
        switch(choice)
        {
            /* ...
             * PREVIOUS CASES
             */
            case '5':
                running = FALSE;
                break;
                
            default:
                printf("Invalid choice.\n");
        }
    }

    return 0;
}

Upvotes: 1

Related Questions