Please help me troubleshoot this code

This is a discussion on Please help me troubleshoot this code within the C Programming forums, part of the General Programming Boards category; Hi guys I need your help. Basically this is a restaurant ordering program where a customer can ordering multiple kinds ...

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    12

    Please help me troubleshoot this code

    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
    Order 2: salad
    Quantity: 1

    I am actually hoping that the program will output:

    Code:
    Table 1, Priority 0,
    Order: chicken , Quantity: 2
    Order: salad , Quantity: 1
    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;
    TABLE *head, *s;
    
    
    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];
        }
    
    
        if(head == NULL)
        {
            head = newO;
            newO->next = NULL;
        }
        else
        {
            temp = head;
            newO->next = temp;
            while(temp->next != NULL)
            {
                if(newO->priority > head->priority)
                {
                    head->next = newO;
                    newO->next = temp->next;
                }
                else
                {
                    newO->next = head->next;
                    head->next = newO;
                }
                temp = temp->next;
            }
        }
        n++;
        printf("\nTotal numbers of orders for today: %d", n);
        return 1;
    }
    
    
    void showlist()    
    {    
        int i, k;
        s=head;    
        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();
    }
    Please help me.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    12
    Thanks for response
    Quote Originally Posted by Salem View Post
    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. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Post the latest code and error messages.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    12
    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. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jan 2014
    Posts
    12
    I'm currently working on it right now. Hopefully, I could get it.

  8. #8
    Registered User
    Join Date
    Jan 2014
    Posts
    12
    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;
    TABLE *head, *s;
    
    
    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;
    
    
        if(head == NULL)
        {
            head = newO;
            newO->next = NULL;
        }
        else
        {
            temp = head;
            newO->next = temp;
            temp2->next = temp;
    
    
            while(temp2->next != NULL)
            {
                if(newO->priority > head->priority)
                {    
                    head = newO;
                    newO->next = temp->next;
                }
                else
                {
                    newO->next = head->next;
                    head->next = newO;
                }
                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;
        s=head;    
        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);
                    printf("What is your order? \n");
                    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. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.)
    Last edited by tabstop; 01-14-2014 at 08:32 AM.

  10. #10
    Registered User
    Join Date
    Jan 2014
    Posts
    12
    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. #11
    Registered User
    Join Date
    Jan 2014
    Posts
    12
    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.

    Name:  wrong..PNG
Views: 48
Size:  6.3 KB

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,628
    Your linked list is probably broken.

    To insert a value at the front of a list it's:
    Code:
    new0->next = head;
    head = new0;
    Not the other way around. This makes it so 'head' is updated, and you can still use it.

    I think you are trying to make it so the list is sorted by priority, but the way you are doing it now will also break the list. Inserting in the middle of a list means that the new value has to take on the next value of the previous node, because the new node is going to be placed after it. All you need to do for this snippet to work is search for and set prev. The snippet is:
    Code:
    new0->next = prev->next;
    prev->next = new0;
    This does not solve any problems if a user wants to update an existing table.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 10:28 AM
  2. Troubleshoot Please
    By xinglong in forum C Programming
    Replies: 4
    Last Post: 03-03-2010, 09:51 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. basic troubleshoot true and false
    By joker_tony in forum C Programming
    Replies: 1
    Last Post: 03-08-2008, 03:53 PM
  5. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 06:09 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21