-
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;
}
-
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.
-
how do I declare newEmployee as a struct?
-
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.
-
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?
-
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.
-
Also, you have some "return;" in main, that should return some value, say "return -1;".
-
"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
-
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