Re: can any bdy help me
- From: Chris Dollin <chris.dollin@xxxxxx>
- Date: Thu, 31 May 2007 12:04:47 +0100
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
.
- Follow-Ups:
- Re: can any bdy help me
- From: Christopher Benson-Manica
- Re: can any bdy help me
- References:
- can any bdy help me
- From: shanti
- can any bdy help me
- Prev by Date: can any bdy help me
- Next by Date: Re: ICU Unicode library
- Previous by thread: can any bdy help me
- Next by thread: Re: can any bdy help me
- Index(es):
Relevant Pages
|