How do i output the contents of the stack with the functions Output ?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2007
Posts: 30
Reputation: Reg74 is an unknown quantity at this point 
Solved Threads: 1
Reg74 Reg74 is offline Offline
Light Poster

How do i output the contents of the stack with the functions Output ?

 
0
  #1
Jul 19th, 2007
//Program to demonstrate the Stack class
#include <iostream>
#include <vector>
using namespace std;

template<class T>
class Stack
{
public:
Stack();
bool empty() const;
T top() const;
T pop();
void push(T element);
void Output(T element);
private:
vector <T> pool;
};

//constructor
template<class T>
Stack<T>:tack()
{
pool.reserve(10);
}

//function to output the values in the stack
template<class T>
void Stack<T>::Output(T element)
{

}



int main()
{

Stack<double> dStack;
Stack<string> sStack;

cout << "Pushing 3.4, 5.5, 2.2, 6.7 -> on to dStack" << endl;
dStack.push(3.4);
dStack.push(5.5);
dStack.push(2.2);
dStack.push(6.7);
cout << endl;

cout << "Pushing Sawubona, Molo, Lotjha, Dumela, Hi, Hallo" << endl;
cout << "Ndi Masiari, Avuxeni -> on to sStack" << endl;
sStack.push("Sawubona");
sStack.push("Molo");
sStack.push("Lotjha");
sStack.push("Dumela");
sStack.push("Hi");
sStack.push("Hallo");
sStack.push("Ndi Masiari");
sStack.push("Avuxeni");

cout << endl << "dStack:" << endl;
Output(dStack);
cout << endl << "sStack:" << endl;
Output(sStack);

return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: How do i output the contents of the stack with the functions Output ?

 
0
  #2
Jul 19th, 2007
From the prototype I'd guess this.
  1. template<class T>
  2. void Stack<T>::Output(T element)
  3. {
  4. cout<< element;
  5. }
I don't understand why you have a parameter. It seems you want to output the whole stack.
  1. template<class T>
  2. void Stack<T>::Output()
  3. {
  4. for ( int i = 0; i < pool.size(); ++i ) {
  5. cout<< pool[i] <<endl;
  6. }
  7. }
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 30
Reputation: Reg74 is an unknown quantity at this point 
Solved Threads: 1
Reg74 Reg74 is offline Offline
Light Poster

Re: How do i output the contents of the stack with the functions Output ?

 
0
  #3
Jul 19th, 2007
My compiler is complaining about the call to the function Output as in Output (dStack); saying that Output is undeclared ?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: How do i output the contents of the stack with the functions Output ?

 
0
  #4
Jul 19th, 2007
Right. You prototype Output to take a T and try to pass a Stack<T>. I guess you'd call it like this for the two different functions I wrote.
  1. // Output takes a T parameter
  2. dStack.Output( dStack.top() );
  3.  
  4. // Output takes no parameters
  5. dStack.Output();
Last edited by Hamrick; Jul 19th, 2007 at 10:09 am.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 30
Reputation: Reg74 is an unknown quantity at this point 
Solved Threads: 1
Reg74 Reg74 is offline Offline
Light Poster

Re: How do i output the contents of the stack with the functions Output ?

 
0
  #5
Jul 19th, 2007
Hamrick i'm still getting compiler errors.
Could you copy the code into a compiler to see what it does or is that too much to ask?
I would just love to see what the problem was ?
Thanks for all your inputs thus far.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: How do i output the contents of the stack with the functions Output ?

 
0
  #6
Jul 19th, 2007
I compiled it and everything's like I said. Change how you create Output then change how you call it. I also added a body for push so that it runs, I hope you don't mind.
  1. //Program to demonstrate the Stack class
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. template<class T>
  7. class Stack
  8. {
  9. public:
  10. Stack();
  11. bool empty() const;
  12. T top() const;
  13. T pop();
  14. void push(T element) { pool.push_back( element ); }
  15. void Output();
  16. private:
  17. vector <T> pool;
  18. };
  19.  
  20. //constructor
  21. template<class T>
  22. Stack<T>::Stack()
  23. {
  24. pool.reserve(10);
  25. }
  26.  
  27. //function to output the values in the stack
  28. template<class T>
  29. void Stack<T>::Output()
  30. {
  31. for ( int i = 0; i < pool.size(); ++i ) {
  32. cout<< pool[i] <<endl;
  33. }
  34. }
  35.  
  36. int main()
  37. {
  38.  
  39. Stack<double> dStack;
  40. Stack<string> sStack;
  41.  
  42. cout << "Pushing 3.4, 5.5, 2.2, 6.7 -> on to dStack" << endl;
  43. dStack.push(3.4);
  44. dStack.push(5.5);
  45. dStack.push(2.2);
  46. dStack.push(6.7);
  47. cout << endl;
  48.  
  49. cout << "Pushing Sawubona, Molo, Lotjha, Dumela, Hi, Hallo" << endl;
  50. cout << "Ndi Masiari, Avuxeni -> on to sStack" << endl;
  51. sStack.push("Sawubona");
  52. sStack.push("Molo");
  53. sStack.push("Lotjha");
  54. sStack.push("Dumela");
  55. sStack.push("Hi");
  56. sStack.push("Hallo");
  57. sStack.push("Ndi Masiari");
  58. sStack.push("Avuxeni");
  59.  
  60. cout << endl << "dStack:" << endl;
  61. dStack.Output();
  62. cout << endl << "sStack:" << endl;
  63. dStack.Output();
  64.  
  65. return 0;
  66. }
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 30
Reputation: Reg74 is an unknown quantity at this point 
Solved Threads: 1
Reg74 Reg74 is offline Offline
Light Poster

Re: How do i output the contents of the stack with the functions Output ?

 
0
  #7
Jul 20th, 2007
Thanks alot HAMRICK !!!!!!!!!!!!!!!!!!!!!!!!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC