Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

How to change these arrays to Dynamic allocations

mikexstiffler

New Coder
Code:
//Stage D

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
typedef enum
{ // declaring enum with assigning the prices as its integer constants
    Chicken = 15,
    Ham = 17,
    Vegie = 13
} BurgerTypes;
typedef struct
{ //delcaring struct for burger details
    int quantity;           
    BurgerTypes burgerType; 
}burger;
typedef struct
{ // declaring struct for order details
    char receiptNo[6];                 
    float totalPrice;                   
    burger burgers;    //nested structure     
} order;

int i = 1, counter = 0;
order *orderArray=(order*)malloc(sizeof(order)); // declaring burger array with data type burger


void billAppend();
float calculateDiscount(float total, int quantity);
float calculateDiscount(float total, int quantity);
float calculateTotalPriceBurgers(char userChoice, int number);
float calculateTotalPriceMeals(char userChoice, int number);
float calcTotalRev(int index);

float totalRevenue = 0;

int main()
{
    int order = 0; // to store user's order type
    int amount;
    char choice; // to get burger choice
    int noOfSingleBurger = 0;
    bool addMore = true;
    char addChoice;

    while (order != 7)
    {
        printf("\n\nWelcome to Tasty Burgers\n\n");

        printf("1. Order Meals\n");
        printf("2. Order Burgers\n");
        printf("3. Order Chips\n");
        printf("4. Order Cold Drinks\n");
        printf("5. Process Order\n");
        printf("6. Cancel\n");
        printf("7. Exit\n");
        printf("\nEnter your option : ");
        scanf("%d", &order);

        addMore = true;

        if (order == 1) // function related to ordering meals
        {
            if (counter < 25)
            { // Gets input only if array is not full(<25).If not then error msg given in else condition.

                while (addMore)
                {
                    printf("\nSelect the Meal Type\n\n");
                    printf("A - Big Chicken Meal - 20$\n");
                    printf("B - Big Ham Meal - 22$\n");
                    printf("C - Big Vegie Meal - 18$\n");
                    printf("X - To return Main Menu\n");

                    // prompting and input of burger type
                    printf("\nSelect your burger type: ");
                    scanf(" %c", &choice);

                    if (choice == 'A' || choice == 'a')
                    {
                        BurgerTypes price = Chicken;
                        orderArray[counter].burgers.burgerType = 1;
                        printf("You have selected Chicken Meal. How many meals you wish you order? : ");
                        scanf("%d", &amount);

                        orderArray[counter].totalPrice = orderArray[counter].totalPrice + calculateTotalPriceMeals(choice, amount);

                        printf("Your current bill value is $%.2f\n", orderArray[counter].totalPrice);

                        printf("Press “Y” to add more meals into your order or Press “X” to go back to previous menu : ");
                        scanf(" %c", &addChoice);

                        printf("\n");

                        if (addChoice == 'Y' || addChoice == 'y')
                        {
                            continue;
                        }
                        else if (addChoice == 'X' || addChoice == 'x')
                        {
                            // go out from the current loop and generate the bill
                            addMore = false;
                        }
                    }
                    else if (choice == 'B' || choice == 'b')
                    {
                        BurgerTypes price = Ham;
                        orderArray[counter].burgers.burgerType = 1;
                        printf("You have selected Ham Meal. How many meals you wish you order? : ");
                        scanf("%d", &amount);

                        orderArray[counter].totalPrice = orderArray[counter].totalPrice + calculateTotalPriceMeals(choice, amount);
                        orderArray[counter].burgers.quantity = orderArray[counter].burgers.quantity + amount;

                        printf("Your current bill value is $%.2f\n", orderArray[counter].totalPrice);

                        printf("Press “Y” to add more meals into your order or Press “X” to process your order now : ");
                        scanf(" %c", &addChoice);

                        if (addChoice == 'Y' || addChoice == 'y')
                        {
                            continue;
                        }
                        else if (addChoice == 'X' || addChoice == 'x')
                        {
                            // go out from the current loop and generate the bill
                            addMore = false;
                        }
                    }
                    else if (choice == 'C' || choice == 'c')
                    {
                        BurgerTypes price = Chicken;
                        orderArray[counter].burgers.burgerType = 1;
                        printf("You have selected Vegie Meal. How many meals you wish you order? : ");
                        scanf("%d", &amount);

                        orderArray[counter].totalPrice = orderArray[counter].totalPrice + calculateTotalPriceMeals(choice, amount);
                        orderArray[counter].burgers.quantity = orderArray[counter].burgers.quantity + amount;

                        printf("Your current bill value is $%.2f\n", orderArray[counter].totalPrice);

                        printf("Press “Y” to add more burgers into your order or Press “X” to process your order now : ");
                        scanf(" %c", &addChoice);

                        if (addChoice == 'Y' || addChoice == 'y')
                        {
                            continue;
                        }
                        else if (addChoice == 'X' || addChoice == 'x')
                        {
                            // go out from the current loop and generate the bill
                            choice = 'X';
                            addMore = false;
                        }
                    }
                    else if (choice == 'X' || choice == 'x')
                        break; // exits from the current while loop
                }
                if (choice == 'X' || choice == 'x')
                    continue;
                ; // goes back to the beginning of loop if x is entered
            }
            else
            {
                printf("Orders queue is Full (Maximum 25 orders can be served at a time)");
                printf("\n<<Press Any Key to show the main meu >>\n\n"); // message given showing 25 orders are there and its full.
            }
        }
        else if (order == 2) // function related to the ordering burgers
        {                    // if option 1 is selected
            if (counter < 25)
            { // Gets input only if array is not full(<25).If not then error msg given in else condition.

                while (addMore)
                {
                    printf("\nSelect the Burger Type\n\n");
                    printf("A - Chicken Burger - 15$\nB - Ham Burger - 17$\nC - Vegie Burger - 13$\nX - To return to Main Menu\n"); // printing the menu of available burger types

                    // prompting and input of burger type
                    printf("\nSelect your burger type: ");
                    scanf(" %c", &choice);

                    if (choice == 'A' || choice == 'a')
                    {
                        BurgerTypes price = Chicken;
                        orderArray[counter].burgers.burgerType = 1;
                        printf("You have selected Chicken Burger. How many burgers you wish you order? : ");
                        scanf("%d", &amount);

                        orderArray[counter].totalPrice = orderArray[counter].totalPrice + calculateTotalPriceBurgers(choice, amount);
                        orderArray[counter].burgers.quantity = orderArray[counter].burgers.quantity + amount;

                        printf("Your current bill value is $%.2f\n", orderArray[counter].totalPrice);

                        printf("Press \“Y\” to add more burgers into your order or Press \“N\” to process your order now : ");
                        scanf(" %c", &addChoice);

                        printf("\n");

                        if (addChoice == 'Y' || addChoice == 'y')
                        {
                            continue;
                        }
                        else if (addChoice == 'N' || addChoice == 'n')
                        {
                            // go out from the current loop and generate the bill
                            addMore = false;
                        }
                    }
                    else if (choice == 'B' || choice == 'b')
                    {
                        BurgerTypes price = Ham;
                        orderArray[counter].burgers.burgerType = 1;
                        printf("You have selected Ham Burger. How many burgers you wish you order? : ");
                        scanf("%d", &amount);

                        orderArray[counter].totalPrice = orderArray[counter].totalPrice + calculateTotalPriceBurgers(choice, amount);
                        orderArray[counter].burgers.quantity = orderArray[counter].burgers.quantity + amount;

                        printf("Your current bill value is $%.2f\n", orderArray[counter].totalPrice);

                        printf("Press \“Y\” to add more burgers into your order or Press \“N\” to process your order now : ");
                        scanf(" %c", &addChoice);

                        if (addChoice == 'Y' || addChoice == 'y')
                        {
                            continue;
                        }
                        else if (addChoice == 'N' || addChoice == 'n')
                        {
                            // go out from the current loop and generate the bill
                            addMore = false;
                        }
                    }
                    else if (choice == 'C' || choice == 'c')
                    {
                        BurgerTypes price = Chicken;
                        orderArray[counter].burgers.burgerType = 1;
                        printf("You have selected Vegie Burger. How many burgers you wish you order? : ");
                        scanf("%d", &amount);

                        orderArray[counter].totalPrice = orderArray[counter].totalPrice + calculateTotalPriceBurgers(choice, amount);
                        orderArray[counter].burgers.quantity = orderArray[counter].burgers.quantity + amount;

                        printf("Your current bill value is $%.2f\n", orderArray[counter].totalPrice);

                        printf("Press \“Y\” to add more burgers into your order or Press \“N\” to process your order now : ");
                        scanf(" %c", &addChoice);

                        if (addChoice == 'Y' || addChoice == 'y')
                        {
                            continue;
                        }
                        else if (addChoice == 'N' || addChoice == 'n')
                        {
                            // go out from the current loop and generate the bill
                            addMore = false;
                        }
                    }
                    else if (choice == 'X' || choice == 'x')
                        break; // exits from the current while loop
                }
                if (choice == 'X' || choice == 'x')
                    continue;
                ; // goes back to the beginning of loop if x is entered
            }
            else
            {
                printf("Orders queue is Full (Maximum 25 orders can be served at a time)");
                printf("\n<<Press Any Key to show the main meu >>\n\n"); // message given showing 25 orders are there and its full.
            }
        }
        else if (order == 3) // function related to ordering chips
        {
            if (counter < 25)
            { // Gets input only if array is not full(<25).If not then error msg given in else condition.

                printf("You have selected Chips. How many burgers you wish you order? : ");
                scanf("%d", &amount);

                orderArray[counter].totalPrice = orderArray[counter].totalPrice + (amount * 5);

                printf("Your current bill value is $%.2f\n", orderArray[counter].totalPrice);

                continue;
            }
            else
            {
                printf("Orders queue is Full (Maximum 25 orders can be served at a time)");
                printf("\n<<Press Any Key to show the main meu >>\n\n"); // message given showing 25 orders are there and its full.
            }
        }
        else if (order == 4) // function related to ordering chips
        {
            if (counter < 25)
            { // Gets input only if array is not full(<25).If not then error msg given in else condition.

                printf("You have selected Cold Drinks. How many Cold Drinks you wish you order? : ");
                scanf("%d", &amount);

                orderArray[counter].totalPrice = orderArray[counter].totalPrice + (amount * 3.50);

                printf("Your current bill value is $%.2f\n", orderArray[counter].totalPrice);

                continue;
            }
            else
            {
                printf("Orders queue is Full (Maximum 25 orders can be served at a time)");
                printf("\n<<Press Any Key to show the main meu >>\n\n"); // message given showing 25 orders are there and its full.
            }
        }
        else if (order == 5)
        {
            printf("\nYour total bill value is $%.2f\n", orderArray[counter].totalPrice); // print bill amount
            billAppend();
            orderArray[counter].totalPrice = orderArray[counter].totalPrice - calculateDiscount(orderArray[counter].totalPrice, orderArray[counter].burgers.quantity);
            printf("Your final bill value is $%.2f\n", orderArray[counter].totalPrice);
            printf("Your receipt number is %s \n", orderArray[counter].receiptNo); // print receipt Number
            printf("Please go to a register and make the payment by quoting the Receipt Number - %s\n", orderArray[counter].receiptNo);
            i++;       // i value to find where the array pointer is
            counter++; // counter value to keep track how many orders are there
            printf("\n<<Press Any Key to show the main meu >>\n\n");
        }
        else if (order == 6)
        {

            char getReceipt[6]; // used to get user receipt number input
            int j, compare;     // compare used to get the output after comparing strings
            printf("Enter order number which you want to cancel : ");
            scanf("%s", getReceipt); // gets receipt from user
            for (j = 0; j < 25; j++) // loop to check the order Array for the receipt
            {
                compare = strcmp(getReceipt, orderArray[j].receiptNo); // compare the two strings(output is 0 if both are same).Output stored in compare variable.
                if (compare == 0)                                      // if both strings are same
                {
                    counter = counter - 1; // counter decremented as an order is cancelled.
                    int x = j;
                    for (x = j; x < 24; x++) // loop to shift other values in array to the left to take the deleted ones place
                    {
                        orderArray[x] = orderArray[x + 1];
                    }
                    printf("Your order has been cancelled. Thank you.\n");
                    break;
                }
            }
            if (compare != 0)
                printf("Invalid Receipt Number\n"); // if user entered a receipt number that is not there
            printf("<<Press Any Key to show the main meu >>\n\n");
        }
        else if (order == 7)
        { // when option 3 selected (exit option)
            printf("Thank you for choosing our services");
            break;
        }
        else if (order == 9)
        {
            char correctPass[] = "TastyBurger";
            char enteredPass[50];
            float tRev = 0;

            printf("Enter the Password : ");
            scanf("%s", enteredPass);

            if (strcmp(correctPass, enteredPass) == 0)
            {
                printf("Password Accepted\n");
                totalRevenue = 0;
                tRev = calcTotalRev(0);
                printf("Total Revenue for today is : $%.2f\n", tRev);
            }
            else
            {
                printf("\nInvalid Password\n");
                printf("<<Press Any Key to show the main meu >>");
            }
            continue;
        }
    }

    return 0;
}

