Thread: Please Help! How do I calculate and print total Items in a Menu??

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    7

    Please Help! How do I calculate and print total Items in a Menu??

    I'm very new to programming. I'm currently taking Introduction to Programming and my assignment was to create a fictional restaurant with food items and such. I've gotten everything to work the way the spec requires except I can't quite figure out how to calculate and print the number of selections of each menu at the end of the program.

    Here's what I have so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define HAMBURGER 2.0
    #define CHEESEBURGER 2.25
    #define BURGER_BACON_MANIA 3.5
    #define FRIES 2.0
    #define CHEESE_FRIES 2.75
    #define SODA 2.0
    #define APPLE_CRISP_DELIGHT 3.0
    
    
    main() {
    	double totalPrice = 0;
    	int choice = 0;
    
    
    	printf("\nWelcome to Big Bangin' Burger Hut! How may I take your order?\n");
    	do {
    		//output the menu
    		printf("\nBBB Hut Menu\n");
    		printf("1. Hamburger             $%.2lf \n", HAMBURGER);
    		printf("2. Cheeseburger          $%.2lf \n", CHEESEBURGER);
    		printf("3. Burger Bacon Mania    $%.2lf \n", BURGER_BACON_MANIA);
    		printf("4. Fries                 $%.2lf \n", FRIES);
    		printf("5. Cheese Fries          $%.2lf \n", CHEESE_FRIES);
    		printf("6. Soda                  $%.2lf \n", SODA);
    		printf("7. Apple Crisp Delight   $%.2lf \n", HAMBURGER);
    
    
    		printf("\n8. Complete Order \n");
    		scanf_s("%i", &choice);
    
    
    		// take action based on choice
    		switch (choice) {
    		case 1:
    			totalPrice += HAMBURGER;
    			break;
    		case 2:
    			totalPrice += CHEESEBURGER;
    			break;
    		case 3:
    			totalPrice += BURGER_BACON_MANIA;
    			break;
    		case 4:
    			totalPrice += FRIES;
    			break;
    		case 5:
    			totalPrice += CHEESE_FRIES;
    			break;
    		case 6:
    			totalPrice += SODA;
    			break;
    		case 7:
    			totalPrice += APPLE_CRISP_DELIGHT;
    			break;
    		case 8:
    			printf("Thats: ")
    			break;
    		default:
    			printf("\nThe selection you have chosen is NOT on the menu. Please choose another item or complete this order. \n");
    			break;
    		}
    		printf("\nTotal so far: $%.2lf\n", totalPrice);
    	} while (choice != 8);
    	printf("\nBig Bangin Burger Hut would like to thank you for your order. Your order total is $%.2lf\n", totalPrice);
    	system("pause");
    }



    At the end, I have to calculate the number of selections of each of the items that the user has selected. So if the user selected 2 hamburgers, 1 cheese burger and a soda, I have to be able to make the program calculate and print:

    "That's: 2 Hamburgers
    1 Cheeseburger
    1 Soda

    (followed by)

    Your order total is___.

    How do I get the program to calculate and print the number of selections for the items? Please help!

    Thanks

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    You can define a separate variable for each food item and increment the variable in the corresponding case statement and at the completion of selection, if the variable value is greater than 0 then print the corresponding food item and variable count.

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    7
    Quote Originally Posted by Satya View Post
    You can define a separate variable for each food item and increment the variable in the corresponding case statement and at the completion of selection, if the variable value is greater than 0 then print the corresponding food item and variable count.
    I don't mean to sound very amateurish, but could you articulate that in a way where i may understand...or perhaps even code what you're trying to articulate so I can understand better?

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    It is a lonely Friday night, and my girlfriend is away at her friend's bachelorette party, so... although I should NOT have done this, here you go, gaze at this awesomeness (which I spent way too much time on), for your coding pleasure:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct MenuItem
    {
      char* name;
      float price; // units: U.S. dollars
    }
    MENU[] = {
      { "Hamburger", 2.00f },
      { "Cheeseburger", 2.25f },
      { "Burger Bacon Mania", 3.50f },
      { "Fries", 2.00f },
      { "Cheese Fries", 2.00f },
      { "Soda", 2.00f },
      { "Apple Crisp Delight", 3.00 },
    };
    
    #define NUM_MENU_ITEMS (sizeof(MENU)/sizeof(struct MenuItem))
    #define MAX_ITEM_NAME_LEN 20
    
    int main(void)
    {
      double total_price = 0.00f; 
      int num_item_order[NUM_MENU_ITEMS] = { 0 };
    
      printf("\nWelcome to Big Ass Bangin' Burgers (.)(.)!!! How may I take your order?\n");
      
      do
      {
        // print menu
        for (int i = 0; i < NUM_MENU_ITEMS; ++i)
        {
          printf("%d. %-*s $%.2f\n", i+1, MAX_ITEM_NAME_LEN, MENU[i].name, MENU[i].price);
        }
        printf("%d. Complete Order\n", NUM_MENU_ITEMS+1);
        
        int choice;
        
        // input menu selection
        if (1 != scanf("%d", &choice)) // a basic input validation test (if you enter a non-number, the program goes into an infinite loop -- I ignore that case here
        {
          printf("\nThe selection you have chosen is NOT on the menu. Please choose another item or complete this order.\n");
          continue;
        }
        
        if (choice == NUM_MENU_ITEMS+1) // user has selected to exit menu
          break;
        
        // add total and keep track of number of items ordered (note that total could be calculated later, since we know cost and # of items ordered per item)
        total_price += MENU[choice-1].price;
        ++num_item_order[choice-1];
      }
      while (1); // so-called "infinite" loop -- not really infinite since "break" statement above will get out
      
      printf("\nBig Ass Bangin' Burgers (.)(.)!!! would like to thank you for your order. Here is your reciept:\n\n");
      
      for (int i = 0; i < NUM_MENU_ITEMS; ++i)
      {
        printf("%d. %-*s %d\n", i+1, MAX_ITEM_NAME_LEN, MENU[i].name, num_item_order[i]);
      }
      printf("TOTAL: %.2f\n", total_price);
      
      return 0;
    }
    I did NOT write this code so that people can copy and paste and claim as their own work.. Please do NOT attempt this, for I am sure your Professor will see through it quickly.

    However, I DID write this code, so that newbs (such as the OP) can see how to write a program in a better style. You're welcome.

    A few things for OP to note about the difference between this program and original:

    1) The basic program is independent of the actual menu items or prices, these can easily be added by extending the MENU array, without modification to the rest of the program. Changing data > changing code, always, as data is usually stored in external files or database, and loaded at runtime.. but the basic algorithm remains the same. That is, if the data changes, the code should not have to be re-compiled. Imagine that the menu items are loaded from a text file, for example. In that case, we will not define MENU array in code, but will dynamically create array at run-time and load with data from external source.

    2) The user input is validated and the program prints an appropriate error message and continues gracefully, in case of invalid inputs. It should not go into an infinite loop (although I allow that here, for sake of simplicity), and not crash, or produce invalid results.
    Last edited by MacNilly; 07-16-2016 at 03:29 AM.

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    I see a bug in the above program that I just wrote..

    Code:
    if (choice == NUM_MENU_ITEMS+1) // user has selected to exit menu
          break;
    should be changed to

    Code:
    if (choice >= NUM_MENU_ITEMS+1) // user has selected to exit menu
          break;
    because if the user enters 100 for example, then it will result in an invalid array index access later on.. Actually it would be better to break iff choice is equal to NUM_MENU_ITEMS+1, otherwise, if choice is not in the set {1 ... NUM_MENU_ITEMS} print an error message and continue the loop.
    Last edited by MacNilly; 07-16-2016 at 05:54 AM.

  6. #6
    Registered User
    Join Date
    Jul 2016
    Posts
    7
    Thanks. I will try to figure out my solution based on the code you've written!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Links to menu items
    By Olidivera in forum Windows Programming
    Replies: 1
    Last Post: 05-29-2005, 11:38 PM
  2. Replies: 0
    Last Post: 07-07-2004, 10:18 AM
  3. Checkmarked menu items
    By -leech- in forum Windows Programming
    Replies: 3
    Last Post: 02-25-2002, 05:21 PM
  4. Enabling menu items
    By chomper in forum Windows Programming
    Replies: 2
    Last Post: 02-20-2002, 08:43 PM

Tags for this Thread