| | |
linked stack question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2005
Posts: 8
Reputation:
Solved Threads: 0
I'm confused as to why my program is turning out errors when I try to compile the code. Please help.
C++ Syntax (Toggle Plain Text)
#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(); }
•
•
Join Date: Nov 2004
Posts: 108
Reputation:
Solved Threads: 3
Here we go.
C++ Syntax (Toggle Plain Text)
#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. }
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
>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:
>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:
C++ Syntax (Toggle Plain Text)
void link_stack::print_all() { for ( node *p = top; p != 0; p = p->next ) cout<< p->id <<": "<< p->name <<'\n'; }
I'm here to prove you wrong.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Multimap or other ?
- Next Thread: i/o files
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






