printf scanf正在交换执行订单

时间:2014-06-12 04:31:17

标签: c printf scanf

printf("Enter the value\n");
scanf("%d ",&data);

但是scanf正在printf之前执行。可能是什么原因?

这是我的整个计划

#include<stdio.h>
#include<stdlib.h>
struct Queue{
    int front,rear;
    int capacity;
    int *array;
};

void enqueue(struct Queue *q){

    int data;
    if(q->front == (q->rear+1)%q->capacity){
        printf("Queue is full\n");
        return;
    }else{
        printf("Enter the value\n");
        scanf("%d ",&data);
        q->rear = (q->rear+1) % q->capacity;
        q->array[q->rear] =data;

        if(q->front == -1)
            q->front = q->rear;
    }

}
void dequeue(struct Queue *q){

    if( q->front == -1){
        printf("Queue is Empty\n");
        return;
    }else{
        printf("value dequeued %d \n",q->array[q->front]);
        if(q->front == q->rear)
            q->front = q->rear = -1;
        else
            q->front = (q->front+1)% q->capacity;
    }
}
void printQueue(struct Queue *q){

    if( q->front == -1){
        printf("Queue is Empty\n");
        return;
    }else{
        int f = q->front, r =q->rear;

        while((f% (q->capacity)) != r)
        {
            printf ("\t %d", q->array[f] );
            f++;
        }
    printf("\t %d",q->array[q->rear]);
    printf("\n");
    }
}


main(){

    struct Queue *q = malloc(sizeof(struct Queue));
    if(!q){
        printf("Memory Error\n");
        exit;
    }
    q->capacity = 3;
    q->front= q->rear = -1;
    q->array = malloc(q->capacity * sizeof(int));   
    if(!q->array){
        printf("Memory Error\n");
        exit;
    }

    int option;
    char ch ='a';
    while(1) {
        printf("Enter the choice 1.Enqueue or 2.Dequeue 3.print\n");
        fflush(stdout);
        scanf("%d",&option);
        switch(option){
        case 1:
            enqueue(q);                 
            break;
        case 2:
            dequeue(q);
            break;
        case 3:
            printQueue(q);
            break;
        default:
            printf("Wrong choice\n");
        }


    printf("Do you want to continue? press y to continue\n");
    scanf("%s", &ch);
    if(ch != 'y')break;
    }
}

当我将一个元素排入队列时,它等待输入,然后执行printf语句。你想继续吗? 理想情况下,当我输入要排队并点击输入的元素时,它应该问我是否要继续,然后它应该等待输入,即&#39; y&#39;。但是,一旦我输入enqueue元素,它就会等待输入&#39; y&#39;进入&#39; y&#39; ,&#34;你想继续&#34;被执行

1 个答案:

答案 0 :(得分:0)

重写了问题代码。最大的问题是使用scanf(),它在stdin中留下未读取的值(例如回车)。未读的字符会干扰scanf()的下一次使用。更改scanf()以通过将%*c添加到格式字符串来从stdin中获取多余的字符:

#include<stdio.h>
#include<stdlib.h>
struct Queue{
    int front,rear;
    int capacity;
    int *array;
};

void enqueue(struct Queue *q){

    int data;
    if(q->front == (q->rear+1)%q->capacity){
        printf("Queue is full\n");
        return;
    }else{
        printf("Enter the value\n");
        scanf(" %d%*c",&data);
        q->rear = (q->rear+1) % q->capacity;
        q->array[q->rear] =data;

        if(q->front == -1)
            q->front = q->rear;
    }

}
void dequeue(struct Queue *q){

    if( q->front == -1){
        printf("Queue is Empty\n");
        return;
    }else{
        printf("value dequeued %d \n",q->array[q->front]);
        if(q->front == q->rear)
            q->front = q->rear = -1;
        else
            q->front = (q->front+1)% q->capacity;
    }
}
void printQueue(struct Queue *q){

    if( q->front == -1){
        printf("Queue is Empty\n");
        return;
    }else{
        int f = q->front, r =q->rear;

        while((f% (q->capacity)) != r)
        {
            printf ("\t %d", q->array[f] );
            f++;
        }
    printf("\t %d",q->array[q->rear]);
    printf("\n");
    }
}


int main()
   {
   int  option;
   char ch ='a';

   struct Queue *q = malloc(sizeof(struct Queue));
   if(!q)
      {
      printf("Memory Error\n");
      goto CLEANUP;
      }

   q->capacity = 3;
   q->front= q->rear = -1;
   q->array = malloc(q->capacity * sizeof(int));
   if(!q->array)
      {
      printf("Memory Error\n");
      goto CLEANUP;
      }

   while(1)
      {
      printf("Enter the choice 1.Enqueue or 2.Dequeue 3.print\n");
      scanf(" %d%*c",&option);
      switch(option)
         {
         case 1:
            enqueue(q);
            break;

         case 2:
            dequeue(q);
            break;

         case 3:
            printQueue(q);
            break;

         default:
            printf("Wrong choice\n");
            break;
         }


   printf("Do you want to continue? press y to continue\n");
   scanf(" %c%*c", &ch);
   if(ch != 'y')
      break;
   }

CLEANUP:

   return(0);
   }