#help me with pop function!!

65 messages · Page 1 of 1 (latest)

fresh quailBOT
#

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 more information use !howto ask.

real dove
#

!howto ask

visual lynxBOT
#
How to Ask A Programming Question

Anyone can ask a question in our programming channels. Following the guide Writing The Perfect Question is recommended.

What To Post

State your problem clearly and provide all necessary details:
• the relevant portion of your code, or all of it
• the expected output
• the actual output (or the full error)
🏆 Gold Standard: Minimal Reproducible Example

Where To Post

Provide the relevant code in the message, and format it nicely with a code block*. If it's too much for one message, you can upload it:
Compiler Explorer for most C/C++ snippets
OnlineGDB for interaction, debugging
Do not post screenshots, let alone photos of your screen!

waxen ivy
#

could you show code?

real dove
#
    {
        if (y == nullptr)
        {
            return -128;
        }
        else 
        {   
            return -1
        }
    }

int main(){
        signed char x[100];
        signed char* y = x;
        return 0
}```
#

im still a beginner

thorn atlas
real dove
#

yea

#

still working on it

thorn atlas
# real dove yea

what is the data structure that holds the elements you are storing

#

how are elements added a.k.a. push-ed

#

and how are they poped?

real dove
#

im trying to write stack pop from scratch

#

how pop remove the last element of the array

#

thats what im trying to do

thorn atlas
real dove
#

c++

thorn atlas
real dove
#

not really

#

i can try

thorn atlas
real dove
#
class Stack {
    public:
    signed char pop(void) 
    {
        if (y == nullptr)
        {
            return -128;
        }
        else 
        {   
            return -1
        }
    }    
    private:
        signed char x[10];
        signed char* y = x;

};```
real dove
thorn atlas
#

explain to me in english, what fields does the stack class need in order to track the elements stored in it?

#

you have a signed char of 10 elements

#

a.k.a. the actual storage

#

and what else do you need?

real dove
#

im not a english speaker btw

thorn atlas
real dove
#

pointer the first element

thorn atlas
real dove
#

yes

#

then remove the top or last element

thorn atlas
real dove
#

back to first element?

#

so -1

thorn atlas
#

a number?

real dove
#

Character

#

thats why i use char

thorn atlas
#

the type of the variable y is a pointer to signed char

#

change it to int elementCount please

real dove
#
class Stack {
    public:
    int pop(void) 
    {
        if (elementCount == nullptr)
        {
            return -128;
        }
        else 
        {   
            return -1
        }
    }

    
    private:
        int x[10];
        int elementCount = x;

};```
thorn atlas
#

create a constructor with zero parameters and in there initialize elementCount to zero, a.k.a. empty stack

real dove
#

uninitialzied?

#

im confused what u mean

thorn atlas
real dove
#

i dont want to use stack()

#

i want to write it in scratch

thorn atlas
real dove
#

oh okay

#

now what should we do now

thorn atlas
#

push adds one element, pop removes one element

real dove
#

let do pop first

thorn atlas
#
void pop()
{
  if(this->count == 0)
  {
    --elementCount;
  }
  else
  {
    cout<<"error - stack is empty\n";
    exit(1)
  }
}

void push(int element)
{
  if(count == 10)
  {
    cout<<"error: stack is full\n";
    exit(1);
  }
  else
  {
    this->x[this->elementCount] = element;
    this=>elementCount++;
  }
}

real dove
#

im confused

#

i though u cant do void function in class

thorn atlas
#

including member functions inside a class

thorn atlas
#
class Stack {
    public:

    Stack()
    {
      this->elementCount = 0;
    }

    void pop()
    {
      if(this->count != 0)
      {
        --elementCount;
      }
      else
      {
        cout<<"error - stack is empty\n";
        exit(1)
      }
    }

    void push(int element)
    {
      if(count == 10)
      {
        cout<<"error: stack is full\n";
        exit(1);
      }
      else
      {
        this->x[this->elementCount] = element;
        this=>elementCount++;
      }
    }
    private:
        int x[10];
        int elementCount;

};