Hi guys I need your help. Basically this is a restaurant ordering program where a customer can ordering multiple kinds food in one order/transaction.

For example, i will input:
Table Number: 1
Priority Number: 1
How many kinds of food to order?: 2
Order 1: chicken
Quantity: 2
Quantity: 1

I am actually hoping that the program will output:

Code:
```Table 1, Priority 0,
Order: chicken , Quantity: 2
But instead, it will just output:

Code:
```Table 1, Priority 0,
Order: (null), Quantity: 2```
So, here is the code:

Code:
```#include<stdio.h>#include<stdlib.h>
#include<string.h>

typedef struct orders
{
int *quantity[50];
char *foodname[50];
}ORDER;

typedef struct Table
{
int tableno;
int priority;
ORDER *orders;
struct Table *next;
}TABLE;

int n=0;

int insert(int tablenum, int prio, int kindsoforder, char *foodname[50], int qty[50])
{
int l;
TABLE *newO, *temp;
newO = (TABLE *)malloc(sizeof(TABLE));
newO->tableno = tablenum;
newO->priority = prio;

for(l=0;l<kindsoforder;l++)
{
strcpy(newO->orders->foodname[l], foodname[l]);
newO->orders->quantity[l] = qty[l];
}

{
newO->next = NULL;
}
else
{
newO->next = temp;
while(temp->next != NULL)
{
{
newO->next = temp->next;
}
else
{
}
temp = temp->next;
}
}
n++;
printf("\nTotal numbers of orders for today: %d", n);
return 1;
}

void showlist()
{
int i, k;
for(i=0;i<n;i++)
{
printf("\nTable No: %d, Priority: %d,  \n",s->tableno, i);
for(k=0;k<n;k++)
{
printf("Order: %s, Quantity: %d \n", s->orders->foodname[k],s->orders->quantity[k]);
}
s=s->next;
}
printf("\n");
}

void main()
{
int tablenum, prio, *qty[50], a, kindsoforder,j;
char choice, *foodname[50];
clrscr();
while(choice != 'b')
{
flushall();
printf("a. Order\nb. Exit\n");
scanf("%c", &choice);

switch(choice)
{
case 'a':
printf("Table Number: \n");
scanf("%d", &tablenum);
printf("Priority Number: \n");
scanf("%d", &prio);
printf("How many kinds of order? \n");
scanf("%d", &kindsoforder);
if(kindsoforder == 0)
{
break;
}
else
{
for(j=0;j<=kindsoforder;j++)
{
printf("Order %d: \n", j+1);
scanf("%s", &foodname[j]);
printf("Quantity? \n");
scanf("%d", &qty[j]);
}
}
a = insert(tablenum, prio, kindsoforder, foodname, qty);
if (a == 1)
{
printf("\nOrder inserted!\n\n");
showlist();
}
else
{
printf("Order not inserted!\n");
}

break;
case 'b':
printf("Exit!");
break;
}
}

getch();
}```

2. FAQ > Casting malloc - Cprogramming.com
FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

You have way too many pointers which are uninitialised.
Code:
```typedef struct orders
{
int quantity[50];
char foodname[50][20];
}ORDER;```
And
Code:
```    int tablenum, prio, qty[50], a, kindsoforder,j;
char choice, foodname[50][20];```
And you'll need this, since I doubt you'll have come across how to pass 2D arrays.
Code:
`int insert(int tablenum, int prio, int kindsoforder, char foodname[50][20], int qty[50])`
When you've made these changes, then go through the rest of the code replacing -> with .

As a follow-on exercise, remove all those global variables and replace them with suitable parameters to each function.

3. Thanks for response
Originally Posted by Salem
When you've made these changes, then go through the rest of the code replacing -> with .
Done this but my compiler gives me an error, so I keep using ->.And also, I still have a problem. When I place on a new order, it prints out characters and symbols.

4. Post the latest code and error messages.

5. I just actually did what you said Salem. I just changed the pointer array into 2D array. Also, have you noticed that when you execute the program, for example, you have 2 kinds of food to order on your 1st order, but it will only display just one of your order.

And when you order another one, it now displays all the food you order on the first order. But for your 2nd order, it will display characters and symbols.

I am getting confused here. But i guess, we are coming close since it already prints the data on the struct.

6. You'll have to keep working on the details yourself for a while (I'm off to work).

I gave you the big issue things that needed fixing, you need to study and fiddle with the detail.

7. I'm currently working on it right now. Hopefully, I could get it.

8. It's been 5 days and still I was not able to solve the problem. I have created a new code though. A bit similar to the previous, but only the user can now just order 1 name food every transaction. And it was set that if the next transaction has a higher priority, it will become the 1st priority. But, I still have a problem. I can't get it iterate through the linked list. Any help?

Anyway, here's the new code:

Code:
```#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

