Re: can any bdy help me



On 31 May, 11:53, shanti <prasanth.than...@xxxxxxxxx> wrote:
hai,
i have developed a c-code to push integers into a stack (an array).
in the program there are three inputs which has to be given through
keyboard. the inputs are "choice", "option","S_R_NO" and "i" all are
defined as "int".
now when the inputs given are int type the program behaviour is as
required. but when the input given are char type the program is
executing infinite loop.i could not understand this behaviour and i am
unable to start. the code is developed on turbo-c and it is
executiable.

can any one help me in finnding the bug and tell me the better way of
doing.

Well, you could start by reading the FAQ - http://c-faq.com/stdio/scanfprobs.html
would probably be a good start.

Reading a decent book on C coding (or coding in general, actually)
would also be useful.

----------------------------------------------------------------------------------
#include<stdio.h>
OK

#include<conio.h>
#include<iostream.h>

Non-standard and not really necessary, as far as I can tell.


#define SIZE 6
int top=-1;

void main()

the standard says otherwise.

{
void push(int*);
int stack[SIZE], *stk_ptr;
int choice;
int S_R_NO,option;
stk_ptr=stack;

start: //here it starts

It's better to use "/*...*/" for comments. Many compilers don't accept
this form, and it's poor for cutting and pasting from newsgroup posts.

clrscr();

I will assume this clears the screen. It's not

printf("\nEnter 1 to continue: ");
scanf("%d",&choice);

You don't check the return value of scanf() - a Bad Thing.
What will scanf do with non-numeric input? (Hint: probably not what
you want). Read the FAQ.

if(choice==1) {
push(stk_ptr);
printf("\nTo continue press 1 to exit 0:");
scanf("%d",&option);
if(option==1) goto start;

"Any references to Goto (an obscure Japanese admiral) are obscene,
unfit for your eyes and anyway don't mean what you think they do -
delete them" (notes on the Algol manual from a university course in
the 1970s).

You almost never need a goto in C.

}
else{
printf("CAUTION:WRONG CHOICE, ENTER 1 TO CONTINUE 0 TO EXIT:
");
fflush(stdin);

According to the standard, this is meaningless.

scanf("%d",&S_R_NO);

Same comments as before about scanf().

if(S_R_NO==1) goto start;
}

end:

As far as I can see you never use this label.

printf("\n------END OF THE PROGRAM-----");
getch();

}

Your formatting (indentation) is appalling. Don't you have a tool to
do it, perhaps in your editor?


/
*----------------------------------------------------------------------
*/

void push(int *stk)
{
register int i,j;
top+=1;
if(top==SIZE){ printf("\nOverflow of the stack");top-=1; }
else { printf("\nEnter the element you want to push: ");
scanf("%d",&i);
*(stk+top)=i;
printf("now the stack is:");
for(j=0;j<=top;j++)
printf("\nelement at <%d> is <%d>",j,*(stk+j));
}}

Are you kidding?
1) The formatting of the code is awful
2) the mixing of interaction with the user and manipulation of the
stack is hopeless.

The natural prototype for push() in your code is much more likely to
be
int push(int *stack, int value); /* return 1 for success, 0 for
failure (overflow) */


/
*---------------------------------------------------------------------------
*/
runtime problems:
1) first round input: if a char is entered for "i" program terminating
without asking for the next value of "option".

Read the FAQ - and perhaps the standard - and find out what scanf()
would return in this case.

2) after first round of input: if char is given as input for any of
the variables "choice", "option","S_R_NO" or "i" the program is
executing infiniteloop.

Similarly.

Use something other than scanf() for a start...

In your position, I'd start by writing code to maintain a stack - with
push and pop operations, and write a simple non-interactive testcase
to exercise it, the only I/O would be a few printf() statements to
show the state of the stack at various points in the code.

Once I'd done that, I'd look at how to add some interactive code to my
testcase.

.



Relevant Pages

  • Re: old skool vc++ 1.0
    ... right-to-left order, a CALL is executed, and upon return from the CALL, the stack pointer ... int f ... mov ebp, esp ...
    (microsoft.public.vc.mfc)
  • Re: Newbie question stack implementation
    ... > would like to pop them and print them out as they come off the stack. ... int main ... In that case push() ... would work correctly you still would have a problem with this loop - ...
    (comp.lang.c)
  • Re: stack
    ... This is a very simple implementation of push and pop functions of a ... int top; ... void push(struct stack *p,int n) ...
    (comp.lang.c)
  • stack
    ... This is a very simple implementation of push and pop functions of a ... int top; ... void push(struct stack *p,int n) ...
    (comp.lang.c)
  • Re: Is it wise to push all-in early in a tournament?
    ... The best times to make a push play ... But why call when you can push and take down a nice pot when your opponent ... But remember that if this "big bet" is a significant portion of your stack, ... I consider it my goal in any tournament to do the latter as much as ...
    (rec.gambling.poker)