Thread: [HELP] Menu using a while loop.

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    [HELP] Menu using a while loop.

    Hello all i am new to the programming world and i have come accross a problem. i am trying to use a menu and repeat it after i have collected input from the user.


    Here is the code right now. When running it, the outcomes of the first step is correct. But when t choose from the first menu it givs me only what i made choice 1 to be for all choices. and then stops.
    i would like it to take in input from the user and repeat the main menu again.

    any help is apriciated

    Code:
    int main() {
    
    
    int choice=1, type_don=0, type_req=0, choice_don=1, choice_req;
    
    
    printf("Welcome to the Food Bank Management Program!\n\n");
    
    
    
    
    printf("What would you like to do?\n");
    printf("\t 1. Enter a Donation\n");
    printf("\t 2. Enter a Request\n");
    printf("\t 3. Fullfill Request\n");
    printf("\t 4. Print status report\n");
    printf("\t 5. Exit\n");
    scanf("&d", &choice);
    
    
    while (choice != 2) {{
    
    
    if (choice == 1) {
    printf("What donation type would you like to make\n");
    printf("0. Protein\n");
    printf("1. Dairy\n");
    printf("2. Grains\n");
    printf("3. Vegetables\n");
    printf("4. Fruits\n");
    scanf("%d", &type_don);
    
    
    for (choice_don != 2) {
    
    
    int don_list[5], don_pro, don_dai, don_gra, don_veg, don_fru;
    
    
    if (choice_don == 1) {
    printf("How many would you like to enter?\n");
    scanf("%d", don_pro);
    
    
    don_pro = don_list[1];
    }
    
    
    else if (choice_don == 2) {
    printf("How many would you like to enter?\n");
    scanf("&d", don_dai);
    don_dai = don_list[2];
    }
    
    
    else if (choice_don == 3) {
    printf("How many would you like to enter?\n");
    scanf("%d", don_gra);
    don_gra = don_list[3];
    }
    
    
    else if (choice_don == 4) {
    printf("How many would you like to enter?\n");
    scanf("%d", don_veg);
    don_veg = don_list[4];
    }
    
    
    else if (choice_don == 5)
    printf("How many would you like to enter?\n");
    scanf("%d", don_fru);
    don_fru = don_list[5];
    
    
    }}
    
    
    else if (choice == 2) {
    
    
    printf("What would you like to request?\n");
    printf("0. Protein\n");
    printf("1. Dairy\n");
    printf("2. Grains\n");
    printf("3. Vegetables\n");
    printf("4. Fruits\n");
    scanf("%d", &type_req);
    
    
    while (choice_req !=5) {
    
    
    int req_list[5], req_pro, req_dai, req_gra, req_veg, req_fru;
    
    
    if (choice == 1) {
    printf("How many would you like to enter?\n");
    scanf("%d", req_pro);
    
    
    req_pro = req_list[1];
    }
    
    
    else if (choice == 2) {
    printf("How many would you like to enter?\n");
    scanf("&d", req_dai);
    req_dai = req_list[2];
    }
    
    
    else if (choice == 3) {
    printf("How many would you like to enter?\n");
    scanf("%d", req_gra);
    req_gra = req_list[3];
    }
    
    
    else if (choice == 4) {
    printf("How many would you like to enter?\n");
    scanf("%d", req_veg);
    req_veg = req_list[4];
    }
    
    
    else if (choice == 5) {
    printf("How many would you like to enter?\n");
    scanf("%d", req_fru);
    req_fru = req_list[5];
    }}}
    
    
    else if (choice == 3) {
    printf("no");
    }
    
    
    else if (choice == 4) {
    printf("yes");
    }
    
    
    else if (choice == 5) {
    printf("Thank you for running our system!");
    }
    }}
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is your problem: {{

    And this is why you should make your indentation style, strictly one of the conforming styles. When you reformat your code, you'll see the problem, right away.

    Either do it by hand, or d/l the program "astyle.exe", and it will automatically do it for you. Also, many IDE's will do it, as well.

    And hereafter, indent properly, and no {{'s. or }}'s, and no "flat"
    Code:
    if (choice == 1) {
    printf("What donation type would you like to make\n");
    printf("0. Protein\n");
    printf("1. Dairy\n");
    printf("2. Grains\n");
    printf("3. Vegetables\n");
    printf("4. Fruits\n");
    scanf("%d", &type_don);
    either.

    Code:
    if (choice == 1) {
       printf("What donation type would you like to make\n");
       printf("0. Protein\n");
       printf("1. Dairy\n");
       printf("2. Grains\n");
       printf("3. Vegetables\n");
       printf("4. Fruits\n");
       scanf("%d", &type_don);
    }
    Last edited by Adak; 10-26-2012 at 10:15 PM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Code:
    }}}
    Okay stop, that's not how to program! This is like messing up the order of all the books in one section of a library and then asking us to find a particular book. You should never be programming like this. You might think you know what the scope of each pair of braces is, but you've probably gotten it wrong and I damn sure aren't even going to try and follow code that's this messed up.
    Just wait until you write a large program, doing it like this would be like randomly rearranging every book in the library. You'd never find anything.

    You must indent your code properly. It is not an "optional" thing, an "I'll do it later", "this is just a draft" or any other such thing. There is no excuse. All code must be properly formatted at all times during development. Anything less, and you're not really a programmer.
    Use google to look up how to do proper indentation.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    whats wrong with my code. why wont it repeat correctly?

    Code:
    int main() {
    
        int choice=1, type_don=0, type_req=0, choice_don=1, choice_req;
    
    
        printf("Welcome to the Food Bank Management Program!\n\n");
    
    
        while (choice != 5) {
            printf("What would you like to do?\n");
            printf("\t 1. Enter a Donation\n");
            printf("\t 2. Enter a Request\n");
            printf("\t 3. Fullfill Request\n");
            printf("\t 4. Print status report\n");
            printf("\t 5. Exit\n");
            scanf("&d", &choice);
    
    
                if (choice == 1) {
    
    
                    int don_list[5], don_pro, don_dai, don_gra, don_veg, don_fru;
    
    
                    while (choice_don != 5) {
    
    
                        printf("What donation type would you like to make\n");
                        printf("0. Protein\n");
                        printf("1. Dairy\n");
                        printf("2. Grains\n");
                        printf("3. Vegetables\n");
                        printf("4. Fruits\n");
                        scanf("%d", type_don);
    
    
                        if (choice_don == 1) {
                            printf("How many would you like to enter?\n");
                            scanf("%d", don_pro);
                            don_pro = don_list[1];
                        }
    
    
                        else if (choice_don == 2) {
                            printf("How many would you like to enter?\n");
                            scanf("&d", don_dai);
                            don_dai = don_list[2];
                        }
    
    
                        else if (choice_don == 3) {
                            printf("How many would you like to enter?\n");
                            scanf("%d", don_gra);
                            don_gra = don_list[3];
                        }
    
    
                        else if (choice_don == 4) {
                            printf("How many would you like to enter?\n");
                            scanf("%d", don_veg);
                            don_veg = don_list[4];
                        }
    
    
                        else if (choice_don == 5) {
                            printf("How many would you like to enter?\n");
                            scanf("%d", don_fru);
                            don_fru = don_list[5];
                        }
                    }}
    
    
    
    
    
    
                else if (choice == 2) {
    
    
                    int req_list[5], req_pro, req_dai, req_gra, req_veg, req_fru;
    
    
                    printf("What would you like to request?\n");
                    printf("0. Protein\n");
                    printf("1. Dairy\n");
                    printf("2. Grains\n");
                    printf("3. Vegetables\n");
                    printf("4. Fruits\n");
                    scanf("%d", &type_req);
    
    
    
    
    
    
                    if (choice == 0) {
                        printf("How many would you like to enter?\n");
                        scanf("%d", req_pro);
                        req_pro = req_list[1];
                    }
    
    
                    else if (choice == 1) {
                        printf("How many would you like to enter?\n");
                        scanf("&d", req_dai);
                        req_dai = req_list[2];
                    }
    
    
                    else if (choice == 2) {
                        printf("How many would you like to enter?\n");
                        scanf("%d", req_gra);
                        req_gra = req_list[3];
                    }
    
    
                    else if (choice == 3) {
                        printf("How many would you like to enter?\n");
                        scanf("%d", req_veg);
                        req_veg = req_list[4];
                    }
    
    
                    else if (choice == 4) {
                        printf("How many would you like to enter?\n");
                        scanf("%d", req_fru);
                        req_fru = req_list[5];
                    }
                }
    
    
                else if (choice == 3) {
                    printf("no");
                }
    
    
                else if (choice == 4) {
                    printf("yes");
                }
    
    
                else if (choice == 5) {
                    printf("Thank you for running our system!");
                }
            }
    
    
            return 0;
    }

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    What is it doing? What do you want it to do?
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your inner while loop is probably the problem.

    The way menus should work is:

    Code:
    while(TestForEndCondition) {
    
        print out menu
       get user choice
       switch statement with 1 case for every menu option + one default
       for handling wrong input
    
       If there is much that you want done in any switch case, you want
       to have it done in a separate function. Otherwise your switch code
       can become enormous.
    
       But it's way easier if you use the switch statement, instead of
       nested while loops, in your menu. Use a for loop if you need to,
       because it offers more logic control, built in to it.
    
    }
    The switch statement also offers built in logic control, making it a great choice, instead of lots of if's and else if's and else's. Those can be confusing. Comment what they do, if you must use them.
    Last edited by Adak; 10-28-2012 at 09:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop in menu help!!
    By Jason Singh in forum C Programming
    Replies: 5
    Last Post: 05-02-2011, 07:38 AM
  2. loop in menu
    By aama100 in forum C++ Programming
    Replies: 1
    Last Post: 01-23-2008, 11:56 PM
  3. Menu Loop
    By alleycat in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2006, 05:06 AM
  4. program will not stay in menu loop
    By StevenGarcia in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 05:17 PM
  5. Help me in while loop with case menu
    By playboy1620 in forum C Programming
    Replies: 1
    Last Post: 02-20-2002, 11:07 PM