typedef struct orders
{
int quantity;
char foodname[50];
}ORDER;
ORDER *ptr;

typedef struct Table
{
int tableno;
int priority;
ORDER *orders;
struct Table *next;
}TABLE;

int n=0;

int insert(int tablenum, int prio, char foodname[], int qty)
{
TABLE *newO, *temp, *temp2;
newO = (TABLE*)malloc(sizeof(TABLE));
newO->tableno = tablenum;
newO->priority = prio;
strcpy(newO->orders->foodname, foodname);
newO->orders->quantity = qty;

{
newO->next = NULL;
}
else
{
newO->next = temp;
temp2->next = temp;

while(temp2->next != NULL)
{
{
newO->next = temp->next;
}
else
{
}
temp2 = temp2->next;
}
}
n++;
/*printf("%d", n);*/
return 1;
}

void showlist()    //I think the problem is in this function. It wont display all data on the structure or iterate through the linked list.
{
int i;
for(i=1;i<=n;i++)
{
printf("\nPriority: %d, Table No: %d, Order: %s, Quantity: %d \n",i, s->tableno, s->orders->foodname,s->orders->quantity);
s=s->next;
}
printf("\n");
}

void main()
{
int tablenum, prio, qty, a;
char choice, foodname[50];
clrscr();
while(choice != 'b')
{
flushall();
printf("a. Order\nb. Exit\n");
scanf("%c", &choice);

switch(choice)
{
case 'a':
printf("Table Number: \n");
scanf("%d", &tablenum);
printf("Priority Number: \n");
scanf("%d", &prio);
scanf("%s", &foodname);
printf("How many order? \n");
scanf("%d", &qty);
a = insert(tablenum, prio, foodname, qty);
if (a == 1)
{
printf("\nOrder inserted!\n\n");
showlist();
}
else
{
printf("Order not inserted!\n");
}

break;
case 'b':
printf("Exit!");
break;
}
}

getch();
}```

9. So from your insert function:
Code:
```newO = (TABLE*)malloc(sizeof(TABLE));
newO->tableno = tablenum;
newO->priority = prio;
strcpy(newO->orders->foodname, foodname);
newO->orders->quantity = qty;```
Those last two lines would be better if newO->orders pointed to something. Since it doesn't point to anything, your foodname and quantity disappear into the ether, never to be seen again. You need to create an ORDER for orders to point to, and then you can fill in the fields in that ORDER with foodname and quantity. (EDIT: I'm assuming that eventually that will lead to a linked list of orders for that table; if you don't want to mess with that now you can just put an ORDER structure inside your TABLE so you don't have to allocate extra space, although you will then have to change it back when you get the list working again.)

10. Hello tabstop,

Those last two lines would be better if newO->orders pointed to something. Since it doesn't point to anything, your foodname and quantity disappear into the ether, never to be seen again. You need to create an ORDER for orders to point to, and then you can fill in the fields in that ORDER with foodname and quantity.
I think what you said is what I needed now. Can you show me an example on how this is usually done?

11. So, I get what you mean. I inserted the code below after line 30:

Code:
`newO->orders = (ORDER*)malloc(sizeof(ORDER));`
But now, I am having another problem. The first two transactions works well, but after I inputted the 3rd transaction, it just print out the data of the 3rd instruction again and again.

To insert a value at the front of a list it's:
Code:
```new0->next = head;
```new0->next = prev->next;