0

I have a main program that implements some functions for a stack in C. While accepting input from the user I find unusual results. Can someone point out what is a better way to accept user input in 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;
}

Sample Output Here when I enter 1 it automatically skips the next scanf statement which asks for user input for an element to push into 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
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
user2808264
  • 459
  • 2
  • 11
  • 24
  • `scanf("%c",&n);` --> `scanf(" %c",&n);` Clear previous newline. – BLUEPIXY Jan 11 '17 at 22:56
  • The last question I looked had this same problem: `scanf("%c",&n);` — this reads the newline left behind by the `%d`. Use `scanf(" %c",&n);` where the blank is all-important and skips the newline (and other white space). Don't use a trailing blank (or other white space character, such as tab or newline) in a format, though — that's a different problem. Yes, `scanf()` et al are very hard to use correctly. – Jonathan Leffler Jan 11 '17 at 22:57

0 Answers0