i develop a database program in c language there the four main contents which are access by using switch statement the each content further has other option which also access by switch but the problem isi n these subcontents last option which is exit. on enter the choice number it exit from whole program iwant it exit and open main content page did any body help me?

Dani AI

Generated

Most likely cause: the submenu code calls exit() or returns from main, or you expect break to jump out of multiple levels. In C a break only leaves the innermost switch or loop, and exit()/return will terminate the whole program. To return to the main menu you need the submenu to end and control to go back to the main menu loop — either by returning from a submenu function or by setting a flag that breaks the submenu loop.

A simple, robust pattern is: keep the main menu in a loop, implement each submenu as a function that loops until the user chooses “back”, and have that function return to the caller. Avoid exit() inside submenus. Example structure:

#include <stdio.h>

void submenu(void) {
    int ch;
    while (1) {
        puts("Submenu: 1=do  0=back");
        if (scanf("%d", &ch) != 1) { while (getchar() != '\n'); continue; }
        switch (ch) {
            case 1: puts("Do something"); break;
            case 0: return; /* return to main menu */
            default: puts("Invalid");
        }
    }
}

int main(void) {
    int ch;
    while (1) {
        puts("Main menu: 1=submenu  0=exit");
        if (scanf("%d", &ch) != 1) { while (getchar() != '\n'); continue; }
        switch (ch) {
            case 1: submenu(); break;
            case 0: return 0;
            default: puts("Invalid");
        }
    }
}

Troubleshooting tips: if behavior still exits, search for exit(), return statements inside main, or misplaced breaks. Use functions for clarity, or a boolean flag to break outer loops. As advised, when asking for help post a minimal reproducible snippet and any compiler/runtime messages. Also follow ’s naming advice and the guideline pointed to about how to ask clearly so helpers can reproduce the problem.

Recommended Answers

All 3 Replies

Give appropriate names to your threads

Welcome aboard. If you have specific problems when trying to do a program...please post the source code here along with what you think may be wrong. Also post the errors you get, so we can take a look and help you. We are not in the business of doing your home-work for you! You must TRY/show some effort!!

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.