Thread: So I'm trying to make menu using do - while

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    19

    Angry So I'm trying to make menu using do - while

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main ()
    {
        int chicken = 5; int amountOfChicken;
        int fish = 3; int amountOfFish;
        int eggs = 1; int amountOfEggs;
        int fries = 4; int amountOfFries;
        int rice = 1; int amountOfRice;
        int amount;
        char choice; char choice2;
    
    
        printf("The Menu is the one below and type 0 to stop.\n");
        printf("[A]Chicken = $5   [B]Fish = $3   [C]Eggs = $1   [D]Fries = $4  [E]Rice = $1\n\n");
    
    
        do {
    
    
    
    
            printf("Enter the letter for the food you want: ");
            scanf(" %c", &choice);
            if (choice == 'A' || choice == 'a') {
                printf("You chose Chicken. How many do you want?\n");
                printf("I want: ");
                scanf(" %d", &amountOfChicken);
                chicken *= amountOfChicken;
                printf("That will be %d", chicken);
                break;
            } else if (choice == 'B' ||choice ==  'b') {
                printf("You chose Fish. How many do you want?\n");
                printf("I want: ");
                scanf(" %d", &amountOfFish);
                fish *= amountOfFish;
                printf("That will be %d", fish);
                break;
            } else if (choice == 'C' ||choice ==  'c') {
                printf("You chose Eggs. How many do you want?\n");
                printf("I want: ");
                scanf(" %d", &amountOfEggs);
                eggs *= amountOfEggs;
                printf("That will be %d", eggs);
                break;
            } else if (choice == 'D' ||choice ==  'd') {
                printf("You chose Fries. How many do you want?\n");
                printf("I want: ");
                scanf(" %d", &amountOfFries);
                fries *= amountOfFries;
                printf("That will be %d", fish);
                break;
            } else if (choice == 'E' ||choice ==  'e') {
                printf("You chose Fish. How many do you want?\n");
                printf("I want: ");
                scanf(" %d", &amountOfRice);
                rice *= amountOfRice;
                printf("That will be %d", rice);
                break;
            } else {
                printf("Error you typed wrong!");
            }
            printf ("Would you like to add more? Y/N \n");
            scanf (" %c", &choice2);
    
    
            if (choice2 == 'y' ||choice2 ==  'Y') {
                printf ("Choose which one to add again: \n\n [A]Chicken = $5   [B]Fish = $3   [C]Eggs = $1   [D]Fries = $4  [E]Rice = $1\n\n");
            } else {
                break;
            }
        } while (choice2 != 'N' ||choice2 == 'n');
    
    
    
    
    
    
        return 0;
    }
    Something is wrong. It doesn't run the way I want it too. When I type 'B' it says "You chose chicken" which is the case 'A' and I don't know why.

  2. #2
    Registered User
    Join Date
    Sep 2015
    Posts
    7
    It works on my computer. I Type 'B', it's asking for fish.

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    7
    Avoid the break instruction in your if/else blocks. Break makes you leave the loop.

  4. #4
    Registered User
    Join Date
    Jul 2015
    Posts
    19
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main ()
    {
        int chicken = 5; int amountOfChicken;
        int fish = 3; int amountOfFish;
        int eggs = 1; int amountOfEggs;
        int fries = 4; int amountOfFries;
        int rice = 1; int amountOfRice;
        int amount;
        char choice; char choice2;
    
    
        printf("The Menu is the one below and type 0 to stop.\n");
        printf("[A]Chicken = $5   [B]Fish = $3   [C]Eggs = $1   [D]Fries = $4  [E]Rice = $1\n\n");
    
    
        do {
    
    
    
    
            printf("Enter the letter for the food you want: ");
            scanf(" %c", &choice);
            if (choice == 'A' || choice == 'a') {
                printf("You chose Chicken. How many do you want?\n");
                printf("I want: ");
                scanf(" %d", &amountOfChicken);
                amountOfChicken *= chicken;
                printf("That will be %d\n\n", amountOfChicken);
    
    
            } else if (choice == 'B' ||choice ==  'b') {
                printf("You chose Fish. How many do you want?\n");
                printf("I want: ");
                scanf(" %d", &amountOfFish);
                amountOfFish *= fish;
                printf("That will be %d\n\n", amountOfFish);
    
    
            } else if (choice == 'C' ||choice ==  'c') {
                printf("You chose Eggs. How many do you want?\n");
                printf("I want: ");
                scanf(" %d", &amountOfEggs);
                amountOfEggs *= eggs;
                printf("That will be %d\n\n", amountOfEggs);
    
    
            } else if (choice == 'D' ||choice ==  'd') {
                printf("You chose Fries. How many do you want?\n");
                printf("I want: ");
                scanf(" %d", &amountOfFries);
                amountOfFries *= fries;
                printf("That will be %d\n\n", fish);
    
    
            } else if (choice == 'E' ||choice ==  'e') {
                printf("You chose Rice. How many do you want?\n");
                printf("I want: ");
                scanf(" %d", &amountOfRice);
                amountOfRice *= rice;
                printf("That will be %d\n\n", amountOfRice);
    
    
            } else {
                printf("Error you typed wrong!\n\n");
                break;
            }
            if (choice == 'A' || choice == 'a' || choice == 'B' || choice == 'b' || choice == 'C' || choice == 'c' || choice == 'D' || choice == 'd' || choice == 'E' || choice == 'e') {
            printf ("Would you like to add more? Y/N\n");
            scanf (" %c", &choice2);
            }
    
    
            if (choice2 == 'y' ||choice2 ==  'Y') {
                printf ("Choose which one to add again: \n\n [A]Chicken = $5   [B]Fish = $3   [C]Eggs = $1   [D]Fries = $4  [E]Rice = $1\n\n");
            } else {
                break;
            }
        } while (choice2 != 'N' ||choice2 == 'n');
        if (choice == 'A' || choice == 'a' || choice == 'B' || choice == 'b' || choice == 'C' || choice == 'c' || choice == 'D' || choice == 'd' || choice == 'E' || choice == 'e') {
        amount = amountOfChicken + amountOfEggs + amountOfFish + amountOfFries + amountOfRice;
        printf ("The total is: $%d", amount);
        }
    
    
    
    
        return 0;
    }
    Okay this is my updated code. Now my problem is the amount and thanks Shadew. So that's why it terminates always. XD

  5. #5
    Registered User
    Join Date
    Jul 2015
    Posts
    19
    Oh nevermind the problem is the variables amountOfChicken and other ........s like that it should be :
    Code:
    int amountOfChicken = 0;
    I should assign them the value 0 XD okay my bad as newbie.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make a simple menu
    By Sharie in forum C++ Programming
    Replies: 5
    Last Post: 04-19-2010, 10:06 AM
  2. How to make a menu on C? DOS
    By luisvalencia in forum C Programming
    Replies: 19
    Last Post: 04-10-2005, 09:31 PM
  3. How do I make a Window Menu?
    By Gamemaniac in forum Windows Programming
    Replies: 7
    Last Post: 02-28-2005, 11:23 AM
  4. How to make a right-click menu ?
    By skywing in forum C++ Programming
    Replies: 4
    Last Post: 01-14-2005, 03:32 PM
  5. Trying to make a menu screen
    By circle in forum C Programming
    Replies: 0
    Last Post: 12-07-2004, 06:06 AM

Tags for this Thread