I need to have my code modified, but am unsure how to write the code to do it. After the ID and name are poped from the stack, I need the program to automatically display the list created i.e. I don't want to use display as an option.

I think I need to have main function loop back after pop and display are done. How can I do this?

Here is the code:

#include<conio.h>
#include<stdio.h>
#include <iostream.h>

class link_stack
	
{

	struct node
	
	{

		int id;

		char name[10];

		node *next;

	};

	node *top,*x,*ptr;

	public:

		link_stack()

		{

			top=x=ptr=NULL;

		}

		void push()

		{

			x=new node;

			cout << "Enter an ID number and name: ";

			cin >> x->id >> x->name;


			x->next=top;

			top=x;

		}

		void pop()

		{

			if(top==NULL)

				cout<<"\nStack is Empty";

			else

			{

				x=top;

				top=top->next;

				delete x;

			}

		}

		void display()

		{

			ptr=top;

			while(ptr!=NULL)

			{

				cout << "\nName: "<< ptr->name;

				cout<<"\nID number: "<<ptr->id;

				ptr=ptr->next;

			}

		}

};

void main()

{

	link_stack obj;

	int choice;

	do

	{

		cout << "\n ----------MENU---------- \n";

		cout << "1.Push\n"
			
			 <<  "2.Pop\n"
			   
			 <<  "3.Display\n"
			   
			 <<  "4.Exit";

			   cout << "\nEnter your choice: ";

		cin>>choice;

		switch(choice)

		{

		case 1: obj.push();

			break;

		case 2: obj.pop();

			break;

		case 3: obj.display();

			break;

		case 4: cout << endl;

		}

	}

	while (choice!=4);

	getch();

}

Code tags added. -Narue

Dani AI

Generated

Short answer: you can remove a separate display() only if some other function produces the visible output. The clean choices are (a) make pop() return the popped data to the caller, (b) make pop() accept output parameters and fill them, or (c) have pop() print the popped item itself. Those keep the stack internals private while letting the program show the removed element automatically. ’s menu fall-through trick is a quick way to call display() after pop() from the menu, but if the goal is to eliminate display() entirely the options below are clearer and safer.

Practical options (illustrative prototypes):

/* C++17: return an optional pair so caller can print */
std::optional<std::pair<int,std::string>> pop();

Usage:

if (auto item = s.pop()) {
  std::cout << "Popped ID: " << item->first << " Name: " << item->second << '\n';
}
/* Pre-C++17: output parameters */
bool pop(int &id, std::string &name);

Usage:

int id; std::string name;
if (s.pop(id,name)) std::cout << "Popped " << id << ' ' << name << '\n';
/* Simpler: pop prints the removed element itself */
void pop_and_print();

Small practical tips and cautions: prefer std::string to fixed char arrays, use standard headers (<iostream>) and int main() returning 0, and always test for empty stack before accessing pointers. If memory management is a concern use a destructor to free nodes or switch to smart pointers or the STL std::stack/std::vector. Returning data from pop() (option 1 or 2) is generally best for testability and keeps responsibilities clear: modification of the data structure stays inside the class, while output stays in the caller. For C++17 details about std::optional see std::optional docs.

Recommended Answers

All 3 Replies

Use code tags for any future code. I've added them for you this time, but next time I may just feel the need to delete your post if you fail to do it properly.

>After the ID and name are poped from the stack, I need the program to
>automatically display the list created
Change this:

case 2: obj.pop();
  break;
case 3: obj.display();
  break;

To this:

case 2: obj.pop();
case 3: obj.display();
  break;

By default, cases will fall through to the next case if you don't use a break statement. So if you want to display the stack after every pop, simply omit the break on the pop case.

If that's not what you want, be more specific.

More specifically, how can I get rid of display () and still "display" poped entries after each pop.

Thanks.

>More specifically, how can I get rid of display () and still "display" poped entries after each pop.
That's easy: you don't. Since the data members of your class are private (as they should be), the only way to display them is with a public member function or a friend function. Either way you would have the equivalent of link_stack::display().

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.