我似乎很难清除键盘缓冲区。每当我接受输入并尝试再次接受输入时,它都不会等待用户输入。使用scanf和printf时,有什么方法可以清除缓冲区吗?我不使用cin和cout的唯一原因是因为我的教授不允许我们将其用于组织编程。
int main(int argc, char** argv)
{
char userOption, answer;
int enqueueNum;
bool enqueueResult, dequeueResult, ifEmpty;
answer = 'y';
QueueClass* queue = NULL;
queue = new QueueClass();
printf("This program implements Queues using object oriented programming.\n "
"Enter a number from 1 to 4 . \n"
"a. To enqueue a number \n"
"b. To dequeue a number \n"
"c. To get the current size of the queue \n"
"d. To see the contents within the queue \n"
"e. Quit the program \n" );
scanf( "%c", &userOption );
while(answer != 'n')
{
switch(userOption)
{
case 'a':
printf("Enter a number to put into the queue \n");
scanf("%d", &enqueueNum);
enqueueResult = queue->Enqueue(enqueueNum);
if(enqueueResult == true)
printf( "The number has been put into the queue! \n");
else
printf("The number was not able to be enqueued! \n");
break;
case 'b':
dequeueResult = queue->Dequeue();
if(dequeueResult == true)
printf( "The number has been dequeued! \n");
else
printf("The number wasn't able to be dequeued! \n");
break;
case 'c':
printf( "The current size of the queue is: " + queue->getCurrentSize() );
break;
case 'd':
queue->toString();
}
printf("Press y to continue, n to exit \n");
scanf("%c", &answer);
}
system("PAUSE");
return 0;
}