944,185 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2422
  • C++ RSS
Jul 26th, 2005
0

linked stack question

Expand Post »
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)
  1. #include<conio.h>
  2. #include<stdio.h>
  3. #include<iostream.h>
  4. #include<string.h>
  5.  
  6. class link_stack
  7.  
  8. {
  9.  
  10. struct node
  11.  
  12. {
  13.  
  14. int id;
  15.  
  16. char name[10];
  17.  
  18. node *next;
  19.  
  20. };
  21.  
  22. node *top,*x,*ptr;
  23.  
  24. public:
  25.  
  26. link_stack()
  27.  
  28. {
  29.  
  30. top=x=ptr=NULL;
  31.  
  32. }
  33.  
  34. void push()
  35.  
  36. {
  37.  
  38. x=new node;
  39.  
  40. cout << "Enter an ID number and name: ";
  41.  
  42. cin >> x->id >> x->name;
  43.  
  44.  
  45. x->next=top;
  46.  
  47. top=x;
  48.  
  49. }
  50.  
  51. int pop(char n[])
  52.  
  53. { int result;
  54.  
  55. if(top==NULL)
  56.  
  57. cout<<"\nStack is Empty";
  58.  
  59. else
  60.  
  61. { result = top->id;
  62.  
  63. strcpy(n, top->name);
  64.  
  65. x=top;
  66.  
  67. top=top->next;
  68.  
  69. delete x;
  70.  
  71. return result;
  72.  
  73. }
  74.  
  75. }
  76.  
  77. void empty()
  78. {
  79.  
  80. char name[10];
  81.  
  82. while(!obj.empty())
  83.  
  84. {
  85.  
  86. cout << pop(name) << ": " << name << endl;
  87.  
  88. }
  89.  
  90. }
  91.  
  92. };
  93.  
  94. void main()
  95.  
  96. {
  97.  
  98. link_stack obj;
  99.  
  100. int choice;
  101.  
  102. do
  103.  
  104. {
  105.  
  106. cout << "\n ----------MENU---------- \n";
  107.  
  108. cout << "1.Push\n"
  109.  
  110. << "2.Pop\n"
  111.  
  112. << "3.Exit";
  113.  
  114. cout << "\nEnter your choice: ";
  115.  
  116. cin>>choice;
  117.  
  118. switch(choice)
  119.  
  120. {
  121.  
  122. case 1: obj.push();
  123.  
  124. break;
  125.  
  126. case 2: obj.pop();
  127.  
  128. obj.empty();
  129.  
  130. break;
  131.  
  132. case 3: cout << endl;
  133.  
  134. }
  135.  
  136. }
  137.  
  138. while (choice!=3);
  139.  
  140. getch();
  141.  
  142. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yaan is offline Offline
8 posts
since Jun 2005
Jul 27th, 2005
0

Re: linked stack question

Here we go.
C++ Syntax (Toggle Plain Text)
  1. #include<conio.h> //Non standard header do you really "need" it?
  2.  
  3. //This is the way the headers are no .h's
  4. #include<cstdio>
  5. #include<iostream>
  6. #include<string>
  7. //Got to put it in the std namespace
  8. using namespace std;
  9. //I would recommend some minor style changes.
  10. class link_stack
  11. {
  12. struct node
  13. {
  14. int id;
  15. char name[10];
  16. node *next;
  17. };
  18.  
  19. node *top,*x,*ptr;
  20.  
  21. public:
  22. link_stack()
  23. {
  24. top = x = ptr =NULL;
  25. }
  26. void push()
  27. {
  28. x = new node;
  29.  
  30. cout << "Enter an ID number and name: ";
  31. cin >> x->id >> x->name;
  32.  
  33. x->next = top;
  34. top = x;
  35. }
  36. int pop(char n[])
  37. {
  38. int result;
  39. if(top==NULL)
  40. {
  41. cout<<"\nStack is Empty";
  42. }
  43. else
  44. {
  45. result = top->id;
  46. strcpy(n, top->name);
  47. x = top;
  48. top = top->next;
  49. delete x;
  50. return result;
  51. }
  52.  
  53. }
  54. void empty()//What is this function really doing?
  55. {
  56. char name[10];
  57.  
  58. //while(!obj.empty()) Where is obj declared?
  59. //{
  60. // cout << pop(name) << ": " << name << endl;
  61. //}
  62.  
  63. }
  64.  
  65. };
  66.  
  67. int main()//It is int main not void main!!!!!!!!!!!!!!!!!!!!!
  68. {
  69. link_stack obj;
  70. int choice;
  71. do
  72. {
  73. cout << "\n ----------MENU---------- \n";
  74. cout << "1.Push\n"
  75. << "2.Pop\n"
  76. << "3.Exit";
  77. cout << "\nEnter your choice: ";
  78. cin>>choice;
  79.  
  80. switch(choice)
  81. {
  82. case 1:
  83. obj.push();
  84. break;
  85. case 2:
  86. //obj.pop(); Is this how you defined pop?
  87. obj.empty();
  88. break;
  89. case 3:
  90. cout << endl;
  91. break;
  92. }
  93.  
  94. }
  95. while (choice != 3);
  96.  
  97. getch(); //Why not cin.get()?
  98. //cin.ignore();
  99. //cin.get();
  100. return 0; //Need to return a value from main.
  101. }
Reputation Points: 14
Solved Threads: 4
Junior Poster
prog-bman is offline Offline
108 posts
since Nov 2004
Jul 27th, 2005
0

Re: linked stack question

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??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yaan is offline Offline
8 posts
since Jun 2005
Jul 27th, 2005
0

Re: linked stack question

>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:
C++ Syntax (Toggle Plain Text)
  1. void link_stack::print_all()
  2. {
  3. for ( node *p = top; p != 0; p = p->next )
  4. cout<< p->id <<": "<< p->name <<'\n';
  5. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Multimap or other ?
Next Thread in C++ Forum Timeline: i/o files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC