#I cannot display after pushing elements in stack. Please help.

36 messages Β· Page 1 of 1 (latest)

tawdry warren
#
#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]);
         }
         }
}
summer ibexBOT
#

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.

summer ibexBOT
#

#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]);
    }
  }
}

Azen
oak prairieBOT
#
Compiler Output
<source>:2:10: fatal error: conio.h: No such file or directory
    2 | #include <conio.h>
      |          ^~~~~~~~~
compilation terminated.
Build failed
atomic pasture
#
#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]);
    }
  }
}
oak prairieBOT
#
Compiler Output
<source>:1:10: fatal error: conio.h: No such file or directory
    1 | #include <conio.h>
      |          ^~~~~~~~~
compilation terminated.
Build failed
atomic pasture
#

@tawdry warren

#

what is conio.h

warped veldt
#

an outdated DOS header one shouldn't use today

tawdry warren
atomic pasture
tawdry warren
#

Pls help

oak prairieBOT
#
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
oak prairieBOT
#
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
atomic pasture
#

@tawdry warren why are u using getch

#

if u were not to use conio.h what would you do instead?

tawdry warren
#

In turbo c++ its compulsory

atomic pasture
#

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

tawdry warren
#

Dam

#

I never thought of that

#

Lemme try

atomic pasture
#

also you can't put your function prototypes on top of the stack declaration

tawdry warren
#

Thank you my friend

#

Youre cool af 😎😎

summer ibexBOT
#

@tawdry warren Has your question been resolved? If so, type !solved :)

tawdry warren
#

!solved