#include <stdio.h>
#define MAX 5
int stk[MAX];
int top=-1;
main()
{
char ch;
void push();
void pop();
void display();
do
{
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
ch=getchar();
if(ch=='1')
push();
if(ch=='2')
pop();
if(ch=='3')
display();
printf("Do u want to continue y/n");
ch=getchar();
}while(ch=='y'||ch=='Y');
}
void push()
{
}
void pop()
{
}
void display()
{
}
The moment i finish with the push operation once…the program prints “”Do u want to continue y/n ” and exits….doesnt wait for the user input “” y/Y”
Pls help
\n) in it from when the user pressed the Enter key.getchar()once, reading in that newline from the buffer, which is assigned tochand does not equate to'y'or'Y', so your loop exits.As for fixing this problem, that is left as an exercise to you. You could look into using other methods of reading in data besides a lone
getchar(). See here for some input functions (hint:fgets). You could also try to extract this character from the buffer and discard it so subsequent calls togetchar()work as expected.If this is for school, which it appears to be, you may want to write a function that you can reuse throughout your course. This way, you can debug it, and are familiar with how it works.
Good luck!