void billAppend()
{                                           // function to appending the bill number to receipt array
    orderArray[counter].receiptNo[0] = 'B'; // moved these 3 from outside loop to inside the function
    orderArray[counter].receiptNo[1] = '0';
    orderArray[counter].receiptNo[2] = '0';
    if (i >= 10)
    {
        orderArray[counter].receiptNo[3] = (i / 10) + '0';
        orderArray[counter].receiptNo[4] = (i % 10) + '0';
        orderArray[counter].receiptNo[5] = '\0';
    }
    else
    {
        orderArray[counter].receiptNo[3] = (i) + '0';
        orderArray[counter].receiptNo[4] = '\0';
    }
}

float calculateDiscount(float total, int quantity)
{ // function to print the discount

    float calculatedDiscount;

    if (total < 100)
    {

        if (quantity >= 5)
        {
            calculatedDiscount = total * 0.1;
            printf("Discount 10%% - $%.2f\n", calculatedDiscount);
            return calculatedDiscount;
        }
        else
        {
            calculatedDiscount = 0.00;
            return calculatedDiscount;
        }
    }
    else if (total >= 100)
    {
        calculatedDiscount = total * 0.15;
        printf("Discount 15%% - $%.2f\n", calculatedDiscount);
        return calculatedDiscount;
    }
}

float calculateTotalPriceBurgers(char userChoice, int number) // function to calculate price
{

    if (userChoice == 'A' || userChoice == 'a')
        return 15 * number; // returns total after discount
    else if (userChoice == 'B' || userChoice == 'b')
        return 17 * number;
    else if (userChoice == 'C' || userChoice == 'c')
        return 13 * number;
}

float calculateTotalPriceMeals(char userChoice, int number) // function to calculate price
{

    if (userChoice == 'A' || userChoice == 'a')
        return 20 * number; // returns total after discount
    else if (userChoice == 'B' || userChoice == 'b')
        return 22 * number;
    else if (userChoice == 'C' || userChoice == 'c')
        return 18 * number;
}

float calcTotalRev(int index)
{
    if (index == 24)
    {
        return totalRevenue;
    }
    totalRevenue = +totalRevenue + orderArray[index].totalPrice;
    calcTotalRev(index + 1);
}
 
Back
Top Bottom