Reputation:
When I choose push in my menu, then I enter some number value, then the program works fine, but when I enter some letter value, then program never stops, where is my mistake? I'm beginner in c, so maybe someone can help solve this problem. I have this code:
#include <stdio.h>
#include <curses.h>
int a[15], top = -1;
void push (int value)
{
if (top == 14)
{
printf("Stack is full");
}
else{
top = top + 1;
a[top] = value;
}
}
void pop()
{
if (top == -1)
{
printf("Stack is empty");
}
else
{
top = top - 1;
}
}
void display()
{
int i;
if (top == -1)
{
printf("\n Nothing to display");
}
else
{
printf("\nArray is:\n");
for (i=0; i<=top; i++)
{
printf("%d\n", a[i]);
}
}
}
int main()
{
int choice, value;
do{
printf("\n1.Push :");
printf("\n2.POP :");
printf("\n3.Display :");
printf("\n4.Exit :");
printf("\nEnter your Choice:");
scanf("%d", &choice);
if(choice == 1)
{
printf("\nEnter Value to be inserted: ");
scanf("%d", &value);
push(value);
}
if(choice == 2)
{
pop();
}
if (choice == 3)
{
display();
}
}
while (choice !=4);
getch();
return 0;
}
Upvotes: 1
Views: 59
Reputation: 1871
You need to change two things for your code to work.First change the following variables to character
char a[15];
char value;
plus you also need to pass a charater to the function not an integer.
Upvotes: 1