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.
65 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 more information use !howto ask.
!howto ask
Anyone can ask a question in our programming channels. Following the guide Writing The Perfect Question is recommended.
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
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!
could you show code?
{
if (y == nullptr)
{
return -128;
}
else
{
return -1
}
}
int main(){
signed char x[100];
signed char* y = x;
return 0
}```
im still a beginner
this does not compile
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?
im trying to write stack pop from scratch
how pop remove the last element of the array
thats what im trying to do
are you writing in C, or C++ ?
c++
do you know how to write classes?
do you know what a class is and what it is used for in C++ ?
class Stack {
public:
signed char pop(void)
{
if (y == nullptr)
{
return -128;
}
else
{
return -1
}
}
private:
signed char x[10];
signed char* y = x;
};```
store stuff and can use for function?
well, yea
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?
im not a english speaker btw
what is signed char* y used for here?
pointer the first element
so you use is to track which element is on the top of the stack?
what is the value of y when your stack is empty?
that is not true
the type of the variable y is a pointer to signed char
change it to int elementCount please
class Stack {
public:
int pop(void)
{
if (elementCount == nullptr)
{
return -128;
}
else
{
return -1
}
}
private:
int x[10];
int elementCount = x;
};```
leave elementCount uninitialized
create a constructor with zero parameters and in there initialize elementCount to zero, a.k.a. empty stack
class Stack {
public:
int pop(void)
{
//TODO
}
Stack()
{
this->elementCount = 0;
}
private:
int x[10];
int elementCount;
};
I am writing it from scratch
create two functions, push and pop
push adds one element, pop removes one element
let do pop first
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++;
}
}
any function can use the return type void
including member functions inside a class
which line
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;
};