I have a program, consisting of 3 files: naivepq.c, naivepq.h, and printq.c. It is a printer queue that prints out a queued list, allows the user to add to the list, remove a selected item from the list, service the first in the list and print the list. My instructor gave us his executable to see how it is supposed to turn out. Everytime I test mine, I get junk under the pages column, and everything is backwards. *note: canceljob and servicejob work* Anyone have any suggestions?
Here is naivepq.c---------------------------------------------------
Code:
#include <stdio.h>
#include "naivepq.h"
/* private functions. */
/* Function: assignjobid
* Usage: id = assignjobid(printq,numjobs);
* This function assigns a unique job id
* to a new print job.
*/
static int assignjobid(job printq[],int numjobs)
{
int i, j, found;
if (numjobs == 0)
return 1;
if (numjobs == MAX_JOBS)
return 0;
i = 0;
while (i <= numjobs)
{
i++;
j=0;
found = 0;
while(j<numjobs && !found)
{
if (printq[j].id == i)
found = 1;
j++; }
if (!found)
return i;
}
return numjobs+1;
}
/* Define these functions. See descriptions in the header file.*/
int addjob(job printq[], int* numjobs)
{
int jobid, i;
job newjob;
jobid = assignjobid(printq,*numjobs);
if (!jobid)
return 0;
newjob.id = jobid;
printf("Enter the username: ");
scanf("%s",newjob.who);
fflush(stdin);
printf("Pages: ");
scanf("%d",&(newjob.pages));
i = 0;
while (i < 1 || i > 5)
{
printf("\n\nText.........[1]\n");
printf("Image........[2]\n");
printf("Html.........[3]\n");
printf("Postscript...[4]\n");
printf("Others.......[5]\n");
printf("\n\nSelect an option: ");
scanf("%d",&i);
}
if (i==1)
{
newjob.kind = TEXT;
newjob.duration = 3 * newjob.pages;
}
if (i==2)
{
newjob.kind = IMAGE;
newjob.duration = 6 * newjob.pages;
}
if (i==3)
{
newjob.kind = HTML;
newjob.duration = 3 * newjob.pages;
}
if (i==4)
{
newjob.kind = POSTSCRIPT;
newjob.duration = 4 * newjob.pages;
}
if (i==5)
{
newjob.kind = OTHERS;
newjob.duration = 7 * newjob.pages;
}
for (i = *numjobs; i >= 1; i--)
printq[i] = printq[i-1];
printq[0] = newjob;
(*numjobs)++;
return 1;
}
int canceljob(job printq[], int* numjobs, int jobid)
{
int i,j;
for(i=0; i < *numjobs; i++) {
if(printq[i].id == jobid) {
for(j=i; j < *numjobs; j++) {
printq[j] = printq[j+1];
};
(*numjobs)--;
return 1;
};
};
return 0;
}
int servicejob(job printq[], int* numjobs)
{
(*numjobs)--;
return 1;
}
void printjoblist(job printq[], int numjobs)
{
if(numjobs > 0) {
int i;
printf("id -- who ---- pages ----- duration -- kind\n\n");
for (i=0; i < numjobs; i++) {
printf("%d -- %s ------ %d ----- %d ------- ", printq[i].id, printq[i].who, printq[i].pages, printq[i].duration);
switch (printq[i].kind)
{
case TEXT: printf("TEXT\n");
break;
case IMAGE: printf("IMAGE\n");
break;
case HTML: printf("HTML\n");
break;
case POSTSCRIPT: printf("POSTSCRIPT\n");
break;
case OTHERS: printf("OTHERS\n");
break;
default: printf("Something is wrong \n");
}
printf("\n");
};
}
else {
printf("There are currently no jobs in the queue\n\n");
};
}