can any bdy help me

S

shanti

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

#define SIZE 6
int top=-1;

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

start: //here it starts
clrscr();
printf("\nEnter 1 to continue: ");
scanf("%d",&choice);

if(choice==1) {
push(stk_ptr);
printf("\nTo continue press 1 to exit 0:");
scanf("%d",&option);
if(option==1) goto start;
}
else{
printf("CAUTION:WRONG CHOICE, ENTER 1 TO CONTINUE 0 TO EXIT:
");
fflush(stdin);
scanf("%d",&S_R_NO);
if(S_R_NO==1) goto start;
}

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

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

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));
}
}
/
*---------------------------------------------------------------------------
*/
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.

thaking you.
regards
thandra.
 
C

Chris Dollin

shanti said:
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.

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 ...

.... 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.
 
C

Christopher Benson-Manica

Chris Dollin said:
shanti wrote:
int stack[SIZE], *stk_ptr;
int choice;
int S_R_NO,option;
All these uninitialised variables make me nervous.

I'm not sure what one would initialize choice and option to; from
their names alone it would seem to be clear that they are intended to
store input from the user; my religion indicates that initialization
is therefore optional at best. (S_R_NO, on the other hand, might be
profitably initialized, although the question of "to what" is not easy
to answer without deciphering its very poor name.)
DON'T SHOUT AT ME, YOU STUPID MACHINE.

My Commodore 64 shouted at me a lot, and was only pacified by magical
incantations such as "LOAD *, 8, 1" (or some such nonsense) :)
Um. If you weren't going to let me not-continue, why did you
offer me a choice in the first place?

For the same reason that users always got the option to "abort, retry,
fail" no matter how much the real choices might have been "fail, fail,
fail"...
 
O

osmium

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

He may have interned at Microsoft. They are experts at promising things
that won't happen. Press "Cancel" on a download for example.
 
M

mark_bluemel

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top