有人可以看看我的队列吗?

时间:2014-08-10 06:22:01

标签: c function queue

我正在使用Dev C ++。一旦我在队列中输入名称并按下

,程序就会崩溃
void queue (Client c[])
{
    int choice,tail=0,max=20,front=0,ch,w;
    char queue [20];

    do {
        printf ("Do You Want To (1)Enter Clients To A Queue or (2)Remove Clients From A Queue?");
        scanf ("%d",&choice);
    }while (choice<1 || choice>2);

    if (choice==1)
    {
        if (tail==max-1)
        {
            printf("Queue is full!");
            return;
        }

        tail++;
        printf("Enter Client Name:");
        scanf (" %[^\n]",queue[tail]);            
        if (front==-1)
            front++;
    }
    else if (choice==2)
    {
        if (front==-1)
        {
            printf("Queue is empty!");
            return;
        }
        printf("Enter Client Name To Remove:");
        scanf (" %[^\n]",queue[front]);
        if (front==tail)
            front=tail=-1;
        else
            front++;
    }

    do {
        printf ("Do You Want To (1)Queue More Clients or (2)Continue?");
        scanf ("%d",&ch);
    }while (ch<1 || ch>2);

    if (ch==1)
    {
        void queue (Client []);
        queue (c);
    } 
    else if (ch==2)
    {
        do{
            printf ("Do You Want To Go Back To (1)The Main Menu Or (2)Exit?");
            scanf ("%d",&w);
        }while (w<1 || w>2);

        if (w==1)
        {
            void main_menu (Client[]);
            main_menu (c);   
        }
        else if (w==2)
        {
            void end (Client []);
            end (c);
        }
    }                 
}

2 个答案:

答案 0 :(得分:1)

您需要一个多维字符数组来保存名称。你要做的是接受单个字符的名字。它怎么样?

只需更改代码:

char queue [20];

到:

char queue [20][20];

答案 1 :(得分:0)

这似乎有些不对劲:

printf("Enter Client Name:");
scanf (" %[^\n]",queue[tail]);

您正在尝试将一个名称(这是一个char数组)输入到队列元素(这是一个char)。也许你应该把它改成:

printf("Enter Client Name:");
scanf (" %[^\n]",queue);

您的代码有改进的余地。一些重要的事情需要测试:

  1. 为队列变量使用不同的名称(将其与队列方法区分开来)。
  2. 您的队列变量应该全局声明,而不是在函数中声明。
  3. 你的队列变量应该是这样的:char * queueVariable [20];它应该是一个名字数组(它本身就是一个字符数组)。
  4. 队列绑定控制器变量也应该全局声明。