Thread: Cafeteria Simulation

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    36

    Cafeteria Simulation

    I am having a problem getting my program to compile. Can someone please see if they can get this to compile?

    Thanks

    Code:
    
    #include <stdio.h>
    
    struct stack
    {
        int* store;
        int top;
        int capacity;
    };
    
    struct queue
    {
        int* store;
        
        int front;
        int back;    
        
        int capacity;
    };
    
    struct employee
    {
        int id;
        
        int* tray;  
    };
    
        struct stack* createStack();   //Stack
        struct queue* createQueue();   //Queue
        struct queue* newEmployee();
    
    int main (void)
    {
        FILE* fin;
        FILE* fout;
        
        char fname[1024];   //name of the input file we are reading from 
        char output[1024];  //name of the output file we are writing to
        char letter;        // the character determines what happens
        
        int num;        //variable for the number of lines to follow
        int count = 0;  //variable for the minute that we are on
        int j, k;
        int temp;
        int newEmployee;
        int incompetence = 0;
        
        printf("Welcome to Cafeteria in Latveria!\n");
        //Ask user for the name of the file to read from
        printf("Please enter the name of the file you wish to read from: \n");
        //Read in the file to read from
        scanf("%s", fname);
        //Open file for reading
        fin = fopen(fname, "r");
        
        //Check to make sure the file exists and opens correctly, if not display
        //an error message to the user
        if(fin == NULL)
        {
            printf("Unable to open the file %s\n", fname);
            system("PAUSE");
            return 0;   
        } 
        
        fscanf(fin, "%d ", &num);
               
        // Ask the user to specify and output file name
        printf("What is the name of the file you wish to save?\n");
        scanf("%s", output);
        //Open the specified file
        fout = fopen(output, "w");
        
        struct stack* stack  = createStack();
        struct queue* q = createQueue();
     //   struct employee e = newEmployee();
            
        //Operation 
        for(count = 1; count < num; count++)
        {
            fprintf(fout, "Minute %d: ", count);
            if(!feof(fin))
            {
                fscanf(fin, "%c ", &letter);
                
                // Dispensing
                if(letter == 'D')
                    {
                        incompetence = dispense(stack, q);
                        if(incompetence == 1)
                            break;
                    }
        
                    // Trays
                    else if(letter == 'T')
                    {
                        fscanf(fin, "%d ", &j);
                        for(k = 0;k < j; k++)
                        {
                            fscanf(fin, "%d ", &temp);
                            push(stack, temp);
                        }
                        fprintf(fout, "%d trays were added to the dispenser. The topmost tray is %d \n", j, getTop(stack));
                     }
                     
                     // Employee
                     else if(letter == 'E')
                     {
                         fscanf(fin, "%d ", &j);
        
                         for(k = 0;k < j; k++)
                         {
                            fscanf(fin, "%d ", &temp);
                            newEmployee = malloc(sizeof(struct employee));
                            newEmployee->id = temp;
                            newEmployee->tray = -1;
                            enqueue(queue, newEmployee);
                         }
                         fprintf(fout, "%d people entered the line. \n", j );}
                    }
                    continue;
                 }
        
                 if(queueEmpty(queue))
                 {
                     fprintf(fout, "Lunchtime is over. Long live our Fearless Leader!\n");
                     break;
                 }
        
                 // When the queue is empty you reads the statement above
                 // Otherwise dispenser will be empty and you are incompetent!!
        
                 incompetence = dispense(stack, queue);
                 
                 if(incompetence == 1)
                    break;
             }
         // This is used to print out information to the saved file..
         // fprintf(fout, "Sample");
    
        //Close the input and output files
        fclose(fin);
        fclose(fout);
        
        system ("PAUSE");
        return 0;    
    }
    
    //Create a new stack
    struct stack* createStack()
    {
        struct stack* s;
        
        s = malloc(sizeof(struct stack));
        s->store = calloc(999, sizeof(int));
        s->top = 0;
        s->capacity = 999;
        
        return s;
    }
    
    struct queue* createQueue()
    {
        struct queue* q = malloc(sizeof(struct queue));
        
        q->store = calloc(99, sizeof(int));
        q->front = 0;
        q->back = 0;
        q->capacity = 99;
        
        return q;
    }
    
    struct queue* newEmployee();
    {
        struct queue* newEmp = malloc(sizeof(int));   
        
        return newEmp;
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You need to include stdlib.h to be able to use malloc, then newEmployee is declared as an int but you use it as if it was a struct, (it's also the name of a function btw, not sure how well that will fly). That should get you started at least. A tip is to compile after every small change instead of everything at once, that way you catch the errors as they happen.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    36
    how do I declare newEmployee as a struct?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Code:
    struct employee newE;
    That will give you an instance of the struct employee. I chose the name newE just randomly, I don't think you should pick the name newEmployee as the last couple of lines in your code is a function named newEmployee.

    Wait! In the context of your code you'd want a pointer to the struct employee, since you allocate memory for it with malloc.
    Last edited by Subsonics; 07-03-2010 at 06:17 PM.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    36
    ok so this is what I have now...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct stack
    {
        int* store;
        int top;
        int capacity;
    };
    
    struct queue
    {
        int* store;
        int front;
        int back;    
        int capacity;
    };
    
    struct employee
    {
        int id;
        int tray;  
    };
    
        struct stack* createStack();   //Stack
        struct queue* createQueue();   //Queue
        struct queue* newEmployee(); 
    
    int main (void)
    {
        FILE* fin;
        FILE* fout;
        
        char fname[1024];   //name of the input file we are reading from 
        char output[1024];  //name of the output file we are writing to
        char letter;        // the character determines what happens
        
        int num;        //variable for the number of lines to follow
        int count = 0;  //variable for the minute that we are on
        int j, k;
        int temp;
        int incompetence = 0;
        
        printf("Welcome to Cafeteria in Latveria!\n");
        //Ask user for the name of the file to read from
        printf("Please enter the name of the file you wish to read from: \n");
        //Read in the file to read from
        scanf("%s", fname);
        //Open file for reading
        fin = fopen(fname, "r");
        
        //Check to make sure the file exists and opens correctly, if not display
        //an error message to the user
        if(fin == NULL)
        {
            printf("Unable to open the file %s\n", fname);
            system("PAUSE");
            return 0;   
        } 
        fscanf(fin, "%d ", &num);
               
        // Ask the user to specify and output file name
        printf("What is the name of the file you wish to save?\n");
        scanf("%s", output);
        //Open the specified file
        fout = fopen(output, "w");
        
        struct stack* stack  = createStack();
        struct queue* q = createQueue();
        struct employee* newE;
            
        //Operation 
        for(count = 1; count < num; count++)
        {
            fprintf(fout, "Minute %d: ", count);
            if(!feof(fin))
            {
                fscanf(fin, "%c ", &letter);
                
                // Dispensing
                if(letter == 'D')
                    {
                        incompetence = dispense(stack, q);
                        if(incompetence == 1)
                            break;
                    }
        
                    // Trays
                    else if(letter == 'T')
                    {
                        fscanf(fin, "%d ", &j);
                        for(k = 0;k < j; k++)
                        {
                            fscanf(fin, "%d ", &temp);
                            push(stack, temp);
                        }
                        fprintf(fout, "%d trays were added to the dispenser. The topmost tray is %d \n", j, getTop(stack));
                     }
                     
                     // Employee
                     else if(letter == 'E')
                     {
                            fscanf(fin, "%d ", &j);
                            for(k = 0;k < j; k++)
                            {
                                fscanf(fin, "%d ", &temp);
                                newE->id = temp;
                                newE->tray = -1;
                                enqueue(q, newE);
                            }
                            fprintf(fout, "%d people entered the line. \n", j );
                     }
                    continue;
                }
            }
        
                 if(queueEmpty(q))
                 {
                     fprintf(fout, "Lunchtime is over. Long live our Fearless Leader!\n");
                     return;
                 }
        
                 incompetence = dispense(stack, q);
                 
                 if(incompetence == 1)
                    return;
    
        //Close the input and output files
        fclose(fin);
        fclose(fout);
        
        system ("PAUSE");
        return 0;    
    }
    
    
    /**********************************FUNCTIONS**********************************/
    
    //Create a new stack
    struct stack* createStack()
    {
        struct stack* s;
        
        s = malloc(sizeof(struct stack));
        s->store = calloc(999, sizeof(int));
        s->top = 0;
        s->capacity = 999;
        
        return s;
    }
    
    struct queue* createQueue()
    {
        struct queue* q = malloc(sizeof(struct queue));
        
        q->store = calloc(99, sizeof(int));
        q->front = 0;
        q->back = 0;
        q->capacity = 99;
        
        return q;
    }
    
    struct queue* newEmployee()
    {
        struct queue* newEmp = malloc(sizeof(int));   
        
        return newEmp;
    }
    and it still won't compile. any suggestions?

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yes, create it incrementally and compile often so that you can test each component of the program. There is stuff that is not C in your program like enqueue(), dispense() and queueEmpty() for example.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    74
    Also, you have some "return;" in main, that should return some value, say "return -1;".

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    "return -1 ;" or anything except non zero would do. The "return 0;" is a normal exit and anything except non-zero is an, abnormal exit. Alternatively you could EXIT_SUCCESS and EXIT_FAILURE which are defines in stdlib.h and also been defined under C89 and C99.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    And also see few pitfalls in your code which you might have to consider improving

    1. systen("PAUSE").
    This is clearly non-portable code. Would be better off using getchar() from stdio.h

    2. scanf("%s", ... )
    Avoid using scanf function reading any user data. Read here to know why. Instead use fgets to read string which avoid potential problems like the once scanf leaving the '\n' behind and causing problem when reading the next value and buffer overrun or boundary check.

    Harish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fork, execv, spawn, or something else?
    By DavidP in forum C++ Programming
    Replies: 8
    Last Post: 01-26-2009, 04:25 PM
  2. Role of c++ in simulation software
    By CChakra in forum C++ Programming
    Replies: 9
    Last Post: 11-18-2008, 02:36 AM
  3. Help!For poker game simulation
    By tx1988 in forum C++ Programming
    Replies: 24
    Last Post: 05-25-2007, 09:59 PM
  4. Help with time simulation assignment
    By rakan in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2006, 11:39 AM
  5. Pipeline freeze simulation
    By darklightred in forum C Programming
    Replies: 2
    Last Post: 07-24-2006, 11:57 AM