stack

R

rhle.freak

This is a very simple implementation of push and pop functions of a
stack.ive compiled it on msvc++ on windows platform and it wrks fine.
ive used 'int' as the data for the push operations,however if i enter a
floating point number during runtime,it does not break out of the the
infinite loop.
what should i do in order to prevent this ??


#include<stdio.h>
#include<stdlib.h>
#define MAX 4

struct stack{
int top;
int item[MAX];
};

void push(struct stack *p,int n)
{
if(p->top==MAX-1)
printf("overflow\n");
else
p->item[++p->top]=n;
return;
}

void pop(struct stack *p)
{
if(p->top==-1)
printf("underflow\n");
else
{
printf("deleted number is %d\n",p->item[p->top]);
--p->top;
}
return;
}

void display(struct stack *p)
{
int i;
if(p->top==-1)
printf("stack is empty\n");
else
{
for(i=p->top;i>=0;i--)
printf("%d\n",p->item);
}
return;
}

int main(void)
{
int num=0,choice;
struct stack s;
s.top=-1;
for(;;)
{
printf("Menu");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n5.Clear screen\n");
printf("Enter your choice :");
scanf_s("%d",&choice);

switch(choice)
{
case 1:
printf("enter the number to push");
scanf_s("%d",&num);
push(&s,(int)num);
break;
case 2:
pop(&s);
break;
case 3:
display(&s);
break;
case 4:
exit(0);
case 5:
system("cls");
default:
printf("wrong choice\n");
}
}
return 0;
}
 
X

Xavier Serrand

rhle.freak said:
This is a very simple implementation of push and pop functions of a
stack.ive compiled it on msvc++ on windows platform and it wrks fine.
ive used 'int' as the data for the push operations,however if i enter a
floating point number during runtime,it does not break out of the the
infinite loop.
what should i do in order to prevent this ??


#include<stdio.h>
#include<stdlib.h>
#define MAX 4

struct stack{
int top;
int item[MAX];
};

void push(struct stack *p,int n)
{
if(p->top==MAX-1)
printf("overflow\n");
else
p->item[++p->top]=n;
return;
}

void pop(struct stack *p)
{
if(p->top==-1)
printf("underflow\n");
else
{
printf("deleted number is %d\n",p->item[p->top]);
--p->top;
}
return;
}

void display(struct stack *p)
{
int i;
if(p->top==-1)
printf("stack is empty\n");
else
{
for(i=p->top;i>=0;i--)
printf("%d\n",p->item);
}
return;
}

