please check my code and help me

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 10
Reputation: iqbal is an unknown quantity at this point 
Solved Threads: 0
iqbal iqbal is offline Offline
Newbie Poster

please check my code and help me

 
0
  #1
Nov 28th, 2005
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

  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:

  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 >>
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: please check my code and help me

 
0
  #2
Nov 28th, 2005
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 ;
	}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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