#include<stdio.h>
#include<conio.h>
void push(struct stack);
void pop(struct stack);
void display(struct stack);
struct stack{
int a[5];
int p;
};
int main()
{ int ch;
struct stack st;
st.p=-1;
while(ch!=4)
{
printf("Enter your choice:\n1.Push\n2.Pop\n3.Display\n4.Exit");
scanf("%d",&ch);
switch(ch)
{
case 1: push(st);
break;
case 2: pop(st);
break;
case 3: display(st);
break;
default: printf("Andha hai kya!!");
}
}
getch();
return 0;
}
void push(struct stack st)
{
int x;
if(st.p==4)
{
printf("Stack Overflow.");
}
else
{
printf("Enter element to be input in stack:");
scanf("%d",&x);
st.a[++st.p]=x;
}
}
void pop(struct stack st)
{
if (st.p==-1)
{
printf("Stack Empty");
}
else
{
printf("%d is now popped.",st.a[st.p]);
st.p--;
}
}
void display(struct stack st)
{
int i;
if(st.p==-1)
{
printf("Nothing to display.");
}
else
{
for(i=0;i<=st.p;i++)
{
printf("%d",st.a[i]);
}
}
}
#I cannot display after pushing elements in stack. Please help.
36 messages Β· Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
!f
#include <conio.h>
#include <stdio.h>
void push(struct stack);
void pop(struct stack);
void display(struct stack);
struct stack {
int a[5];
int p;
};
int main() {
int ch;
struct stack st;
st.p = -1;
while (ch != 4) {
printf("Enter your choice:\n1.Push\n2.Pop\n3.Display\n4.Exit");
scanf("%d", &ch);
switch (ch) {
case 1:
push(st);
break;
case 2:
pop(st);
break;
case 3:
display(st);
break;
default:
printf("Andha hai kya!!");
}
}
getch();
return 0;
}
void push(struct stack st) {
int x;
if (st.p == 4) {
printf("Stack Overflow.");
} else {
printf("Enter element to be input in stack:");
scanf("%d", &x);
st.a[++st.p] = x;
}
}
void pop(struct stack st) {
if (st.p == -1) {
printf("Stack Empty");
} else {
printf("%d is now popped.", st.a[st.p]);
st.p--;
}
}
void display(struct stack st) {
int i;
if (st.p == -1) {
printf("Nothing to display.");
} else {
for (i = 0; i <= st.p; i++) {
printf("%d", st.a[i]);
}
}
}
;compile
Compiler Output
<source>:2:10: fatal error: conio.h: No such file or directory
2 | #include <conio.h>
| ^~~~~~~~~
compilation terminated.
Build failed
j_oe.y | c++ | x86-64 gcc 13.2 | godbolt.org
#include <stdio.h>
struct stack {
int a[5];
int p;
};
void push(struct stack);
void pop(struct stack);
void display(struct stack);
int main() {
int ch;
struct stack st;
st.p = -1;
while (ch != 4) {
printf("Enter your choice:\n1.Push\n2.Pop\n3.Display\n4.Exit");
scanf("%d", &ch);
switch (ch) {
case 1:
push(st);
break;
case 2:
pop(st);
break;
case 3:
display(st);
break;
default:
printf("Andha hai kya!!");
}
}
getch();
return 0;
}
void push(struct stack st) {
int x;
if (st.p == 4) {
printf("Stack Overflow.");
} else {
printf("Enter element to be input in stack:");
scanf("%d", &x);
st.a[++st.p] = x;
}
}
void pop(struct stack st) {
if (st.p == -1) {
printf("Stack Empty");
} else {
printf("%d is now popped.", st.a[st.p]);
st.p--;
}
}
void display(struct stack st) {
int i;
if (st.p == -1) {
printf("Nothing to display.");
} else {
for (i = 0; i <= st.p; i++) {
printf("%d", st.a[i]);
}
}
}
;compile
Compiler Output
<source>:1:10: fatal error: conio.h: No such file or directory
1 | #include <conio.h>
| ^~~~~~~~~
compilation terminated.
Build failed
j_oe.y | c | x86-64 gcc 13.2 | godbolt.org
an outdated DOS header one shouldn't use today
It is header file with full name console input output
im just going to read ur code and ignore it
Pls help
;compile
Compiler Output
<source>:3:18: warning: 'struct stack' declared inside parameter list will not be visible outside of this definition or declaration
3 | void push(struct stack);
| ^~~~~
<source>:4:17: warning: 'struct stack' declared inside parameter list will not be visible outside of this definition or declaration
4 | void pop(struct stack);
| ^~~~~
<source>:5:21: warning: 'struct stack' declared inside parameter list will not be visible outside of this definition or declaration
5 | void display(struct stack);
| ^~~~~
<source>: In function 'main':
<source>:21:14: error: type of formal parameter 1 is incomplete
21 | push(st);
| ^~
<source>:24:13: error: type of formal parameter 1 is incomplete
24 | pop(st);
| ^~
<source>:27:17: error: type of formal parameter 1 is incomplete
27 | display(st);
| ^~
<source>:33:3: warning: impli
j_oe.y | c | x86-64 gcc 13.2 | godbolt.org
;compile
Compiler Output
<source>: In function 'main':
<source>:34:3: warning: implicit declaration of function 'getch'; did you mean 'getc'? [-Wimplicit-function-declaration]
34 | getch();
| ^~~~~
| getc
/opt/compiler-explorer/gcc-13.2.0/bin/../lib/gcc/x86_64-linux-gnu/13.2.0/../../../../x86_64-linux-gnu/bin/ld: /tmp/ccX0AkiT.o: in function β`main':
<source>:34: undefined reference to β`getch'
collect2: error: ld returned 1 exit status
Build failed
j_oe.y | c | x86-64 gcc 13.2 | godbolt.org
@tawdry warren why are u using getch
if u were not to use conio.h what would you do instead?
In turbo c++ its compulsory
well i know your issue lol
ur passing the struct by value
u need to pass the address of the struct so you can modify it in other functions
ur currently creating a copy
@tawdry warren do you know how pointers work
also you can't put your function prototypes on top of the stack declaration
πππππππ
Thank you my friend
Youre cool af ππ
@tawdry warren Has your question been resolved? If so, type !solved :)
!solved