int main(void)
{
int num=0,choice;
struct stack s;
s.top=-1;
for(;;)
{
printf("Menu");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n5.Clear screen\n");
printf("Enter your choice :");

scanf_s("%d",&choice);
//not standard c ...
//if you type 98.23 ... in the push function, 98 is read ... and .23 is left
in the input buffer ... for ever ...
// next time you go here ... .23 is available ... then go on with choice 1
....
switch(choice)
{
case 1:
printf("enter the number to push");
scanf_s("%d",&num);
//double value in int variable ... not a good idea ...
 
C

CBFalconer

rhle.freak said:
This is a very simple implementation of push and pop functions of a
stack.ive compiled it on msvc++ on windows platform and it wrks fine.
ive used 'int' as the data for the push operations,however if i enter a
floating point number during runtime,it does not break out of the the
infinite loop.
what should i do in order to prevent this ??

Check the return code from scanf, and test the field termination
char.
#include<stdio.h>
#include<stdlib.h>
#define MAX 4

struct stack{
int top;
int item[MAX];
};

void push(struct stack *p,int n)
{
if(p->top==MAX-1)
printf("overflow\n");
else
p->item[++p->top]=n;
return;
}

void pop(struct stack *p)
{
if(p->top==-1)
printf("underflow\n");
else
{
printf("deleted number is %d\n",p->item[p->top]);
--p->top;
}
return;
}

void display(struct stack *p)
{
int i;
if(p->top==-1)
printf("stack is empty\n");
else
{
for(i=p->top;i>=0;i--)
printf("%d\n",p->item);
}
return;
}

int main(void)
{
int num=0,choice;
struct stack s;
s.top=-1;
for(;;)
{
printf("Menu");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n5.Clear screen\n");
printf("Enter your choice :");
scanf_s("%d",&choice);

switch(choice)
{
case 1:
printf("enter the number to push");
scanf_s("%d",&num);
push(&s,(int)num);
break;
case 2:
pop(&s);
break;
case 3:
display(&s);
break;
case 4:
exit(0);
case 5:
system("cls");
default:
printf("wrong choice\n");
}
}
return 0;
}
 
J

jaysome

This is a very simple implementation of push and pop functions of a
stack.ive compiled it on msvc++ on windows platform and it wrks fine.
ive used 'int' as the data for the push operations,however if i enter a
floating point number during runtime,it does not break out of the the
infinite loop.
what should i do in order to prevent this ??


#include<stdio.h>
#include<stdlib.h>
#define MAX 4

struct stack{
int top;
int item[MAX];
};

void push(struct stack *p,int n)
{
if(p->top==MAX-1)
printf("overflow\n");
else
p->item[++p->top]=n;
return;
}

void pop(struct stack *p)
{
if(p->top==-1)
printf("underflow\n");
else
{
printf("deleted number is %d\n",p->item[p->top]);
--p->top;
}
return;
}

void display(struct stack *p)
{
int i;
if(p->top==-1)
printf("stack is empty\n");
else
{
for(i=p->top;i>=0;i--)
printf("%d\n",p->item);
}
return;
}

int main(void)
{
int num=0,choice;
struct stack s;

/* added variables*/
int done = 0;
int num_scanned;
s.top=-1;
for(;;) while ( !done )
{
printf("Menu");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n5.Clear screen\n");
printf("Enter your choice :");
scanf_s("%d",&choice);
/* As others have pointed out, scanf_s() is not a standard C function
(it's a Microsoft extension "with security enhancements"), and always
check the return value of scanf().*/
num_scanned = scanf("%d",&choice);
if ( num_scanned == 1 )
switch(choice)
{
case 1:
printf("enter the number to push");
scanf_s("%d",&num);
push(&s,(int)num);
break;
case 2:
pop(&s);
break;
case 3:
display(&s);
break;
case 4:
exit(0);
/*Change that to:*/
done = 1;
break;
case 5:
system("cls");
Oops. Warning 616: control flows into case/default
default:
printf("wrong choice\n");
}
}
return 0;
/* If you keep exit(0), then:*/
Warning 527: Unreachable code at token 'return'
 
R

rhle.freak

This is a very simple implementation of push and pop functions of a
stack.ive compiled it on msvc++ on windows platform and it wrks fine.
ive used 'int' as the data for the push operations,however if i enter a
floating point number during runtime,it does not break out of the the
infinite loop.
what should i do in order to prevent this ??
#include<stdio.h>
#include<stdlib.h>
#define MAX 4
struct stack{
int top;
int item[MAX];
};
void push(struct stack *p,int n)
{
if(p->top==MAX-1)
printf("overflow\n");
else
p->item[++p->top]=n;
return;
}
void pop(struct stack *p)
{
if(p->top==-1)
printf("underflow\n");
else
{
printf("deleted number is %d\n",p->item[p->top]);
--p->top;
}
return;
}
void display(struct stack *p)
{
int i;
if(p->top==-1)
printf("stack is empty\n");
else
{
for(i=p->top;i>=0;i--)
printf("%d\n",p->item);
}
return;
}

int main(void)
{
int num=0,choice;
struct stack s;/* added variables*/
int done = 0;
int num_scanned;> s.top=-1;
for(;;) while ( !done )> {
printf("Menu");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n5.Clear screen\n");
printf("Enter your choice :");
scanf_s("%d",&choice);/* As others have pointed out, scanf_s() is not a standard C function
(it's a Microsoft extension "with security enhancements"), and always
check the return value of scanf().*/
num_scanned = scanf("%d",&choice);
if ( num_scanned == 1 )


switch(choice)
{
case 1:
printf("enter the number to push");
scanf_s("%d",&num);
push(&s,(int)num);
break;
case 2:
pop(&s);
break;
case 3:
display(&s);
break;
case 4:
exit(0);/*Change that to:*/
done = 1;
break;> case 5:
system("cls");Oops. Warning 616: control flows into case/default> default:
printf("wrong choice\n");
}
}
return 0;/* If you keep exit(0), then:*/
Warning 527: Unreachable code at token 'return'
jay


this might seem to be a trivial solution to the problem but simply
clearing the buffer before the push operation is called in main solves
the problem!
now if i enter a floating point number during runtime it simply pushes
the integer part n on entering any other character it pushes '0' into
the stack.

int main(void)
{
int num=0,choice;
struct stack s;
s.top=-1;
for(;;)
{
printf("Menu");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n5.Clear screen\n");
printf("Enter your choice :");
scanf_s("%d",&choice);

switch(choice)
{
case 1:
printf("enter the number to push");
scanf_s("%d",&num); //i know scanf_s is not standard c, but i
get the warning "scanf declared

//deprecated use scanf_s instead"
fflush(stdin); //here is the change
push(&s,(int)num);
break;
case 2:
pop(&s);
break;
case 3:
display(&s);
break;
case 4:
exit(0);
case 5:
system("cls");
break; //yes i missed the break
here..
default:
printf("wrong choice\n");
}
}
return 0; // i did not get
any code unreachable warning !!
}


I am a newbie so please do comment if this is the right way to do it?
 
C

CBFalconer

rhle.freak said:
.... snip ...

switch(choice)
{
case 1:
printf("enter the number to push");
scanf_s("%d",&num); //i know scanf_s is not standard c, but i
get the warning "scanf declared

//deprecated use scanf_s instead"
fflush(stdin); //here is the change
push(&s,(int)num);
break;

fflush does not operate on input files. This is undefined
behaviour. Since scanf leaves the field termination char in the
input stream, you can use the following routine instead, and know
that it works everywhere:

int flushln(FILE *f) {
int ch;
while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;
}

Always check the return value from scanf. Don't use // comments in
material for usenet, they wrap and make the code uncompilable. Use
/* ... */ instead. Don't use tabs, (they cause excessive
indentation and may be totally removed during article processing)
use spaces. Do keep line lengths under 72, better 65, characters
for usenet posts.

scanf is standard. scanf_s is a Microsoftism designed to reduce
the inordinate number of coding mistakes they make while and to
lock people into Microsoft products, and is not a part of standard
C. Never trust Microsoft software.
 
R

rhle.freak

fflush does not operate on input files. This is undefined
behaviour. Since scanf leaves the field termination char in the
input stream, you can use the following routine instead, and know
that it works everywhere:

int flushln(FILE *f) {
int ch;
while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;

}Always check the return value from scanf. Don't use // comments in
material for usenet, they wrap and make the code uncompilable. Use
/* ... */ instead. Don't use tabs, (they cause excessive
indentation and may be totally removed during article processing)
use spaces. Do keep line lengths under 72, better 65, characters
for usenet posts.

scanf is standard. scanf_s is a Microsoftism designed to reduce
the inordinate number of coding mistakes they make while and to
lock people into Microsoft products, and is not a part of standard
C. Never trust Microsoft software.


thanks a lot ,that was really helpful.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top