I am doing a simple program that will add elements to a static stack using structures but Iam failing to implement it. The proram accepts input the first time but then crashes after that. Here is my structure declaration

struct stack{
	int top=1;
	int values[4];
};

the definition for the push function

void push(stack& s, int x)
{

	cout<<"enter: ";
	cin>>x;
	cout<<endl;
	s.top=s.top+1;
	s.values[s.top]=x;

}

then i call it in main

int main(stack& s, int x)
{
          push(s,x);
 return 0;
}

Thanks for any help

> int top=1;
That shouldn't even compile. You can't initialize class or struct fields except in a few specific cases, and this isn't one of those cases.

> s.top=s.top+1;
> s.values[s.top]=x;
top is 1 to begin with, then you increment it before assigning the new value. That means you've lost the first two elements of the array. top should start out at 0 and then increment after assigning to the array.

> int main(stack& s, int x)
The definition of main isn't free-form. You have two options:

int main()
int main(int argc, char *argv[])

Here's a full example to get you started:

#include <iostream>

struct stack {
  int values[4];
  int top;
};

bool push(stack& s, int x)
{
  // Don't push beyond the array
  if (s.top >= 4)
    return false;

  // Assign to the current empty element,
  // then increment to the next empty element
  s.values[s.top++] = x;

  return true;
}

bool pop(stack& s, int& x)
{
  // Don't pop beyond the array
  if (s.top == 0)
    return false;

  // Decrement from the current empty element
  // to the last occupied element before assigning
  x = s.values[--s.top];

  return true;
}

int main()
{
  stack s;

  // Don't forget to set top to the first element
  s.top = 0;

  // You can try to push as many times as you want,
  // but nothing will happen if the stack is full
  for (int i = 0; i < 10; ++i)
    push(s, i);

  int x;

  while (pop(s, x))
    std::cout << x << '\n';
}

Thanx for the reply. Well the problem was that I thought you need to insert the numbers from the function or something. The way you used it in the main is actually easy but it has answered the question.

One more question though. Is the pop used for displaying and deleting the info in the stack or is there any other way to display the information.

> Is the pop used for displaying and deleting the info in the stack or is there any other way to display the information.
It's up to you. :) The STL stack doesn't return the value with pop(), you have to use a top() method, and there's an empty() method for checking if there are any items to pop:

#include <iostream>
#include <stack>

int main()
{
  std::stack<int> s;

  for (int i = 0; i < 10; ++i)
    s.push(i);

  while (!s.empty()) {
    std::cout << s.top() << '\n';
    s.pop();
  }
}

The purest variation of a stack only allows push and pop operations, but that's not always practical. When Edward writes a stack class for general use, it usually has multiple push and pop methods, rotations, swaps and other useful operations. :)

Well You can manually Implement You own Function.

void display (stack& s)
{
for (int x=0; x <s.top;x++)
{
std::cout <<s.values[x]<<"\n";
}

This is for external stack displaying. you can write a class function that will do the same without taking any argument.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.