Thread: Help with Queue

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    20

    Help with Queue

    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");
    };
    }

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Well to start, don't do this:
    Code:
    fflush(stdin);
    and this is why.

    Also:
    Code:
    for (i = *numjobs; i >= 1; i--)
          printq[i] = printq[i-1];
    you shouldn't be starting at numjobs, start at numjobs - 1.

    This is an example of adding to an array the way you are and then printing out the result. See how I don't go to the arraysize, yet only to the arraysize - 1.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct test{
    	int x;
    	char c;
    };
    
    int main(void) {
    	
    	test mytest[3];
    	
    	for(int i = 0; i < 2; i++){
    		mytest[i].c = (char)('a' + i);
    		mytest[i].x = i;
    	}
    	
    	for(int i = 0; i < 2; i++){
    		printf("%d %c\n", mytest[i].x , mytest[i].c);
    	}
    
    	for(int i = 2; i >= 1; i--){
    		mytest[i] = mytest[i-1];
    	}
    
    	mytest[0].c = 'z';
    	mytest[0].x = 0;
    
    	for(int i = 0; i < 3; i++){
    		printf("%d %c\n", mytest[i].x , mytest[i].c);
    	}
    
    	return 0;
    }
    Last edited by andyhunter; 02-10-2005 at 08:32 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    I serviced every job to clear my queue. I then began adding jobs to the queue. The first two are fine but when I add a third job..for some reason, the first job in line is duplicated and printed out again. Canceljob and service job seem to working fine though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 09:09 PM
  4. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 11:39 AM
  5. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 09:38 AM