944,187 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1324
  • C++ RSS
Nov 28th, 2005
0

please check my code and help me

Expand Post »
i try to write a project program about stack. i degined
In stack.h templates for stack, push and pop, and defined two stack objects one for integer and other for Float in main.cpp to get following types of output, but my but program does not compiles and give error. source coding files are:

stack.h

C++ Syntax (Toggle Plain Text)
  1. //stack.h
  2. #pragma once
  3. template <class T>
  4. class Stack
  5. {
  6. public:
  7. Stack(int = 10) ;
  8. ~Stack() { delete [] stackPtr ; }
  9. int push(const T&);
  10. int pop(T&) ; // pop an element off the stack
  11. int isEmpty()const { return top == -1 ; }
  12. int isFull() const { return top == size - 1 ; }
  13. private:
  14. int size ; // Number of elements on Stack
  15. int top ;
  16. T* stackPtr ;
  17. } ;
  18.  
  19. //constructor with the default size 10
  20. template <class T>
  21. Stack<T>::Stack(int s)
  22. {
  23. size = s > 0 && s < 1000 ? s : 10 ;
  24. top = -1 ; // initialize stack
  25. stackPtr = new T[size] ;
  26. }
  27. // push an element onto the Stack
  28. template <class T>
  29. int Stack<T>::push(const T& item)
  30. {
  31. if (!isFull())
  32. {
  33. stackPtr[++top] = item ;
  34. return 1 ; // push successful
  35. }
  36. return 0 ; // push unsuccessful
  37. }
  38.  
  39. // pop an element off the Stack
  40. template <class T>
  41. int Stack<T>::pop(T& popValue)
  42. {
  43. if (!isEmpty())
  44. {
  45. popValue = stackPtr[top--] ;
  46. return 1 ; // pop successful
  47. }
  48. return 0 ; // pop unsuccessful
  49. }


main.cpp:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include "stack.h"
  3. using namespace std ;
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.  
  8. typedef Stack<float> FloatStack ;
  9. typedef Stack<int> IntStack ;
  10.  
  11. //declear two object of class stack one float other int
  12. //cout << "Pushing elements onto fs" << endl ;
  13. //cout << endl << "Stack Full." << endl<< endl << "Popping elements from fs" << endl ;
  14. //cout << endl << "Stack Empty" << endl ;
  15.  
  16. FloatStack fs(5) ;
  17. float f = 1.1 ;
  18. cout << "Pushing elements onto fs" << endl ;
  19. while (fs.push(f))
  20. { cout << f << ' ' ;
  21. f += 1.1 ;
  22. }
  23. cout << endl << "Stack Full." << endl
  24. << endl << "Popping elements from fs" << endl ;
  25. while (fs.pop(f))
  26. cout << f << ' ' ;
  27. cout << endl << "Stack Empty" << endl << endl ;
  28.  
  29. //cout << "Pushing elements onto is" << endl ;
  30. // write ur code here
  31. //cout << endl << "Stack Full" << endl<< endl << "Popping elements from is" << endl ;
  32. //cout << endl << "Stack Empty" << endl ;
  33.  
  34. IntStack is ;
  35. int i = 1.1 ;
  36. cout << "Pushing elements onto is" << endl ;
  37. while (is.push(i))
  38. { cout << i << ' ' ;
  39. i += 1 ;
  40. }
  41. cout << endl << "Stack Full" << endl
  42. << endl << "Popping elements from is" << endl ;
  43. while (is.pop(i)) cout << i << ' ' ;
  44. cout << endl << "Stack Empty" << endl ;
  45.  
  46. system("PAUSE");
  47. return 0;
  48. }
  49. /* output
  50. Pushing elements onto fs
  51. 1.1 2.2 3.3 4.4 5.5
  52. Stack Full.
  53.  
  54. Popping elements from fs
  55. 5.5 4.4 3.3 2.2 1.1
  56. Stack Empty
  57.  
  58. Pushing elements onto is
  59. 1 2 3 4 5 6 7 8 9 10
  60. Stack Full
  61.  
  62. Popping elements from is
  63. 10 9 8 7 6 5 4 3 2 1
  64. Stack Empty
  65. */
<< moderator edit: added [code][/code] tags >>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iqbal is offline Offline
10 posts
since Aug 2005
Nov 28th, 2005
0

Re: please check my code and help me

It compiled fine in VC6.0...yes it did gave 2 warnings regarding truncation from const double to float....i made two changes two remove those warnings
float f = 1.1f ;
	cout << "Pushing elements onto fs" << endl ;
	while (fs.push(f))
	{	cout << f << ' ' ;
		f += 1.1f ;
	}
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005

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: Linked List & Objects
Next Thread in C++ Forum Timeline: Scope Resolution Operator





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


Follow us on Twitter


© 2011 DaniWeb® LLC