Separate code from data.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct dish {
const char *description;
float price;
} dishes[] = {
{ "Rajma Chawal", 40 },
{ "Kadhi Chawal", 40 },
{ "Dal Chawal", 40 },
{ "Roti & 2 sabjis", 80 },
{ "Omlette", 30 },
{ "Egg Roll", 25 },
{ "Maharaja Thali", 380 },
};
void printMenu ( struct dish *d, size_t n ) {
for ( unsigned int i = 0 ; i < n ; i++ ) {
printf("%u.) %s\t%f\n",
i+1, d[i].description, d[i].price );
}
}
void takeOrder ( struct dish *d, size_t n ) {
unsigned int what;
printf("What do you want? ");
scanf("%u",&what);
if ( what > 0 && what < n ) {
printf("%s costs %f\n",
d[what-1].description, d[what-1].price);
}
}
int main ( ) {
printMenu(dishes,sizeof(dishes)/sizeof(dishes[0]));
takeOrder(dishes,sizeof(dishes)/sizeof(dishes[0]));
}
Extending this to read the menu from a file becomes trivial.