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