Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-11-2006, 08:31 PM   #1 (permalink)
nix
Senior Member
 
nix's Avatar
 
Join Date: Oct 2004
Location: Bangalore
Posts: 648
Default whats wrong with this simple C stack program?


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)
__________________
nikhilspoliticalblog.wordpress.com- Common man bows to amitabh
nix is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 09-11-2006, 10:28 PM   #2 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
Default Re: whats wrong with this simple C stack program?

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
sakumar79 is offline  
Old 10-11-2006, 12:32 AM   #3 (permalink)
Back to School Mr. Bean !
 
mod-the-pc's Avatar
 
Join Date: Apr 2004
Location: Chennai
Posts: 343
Default Re: whats wrong with this simple C stack program?

Quote:
Originally Posted by nix
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);
}
/*Move the getch(); here*/
}
getch(); /*remove getch from here and move it to green line*/
}
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
__________________
Desktop: P4 2.8E, Intel 865GBF, 512MB Hynix PC3200, Samsung SV1204H 120GB, Samsung SW-248F, Creative Inspire 4400, Samsung 793MB, Compro PVR/FM, WinLIRC

Laptop: HP Pavilion dv8210us

Last edited by mod-the-pc; 10-11-2006 at 12:43 AM.
mod-the-pc is offline  
Old 30-08-2007, 01:44 PM   #4 (permalink)
s,b+..u cn..
 
nikhil ramteke's Avatar
 
Join Date: Dec 2006
Posts: 76
Default Re: whats wrong with this simple C stack program?

Quote:
Originally Posted by mod-the-pc
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 }}}}}}}}}}}}}
nikhil ramteke is offline  
Old 30-08-2007, 05:10 PM   #5 (permalink)
In The Zone
 
Nav11aug's Avatar
 
Join Date: Sep 2006
Location: Kharagpur for now
Posts: 362
Default Re: whats wrong with this simple C stack program?

Nope.. it means anything after an infinite loop will never get executed
__________________
http://www.last.fm/user/NOLFxceptMe/
http://naveendageek.blogspot.com

Back after a long time. And well, EndSems screwed up as usual :)
Nav11aug is offline  
Old 30-08-2007, 09:40 PM   #6 (permalink)
God of Mistakes...
 
Garbage's Avatar
 
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,923
Default Re: whats wrong with this simple C stack program?

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 !
__________________
Registered Linux User #468778
----------------------------------
http://twitter.com/_Garbage_

Last edited by Garbage; 30-08-2007 at 09:40 PM. Reason: Automerged Doublepost
Garbage is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by topgear
- by abhidev
- by clmlbx
- by Sarath

Advertisement




All times are GMT +5.5. The time now is 04:58 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2