Problem with queue program

This is a discussion on Problem with queue program within the C Programming forums, part of the General Programming Boards category; The program below isn't executing correctly. It should add names to the queue when you choose insert. Instead, if I ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    Problem with queue program

    The program below isn't executing correctly. It should add names to the queue when you choose insert. Instead, if I choose insert it just repeatedly prompts me until the queue is full (then tells me queue full). I'm thinking it's a problem in my while loop, but I can't figure out where. Any help would be greatly appreciated.

    Code:
    #include <stdio.h>
    
    #define MAXIMUM 10
    
    #define MAXS 8
    
    
    
    char q[MAXIMUM][MAXS];
    
    int h, t;
    
    
    
    int main() {
    
        char response[MAXS];
    
        int h, t = - 1;
    
        printf("Insert, delete, print, or quit?");
    
        scanf("%s", &response);
    
    
    
        void insertq(void);
    
        void moveq(void);
    
        void deleteq(void);
    
        void printq(void);
    
        
    
        while(response[0] != 'q' && response[0] != 'Q') {
    
            switch(response[0]) {
    
                case 'I':
    
                case 'i':
    
                insertq();
    
                break;
    
                case 'D':
    
                case 'd':
    
                deleteq();
    
                break;
    
                case 'P':
    
                case 'p':
    
                printq();
    
                break;
    
                default:
    
                printf("Bad input");
    
                break;
    
                printf("Insert, delete, print, or quit?");
    
                scanf("%s", &response);
    
            }
    
        }
    
    
    
        printf("End of job");
    
        return 0;
    
    }
    
    
    
    void insertq(void) {
    
    
    
        char response[MAXS];
    
        if(t - h + 1 == MAXIMUM)
    
            printf("Overflow");
    
        else {
    
            printf("Enter your name \n");
    
            scanf("%s", &response);
    
            
    
            if(h == -1) {
    
                h = 0;
    
                t = -1;
    
            }
    
            
    
            if(t == MAXIMUM - 1)
    
                moveq();    
    
    
    
        }
    
    
    
    }
    
    
    
    void moveq(void) {
    
        
    
        int j;
    
        
    
        for(j = 0; j <= t - h; j++) {
    
            strcpy(q[j], q[h+j]);
    
            t = t - h;
    
            h = 0;
    
        }
    
    }
    
    
    
    void deleteq(void) {
    
        
    
        if(h == -1)
    
            printf("Underflow");
    
        else {
    
            printf("Servicing %s \n", q[h++]);
    
            if(h > t) {
    
                h = -1;
    
                t = -1;
    
            }
    
        }
    
    }
    
    
    
    void printq(void) {
    
    
    
        char response[MAXS];
    
        int j;
    
        char L, l, P, p;
    
        printf("Logical or physical? \n");
    
        scanf("%s", &response);
    
    
    
        if(response[0] == L || response[0] == l) {
    
            for(j = h; j <= t; j++)
    
                printf("%d %s \n", j, q[j]);
    
        }
    
        if(response[0] == P || response[0] == p) {
    
            for(j = 0; j < MAXS; j++)
    
                printf("%d %s \n", j, q[j]);
    
        }
    
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... you're using h and t as global variables ... not good.

    Your functions are all void function(void) also not good. A far better plan would be to move your global variables into main and pass them into your functions as parameters... If the function has to update them pass in pointers to the variables.

    Also... your text setup leaves a lot to be desired. Double spacing is NOT clearer, unless you are visually impaired. A far better strategy would to single space your code and put blank lines between sections and functions... Your indentation needs work too...

    For example:
    Code:
       while(response[0] != 'q' && response[0] != 'Q') {
            switch(response[0]) {
                case 'I':
                case 'i':
                    insertq();
                    break;
                case 'D':
                case 'd':
                    deleteq();
                    break;
                case 'P':
                case 'p':
                    printq();
                    break;
                default:
                    printf("Bad input");
                    break;
    
                printf("Insert, delete, print, or quit?");
                scanf("%s", &response);
            }
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Queue problem
    By denisa in forum C Programming
    Replies: 2
    Last Post: 09-28-2011, 02:40 AM
  2. Queue Problem
    By SMurf in forum C Programming
    Replies: 3
    Last Post: 04-26-2008, 11:30 AM
  3. Queue program
    By esi in forum C Programming
    Replies: 11
    Last Post: 04-08-2007, 04:03 PM
  4. queue problem using c
    By doom83 in forum C Programming
    Replies: 3
    Last Post: 10-16-2005, 07:03 AM
  5. queue problem
    By Yelagin in forum C Programming
    Replies: 1
    Last Post: 07-24-2002, 11:04 AM

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