0

It appears as though I'm having a hard time clearing the keyboard buffer. Anytime I take input and try to take input again, it will not wait for the user input. Is there any way to clear the buffer while using scanf and printf? The only reason im not using cin and cout is because my professor isnt letting us use it for organization of programming.

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;
}
Zachariah
  • 47
  • 5

0 Answers0