whats wrong with this simple C stack program?

Status
Not open for further replies.

nix

Senior Member
Code:
/*stack program*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int s[10],top=-1,item,n=5;
void push()
{
if (top==n-1)
{
printf("stack is full, so pushing not possible\n");
getch();
return;
}
printf("ener the item to be pushed\n");
scanf("%d",&item);
top=top+1;
s[top]=item;

}
void pop()
{
if (top==-1)
{
printf("stack is emptly so popping not possible\n");
getch();
return;
}
item=s[top];
printf("the popped element is %d\n",item);
top=top-1;
}
void display()
{
int i;
{
if (top==-1)
{
printf("stack is empty, so display not possible\n");
getch();
return;
}
for (i=top;i>=0;i--)
{
printf("%d\n",s[i]);
}
}
}
void main()
{
int choice;
clrscr();
for (;;)
{
printf("1....push\n");
printf("2....pop\n");
printf("3....display\n");
printf("4....exit\n");
printf("enter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
default:exit(0);
}
}
getch();
}
when i execute this in borland turbo C, it gives me an error-"unreachable code" and the cursor points to the brackets after the getch. when i execute...it does not accept more than one item in the stack?? it just doesnt work...pls help...thanks...
edit: any helpful links to where i can find codes for C programs? ( i'm in 3rd sem engg)
 

sakumar79

Technomancer
I ran this code in Dev C++ and it ran fine... Only thing was I had to remove the clrscr command that was part of Turbo C specific list...

1. Which getch are you talking about? Each function has one...
2. Since you are defining s to be array of 10, why are you limiting n to 5?
3. Perhaps setting display to be the default action will be better than setting exit as default action...

Arun
 

mod-the-pc

Back to School Mr. Bean !
nix said:
Code:
for (;;)
{
printf("1....push\n");
printf("2....pop\n");
printf("3....display\n");
printf("4....exit\n");
printf("enter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
default:exit(0);
}
[COLOR=seagreen][B]/*Move the getch(); here*/[/B][/COLOR]
}
[COLOR=red][B]getch(); /*remove getch from here and move it to green line*/[/B][/COLOR]
}
Both the problems are due to a single misplaced statement
1. You get a "code unreachable" message because you've placed the getch() outside an infinite loop
2. You're able to enter only one value because your getch is not inside the loop.

Just move the getch() to the line in green in the above segment
 
Last edited:

nikhil ramteke

s,b+..u cn..
mod-the-pc said:
Both the problems are due to a single misplaced statement
1. You get a "code unreachable" message because you've placed the getch() outside an infinite loop
2. You're able to enter only one value because your getch is not inside the loop.

Just move the getch() to the line in green in the above segment
does it mean that after infinite loop there shouldnt b anything except }}}}}}}}}}}}}
 

Garbage

God of Mistakes...
and bcoz, ur infinite loop will end only when user enters wrong choice i.e. default case where u use exit()

So code after infinite loop will never get executed.

If there was any break outside switch (in infinite loop) then this error may not be there !

EDIT: Dear, please use new compilers !! It will reduce most of the errors !
 
Last edited:
Status
Not open for further replies.
Top Bottom