Home > Software design >  Using structs and functions to print a menu
Using structs and functions to print a menu

Time:10-19

This program is supposed to print a menu using functions structs and arrays, My program compiles cleanly but prints nothing to the screen. Any help would be greatly appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
char pizzaName[30];
char ingredients[70];
} pizza_t;

int printPizzas(pizza_t pizzaMenu[3], int numPizzas);

int main (void) {

pizza_t pizzaMenu[3];

strcpy(pizzaMenu[0].pizzaName, "Barbecue");
strcpy(pizzaMenu[0].ingredients,"Beef, chicken, bacon, barbecue sauce");
strcpy(pizzaMenu[1].pizzaName, "Carbonara");
strcpy(pizzaMenu[1].ingredients, "Mushrooms, onion, creamy sauce");
strcpy(pizzaMenu[2].pizzaName, "Ham & Cheese");
strcpy(pizzaMenu[2].ingredients, "Ham, cheese, bacon");

return 0;
}

int printPizzas(pizza_t pizzaMenu[3], int numPizzas) { 

printf("\n\nPizza\t\tIngredients\n");
printf("----------------------------------------------------\n\n");

for (int i = 0; i < numPizzas; i  ) {
   printf("%s\t%s\n", pizzaMenu[i].pizzaName, 
pizzaMenu[i].ingredients);
}

printf("\n\n");

return 0;
}

CodePudding user response:

The main takeaway from the comments above is that just defining a function does not necessarily execute the function on its own. The function needs to be called somewhere, and in this case, you would want to call the function from within the main function. Following is an updated version of your code with a call to your printPizzas function along with a couple other refinements.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
    char pizzaName[30];
    char ingredients[70];
} pizza_t;

void printPizzas(pizza_t pizzaMenu[3], int numPizzas);      /* Changed return type to void as no actual value was going to be used */

int main (void)
{

    pizza_t pizzaMenu[3];

    strcpy(pizzaMenu[0].pizzaName, "Barbecue");
    strcpy(pizzaMenu[0].ingredients,"Beef, chicken, bacon, barbecue sauce");
    strcpy(pizzaMenu[1].pizzaName, "Carbonara");
    strcpy(pizzaMenu[1].ingredients, "Mushrooms, onion, creamy sauce");
    strcpy(pizzaMenu[2].pizzaName, "Ham & Cheese");
    strcpy(pizzaMenu[2].ingredients, "Ham, cheese, bacon");

    printPizzas(pizzaMenu, 3);      /* Here is where you might print out your menu */

    return 0;
}

void printPizzas(pizza_t pizzaMenu[3], int numPizzas)
{

    printf("\n\nPizza\t\tIngredients\n");
    printf("----------------------------------------------------\n");

    for (int i = 0; i < numPizzas; i  )
    {
        printf("%s\t%s\n", pizzaMenu[i].pizzaName, pizzaMenu[i].ingredients);
    }

    printf("\n\n");

    return;     /* FYI - when the function return type is void, this return could be omitted; just a matter of personal taste */
}

In the main function, you will notice that the printPizzas function is called after the program has initialized the menu items. The parameters in the call match the structure array and number of array elements expected by the function. In reviewing your code it seemed that there was no requirement for any type of value to be returned. So a tweak was made to make the return type void instead of int. The function would have worked fine with the return of an integer value, but it is usual and customary to utilize the void return type when there is no need for a return value.

Following was the output to the terminal when running this code.

@Una:~/C_Programs/Console/Pizzas/bin/Release$ ./Pizzas 


Pizza       Ingredients
----------------------------------------------------
Barbecue        Beef, chicken, bacon, barbecue sauce
Carbonara       Mushrooms, onion, creamy sauce
Ham & Cheese    Ham, cheese, bacon

Other than that, you appear to have grasped the concept of structures and just need to do more exercises with functions and calling them.

Give that a try to see if it meets the spirit of your project.

  •  Tags:  
  • c
  • Related