-1
#include<stdio.h>
#include<stdlib.h>
int main(){
    setbuf(stdout,NULL);
    int a[50],n,k,i,ip,top;
    char ch;
    top=-1;
    printf("Enter the size of the stack: ");
    scanf("%d",&n);
    do{
        printf("PROGRAM TO IMPLEMENT STACK USING ARRAY\n");
        printf("1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT");
        printf("\nEnter the choice: ");
        scanf("%d",&ip);
        if(ip==1){
            if(top==n-1){
                printf("Stack Overflow");
            }
            else{
                printf("Enter the element to be inserted: ");
                scanf("%d",&k);
                top++;
                a[top]=k;
                printf("The entered element is: %d",a[top]);
            }
        }
        else if(ip==2){
            if(top==-1){
                printf("The Stack is Empty");
            }
            else{
                printf("The deleted element is: %d",a[top]);
                top--;
            }
        }
        else if(ip==3){
            if(top==-1){
                printf("Stack Underflow");
            }
            else{
                printf("The elements in the stack are: ");
                for(i=top;i>=0;i--){
                    printf("\t%d",a[i]);
                }
            }
        }
        else{
            break;
        }
        printf("\nDo you want to continue(Y/N):");
        scanf("%c",&ch);
    }
    while((ch=='Y')||(ch=='y'));
    return 0;
}

When I try running this code after taking input from the user the loop doesn't execute again. It isn't giving me any particular errors..so please can someone help me understand what's missing in the code.

The loop just runs randomly taking infinite inputs without giving any further outputs after the initial run

Althaf
  • 29
  • 6
  • 1
    Have you tried using a debugger, and why not? – Scott Hunter Feb 18 '21 at 15:42
  • Im really a beginner and just starting to code...I didnt use it @ScottHunter – Althaf Feb 18 '21 at 15:43
  • 2
    `scanf("%c",&ch);` doesn't wait for `Y`/`N` because it immediately collects the newline char pending after previous scanf. – Roberto Caboni Feb 18 '21 at 15:44
  • 2
    Said differently, unless you have a strong reason to use it **avoid** `%c` in `scanf`... Here use `char ch[2];`, `scanf("%1s", ch);` and test `ch[0]`. – Serge Ballesta Feb 18 '21 at 15:45
  • So please could you suggest me what change should I do to my code @RobertoCaboni – Althaf Feb 18 '21 at 15:45
  • 1
    @Althaf A cheap solution to discard the previous newline is to use a space before `%c` – `scanf(" %c", &ch)` – Rohan Bari Feb 18 '21 at 15:47
  • 1
    You should test the return value from `scanf()` each time. If it isn't 1 (in your examples), something has gone wrong — the user typed text where you expected a number, or something similar, or indicated EOF. – Jonathan Leffler Feb 18 '21 at 16:13
  • 1
    'Im really a beginner and just starting to code'.....did it not occur to you to printf out the char before the Y/N check, to see what was happening? I'm sorry, but if you cannot debug to even that level, never mind using an actual debugger, you cannot usefully program computers at all:( – Martin James Feb 18 '21 at 16:31
  • Answer I started writing here, but wrote under the linked Q after this was closed (shameless plug): https://stackoverflow.com/a/66264109/6372809 – ilkkachu Feb 18 '21 at 16:39
  • @MartinJames, come now, being a beginner implies doing things the more experienced of us would consider "not very smart". Your suggestion is good, but there's no need to get so personal about it. – ilkkachu Feb 18 '21 at 16:41
  • @ilkkachu if you cannot debug, you cannot program computers. I stand by that statement. I could also have pointed out that 'newline left in buffer' is a mega-dupe that is fairly easily found, but I did not need to be over-critical. – Martin James Feb 18 '21 at 16:44

2 Answers2

1

Write scanf(" %c", &ch) instead of scanf("%c", &ch). Pay attention to the space before %c.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

Of course, you can use the general solution to this problem:

scanf(" %c", &ch);

But this will no longer work if you give input like Hello where an integer is required in your program, just before the program asks for Yes/No.

To robustly fix this, why not use a loop to discard ALL characters until the newline or EOF of input stream is found:

int c;
while ((c = getchar()) != '\n' && c != EOF)
    ;

printf("\nDo you want to continue(Y/N):");
scanf("%c", &ch);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34