I'm confused as to why my program is turning out errors when I try to compile the code. Please help.

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<string.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;

		}

		int pop(char n[])

                {    int result;

                        if(top==NULL)

                                cout<<"\nStack is Empty";

                        else

                        {    result = top->id;
                                
                                strcpy(n, top->name);

                                x=top;

                                top=top->next;

                                delete x;

                                return result;

                        }

                }

	    void empty()
{

                 char name[10];
                            
                         while(!obj.empty())
                                        
                         {

                                cout << pop(name) << ": " << name << endl;

                          }

                 }
			
};

void main()

{

	link_stack obj;

	int choice;

	do

	{

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

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

	             cout << "\nEnter your choice: ";

		cin>>choice;

		switch(choice)

		{

		case 1: obj.push();

			break;

		case 2: obj.pop();

		obj.empty();

			break;

		case 3: cout << endl;

		}

	}

	while (choice!=3);

	getch();

}

Recommended Answers

All 3 Replies

Here we go.

#include<conio.h>   //Non standard header do you really "need" it?

//This is the way the headers are no .h's
#include<cstdio>
#include<iostream>
#include<string>
//Got to put it in the std namespace
using namespace std;
//I would recommend some minor style changes.
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;
		}
		int pop(char n[])
        {    
            int result;
            if(top==NULL)
            {
                cout<<"\nStack is Empty";
            }
            else
            {    
                result = top->id;
                strcpy(n, top->name);
                x = top;
                top = top->next;
                delete x;
                return result;
            }

        }
	    void empty()//What is this function really doing?
        {
            char name[10];
                            
            //while(!obj.empty()) Where is obj declared? 
            //{
            //    cout << pop(name) << ": " << name << endl;
            //}

        }
			
};

int main()//It is int main not void main!!!!!!!!!!!!!!!!!!!!!
{
    link_stack obj;
	int choice;
	do
	{
        cout << "\n ----------MENU---------- \n";
		cout << "1.Push\n"
			 << "2.Pop\n"
			 << "3.Exit";
        cout << "\nEnter your choice: ";
		cin>>choice;

		switch(choice)
		{
            case 1:
                obj.push();
			    break;
		    case 2:
                 //obj.pop(); Is this how you defined pop?
		         obj.empty();
			    break;
		    case 3:
                cout << endl;
                break;
		}

	}
	while (choice != 3);

	getch();   //Why not cin.get()?
    //cin.ignore();
    //cin.get();
    return 0;   //Need to return a value from main. 
}

Thanks for the feedback. I was trying to create a function empty() to display the entire list of entries after a pop, and if no entries then not to display anything.

I think I'm not doing it right?! any suggestions??

>I was trying to create a function empty() to display the
>entire list of entries after a pop
That's a poor name for the function. People expect a function with the name empty(), to tell them whether or not the stack is empty, not print the contents of the stack. But you can do it like this:

void link_stack::print_all()
{
  for ( node *p = top; p != 0; p = p->next )
    cout<< p->id <<": "<< p->name <<'\n';
}
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.