C从后续scanf语句中跳过用户输入

时间:2017-01-11 22:51:14

标签: c

我有一个主程序,它为C中的堆栈实现了一些功能。在接受用户的输入时,我发现了不寻常的结果。有人可以指出在C中接受用户输入的更好方法。

int main()
{
    int choice;
    int flag=1;
    char n;
    char string[100];
    while(flag==1)
    {
        printf("\nMake a selection\n");
        printf("Enter 1 to push an element in the stack\n");
        printf("Enter 2 to pop an element from the stack\n");
        printf("Enter 3 to print the stack elements\n");
        printf("Enter 4 to display the top element of the stack\n");
        printf("Enter 5 to reverse a string using stack\n");
        printf("Enter 6 to verify balanced brackets in an expression\n");
        printf("Enter 9 to quit\n");
        scanf("%d",&choice);
        printf("Entered choice %d\n",choice);
        switch(choice)
        {
            case 1:
                printf("Enter an element to push into the stack -->  ");
                scanf("%c",&n);
                Push(n);
                break;

            case 2:
                top=Pop();
                break;

            case 3:
                PrintElements();
                break;

            case 4:
                if(top==NULL)
                    printf("Stack is empty");
                else
                    printf("\nThe top element on the stack is --> %c",top->data);
                break;

            case 5:
                printf("Enter a string\n");
                fgets (string,100,stdin);
                printf("\n Original string is --> %s",string);
                for(int i=0;i<100;i++)
                {
                    if(string[i]=='\0')
                        string[i-1]='\0';
                }
                StringReverse(string);
                printf("Modified string is --> %s",string);
                break;

            case 6:
                printf("Enter a string\n");
                fgets (string,100,stdin);
                for(int i=0;i<100;i++)
                {
                    if(string[i]=='\0')
                        string[i-1]='\0';
                }
                CheckParanthese(string);
                break;

            case 9:
                flag=0;
                break;

            default:
                printf("Incorrect selection");
                break;
        }


    }
    return 0;
}

示例输出 这里当我输入1时,它会自动跳过下一个scanf语句,该语句要求用户输入元素以进入堆栈

Make a selection
Enter 1 to push an element in the stack
Enter 2 to pop an element from the stack
Enter 3 to print the stack elements
Enter 4 to display the top element of the stack
Enter 5 to reverse a string using stack
Enter 6 to verify balanced brackets in an expression
Enter 9 to quit
1
Entered choice 1
Enter an element to push into the stack -->
Make a selection
Enter 1 to push an element in the stack
Enter 2 to pop an element from the stack
Enter 3 to print the stack elements
Enter 4 to display the top element of the stack
Enter 5 to reverse a string using stack
Enter 6 to verify balanced brackets in an expression
Enter 9 to quit

0 个答案:

没有答案