Re: can any bdy help me



shanti wrote:

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.
----------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>

Non-standard and unnecessary header.

#include<iostream.h>

Ditto.

#define SIZE 6
int top=-1;

Global variable for stack index: bad idea. Keep it local
to `main` and have `push` take an extra parameter. Later
when you know about `struct` you'll be able to keep the
index and the stack (and the size) all together.

void main()

The portable return type for `main` is `int`.

{
void push(int*);

Usual style is to have function declarations outside
functions.

int stack[SIZE], *stk_ptr;
int choice;
int S_R_NO,option;

All these uninitialised variables make me nervous.

stk_ptr=stack;

start: //here it starts

You don't need labels-and-gotos for this; that's what a
`while` loop is `for`. One so rarely needs a `goto` in
C that a good rule of thumb is `never use goto`; eventually
you will discover circumstances where it's the preferred
construct. Eventually.

clrscr();

Non-standard, unnecessary, and IMAO unforgivable here.

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

There's now stuff in the input stream -- at least a newline.
What do you plan to do if the user doesn't input a digit?
Why enter /1/ to continue -- instead of `y`, say? And
why "continue" when we haven't really started yet?

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

See above.

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

DON'T SHOUT AT ME, YOU STUPID MACHINE.

Um. If you weren't going to let me not-continue, why did you
offer me a choice in the first place?

fflush(stdin);

It's undefined behaviour to fflush an input stream. Don't do that.
(You want to eat characters until a newline, as a best approximation.)

scanf("%d",&S_R_NO);
if(S_R_NO==1) goto start;
}

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

DON'T SHOUT AT ME, YOU STUPID MACHINE.

You say that, but ...

getch();

.... means that it /isn't/ the end.

}

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

void push(int *stk)

Thr's n shrtg f vwls n snt. Call it `stack`.

{
register int i,j;

`register` isn't terribly useful here.

top+=1;
if(top==SIZE){ printf("\nOverflow of the stack");top-=1; }
else { printf("\nEnter the element you want to push: ");

`value` is better than `element`. (Usually `element` refers to the
position in the stack, but that's not what you're asking for.)

scanf("%d",&i);

See above.

*(stk+top)=i;

Um, this is idiomatically written `stk[top] = i;`. Why are you using
the `*(stk+top)` form?

printf("now the stack is:");
for(j=0;j<=top;j++)
printf("\nelement at <%d> is <%d>",j,*(stk+j));

(indent) (ditto)

}
}
/
*---------------------------------------------------------------------------
*/
runtime problems:
1) first round input: if a char is entered for "i" program terminating
without asking for the next value of "option".
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.

Yes. You don't check for these possibilities, so when they happen,
things go badly.

--
"We are on the brink of a new era, if only --" /The Beiderbeck Affair/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

.



Relevant Pages

  • Re: Having C code looking like C++ code
    ... providing an integer stack. ... | extern int push; ... | typedef struct stacktype { ... muck around inside the encapsulation to use the interface. ...
    (comp.lang.c)
  • Re: [OT] C programming, variable size array
    ... The trick to understanding here is to know how memory gets allocated. ... int main{ ... The stack holds "automatic" variables used in C, ... Will automatically allocate and deallocate 1K of data on the stack. ...
    (Debian-User)
  • Re: Link Instruction & tt command
    ... In order to do the trace, ... than C, the routine's entry point is non-standard, or the task's stack ... int f_c ... framepointer, and you can not make any stacktraces. ...
    (comp.os.vxworks)
  • Re: Resources about program design in C
    ... an interest in the reasoning behind global variables in C programs. ... For example, sorting an array is a single, well-defined ... imagine a module that implements a stack ... static int stack_ptr; ...
    (comp.lang.c)
  • Re: Resources about program design in C
    ... an interest in the reasoning behind global variables in C programs. ... A function should isolate its implementation details from the ... imagine a module that implements a stack ... static int stack_ptr; ...
    (comp.lang.c)