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.