944,175 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 10949
  • C++ RSS
Sep 13th, 2005
0

stack palindrome problem?

Expand Post »
I am receiving error messages for this palindrome problem. I am reading in a file and outputting the results if it is a palindrome or not using stacks. Any ideas as to why I am receiving error messages below.

C++ Syntax (Toggle Plain Text)
  1.  
  2. include <iostream>
  3. #include <string>
  4. #include <cmath>
  5. #include <iomanip>
  6. #include <fstream>
  7. #include <cmath>
  8. #include <vector>
  9. #include <cstdlib>
  10. //____________________________________________________________________________________
  11.  
  12. using namespace std;
  13. //____________________________________________________________________________________
  14.  
  15. class CommandLineException
  16. {
  17. public:
  18. CommandLineException(int max, int actual)
  19. {
  20. cout << " Too many command line arguments. " << endl;
  21. }
  22. };
  23. //____________________________________________________________________________________
  24.  
  25.  
  26. class FileException
  27. {
  28. public:
  29. FileException(char* fn)
  30. {
  31. cout << " File " << fn << " could not be opened. " << endl;
  32. }
  33. };
  34. //____________________________________________________________________________________
  35.  
  36. class Stack_Empty_Exception
  37. {
  38. public:
  39. Stack_Empty_Exception(char* a)
  40. {
  41. cout << " I am . " << a <<" empty" << endl;
  42. }
  43. };
  44.  
  45. //____________________________________________________________________________________
  46.  
  47. class Stack_Full_Exception
  48. {
  49. public:
  50. Stack_Full_Exception(char* fn)
  51. {
  52. cout << " I am " << fn << " full. " << endl;
  53. }
  54. };
  55. //____________________________________________________________________________________
  56.  
  57. class Stack
  58. {
  59. public:
  60. Stack(int);
  61. ~Stack();
  62. bool isFull(void); // check less than size
  63. bool isEmpty(void);
  64. void Push(char v);
  65. char Pop(void);
  66.  
  67.  
  68. private:
  69.  
  70. int size;
  71. int top_of_stack;
  72. char* s;
  73.  
  74. };
  75. //____________________________________________________________________________________
  76.  
  77. Stack::Stack()
  78. {
  79. int sz = 100;
  80. size = sz;
  81. top_of_stack = -1;
  82. s = new char[size];
  83. }
  84. //____________________________________________________________________________________
  85.  
  86. Stack::~Stack()
  87. {
  88. if(s)
  89. {
  90. delete[]s;
  91. }
  92. }
  93.  
  94. //____________________________________________________________________________________
  95.  
  96. bool Stack::isFull(void)
  97. {
  98. return top_of_stack >= size - 1;
  99. }
  100.  
  101. //____________________________________________________________________________________
  102. bool Stack::isEmpty(void)
  103. {
  104. return top_of_stack < 0;
  105. }
  106. //____________________________________________________________________________________
  107. void Stack::Push( char v)
  108. {
  109. if (isFull())
  110. throw Stack_Full_Exception();
  111. s[++top_of_stack] = v;
  112. }
  113. //____________________________________________________________________________________
  114. char Stack::Pop(void)
  115. {
  116. if (isEmpty())
  117. throw Stack_Empty_Exception();
  118.  
  119. return s[top_of_stack--];
  120. }
  121. //____________________________________________________________________________________
  122.  
  123. int main(int argc, char * argv[])
  124. {
  125.  
  126. try
  127. {
  128. char infile[100]; // input file
  129. char outfile[100]; // output file
  130.  
  131.  
  132. if (argc == 1)
  133. {
  134. cout << " Enter the input file name. " << endl;
  135. cin >> infile;
  136. cout << " Enter the output file name. " << endl;
  137. cin >> outfile;
  138. cout << endl;
  139. }
  140.  
  141.  
  142. else if (argc == 2)
  143. {
  144. strcpy(infile, argv[1]);
  145. cout << " Enter the output file name. " << endl;
  146. cin >> outfile;
  147. cout << endl;
  148.  
  149. }
  150. else if ( argc == 3)
  151. {
  152. strcpy(infile, argv[1]);
  153. strcpy(outfile, argv[2]);
  154. }
  155. else
  156. {
  157. throw CommandLineException(2,argc -1);
  158. }
  159.  
  160. ifstream i(infile);
  161. if(!i)
  162. throw FileException(infile);
  163.  
  164. ofstream o(outfile);
  165. if(!o)
  166. throw FileException(outfile);
  167.  
  168. o.close();
  169. i.close();
  170. } catch(...)
  171. {
  172. cout << " Program Terminated. " << endl;
  173. exit(EXIT_FAILURE);
  174. }
  175.  
  176.  
  177. void is_Palindrome(isstream& i, ostream& o)
  178. {
  179. for(;;)
  180. {
  181. string word;
  182. i >> word;
  183. if ( i.eof())
  184. break;
  185. o << endl;
  186. o << word << " is a palindrome. " << endl;
  187. } // end of for loop
  188. } // end of is_Palindrome function.
  189.  
  190. bool check_Palindrome( string& word)
  191. {
  192. Stack reverse;
  193.  
  194. for ( int i = 0; i < word.length(); i++)
  195. {
  196. reverse.push(word[i]);
  197. }
  198. for ( int j = 0; j < word.length(); j++)
  199. {
  200. if (word[j] != reverse.pop())
  201.  
  202. return false;
  203. else
  204. return true;
  205. }
  206.  
  207.  
  208. } // end of check_palindrome
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215. return 0;
  216. }

error messages:
C++ Syntax (Toggle Plain Text)
  1.  
  2. mpiling...
  3. p02.cpp
  4. D:\MSDev98\CS2613\p02\p02.cpp(79) : error C2511: 'Stack::Stack' : overloaded member function 'void (void)' not found in 'Stack'
  5. D:\MSDev98\CS2613\p02\p02.cpp(59) : see declaration of 'Stack'
  6. D:\MSDev98\CS2613\p02\p02.cpp(111) : error C2512: 'Stack_Full_Exception::Stack_Full_Exception' : no appropriate default constructor available
  7. D:\MSDev98\CS2613\p02\p02.cpp(118) : error C2512: 'Stack_Empty_Exception::Stack_Empty_Exception' : no appropriate default constructor available
  8. D:\MSDev98\CS2613\p02\p02.cpp(178) : error C2065: 'isstream' : undeclared identifier
  9. D:\MSDev98\CS2613\p02\p02.cpp(178) : error C2065: 'i' : undeclared identifier
  10. D:\MSDev98\CS2613\p02\p02.cpp(178) : error C2065: 'o' : undeclared identifier
  11. D:\MSDev98\CS2613\p02\p02.cpp(178) : error C2275: 'ostream' : illegal use of this type as an expression
  12. d:\vc98\include\iosfwd(257) : see declaration of 'ostream'
  13. D:\MSDev98\CS2613\p02\p02.cpp(179) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
  14. D:\MSDev98\CS2613\p02\p02.cpp(183) : error C2065: 'word' : undeclared identifier
  15. D:\MSDev98\CS2613\p02\p02.cpp(183) : warning C4552: '>>' : operator has no effect; expected operator with side-effect
  16. D:\MSDev98\CS2613\p02\p02.cpp(184) : error C2228: left of '.eof' must have class/struct/union type
  17. D:\MSDev98\CS2613\p02\p02.cpp(186) : error C2563: mismatch in formal parameter list
  18. D:\MSDev98\CS2613\p02\p02.cpp(186) : error C2568: '<<' : unable to resolve function overload
  19. could be 'class std::basic_ostream<unsigned short,struct std::char_traits<unsigned short> > &__cdecl std::endl(class std::basic_ostream<unsigned short,struct std::char_traits<unsigned short> > &)'
  20. d:\vc98\include\ostream(377) : see declaration of 'endl'
  21. or 'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl std::endl(class std::basic_ostream<char,struct std::char_traits<char> > &)'
  22. d:\vc98\include\ostream(372) : see declaration of 'endl'
  23. or 'class std::basic_ostream<_E,_Tr> &__cdecl std::endl(class std::basic_ostream<_E,_Tr> &)'
  24. d:\vc98\include\ostream(367) : see declaration of 'endl'
  25. D:\MSDev98\CS2613\p02\p02.cpp(187) : error C2297: '<<' : illegal, right operand has type 'char [19]'
  26. D:\MSDev98\CS2613\p02\p02.cpp(192) : error C2601: 'check_Palindrome' : local function definitions are illegal
  27. Error executing cl.exe.
  28.  
  29. p02.obj - 14 error(s), 1 warning(s)
Similar Threads
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
djbsabkcb is offline Offline
92 posts
since Jun 2005
Sep 13th, 2005
0

Re: stack palindrome problem?

why stacks?
Heres a palindrome proggy i did for another board.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4.  
  5. int IsMirrorPalindrome( const char* word, size_t length )
  6. {
  7. if( length <= 1) return 1;
  8. else
  9. return ( (word[0] == word[length-1]) && IsMirrorPalindrome(word+1,length-2));
  10. }
  11.  
  12. int IsPalindrome(std::string test)
  13. {
  14. std::string tempcopy;
  15. std::string::iterator iter = test.begin();
  16. const std::string::iterator end = test.end();
  17. while ( iter != end )
  18. {
  19. const char c = *iter;
  20. if ( (!std::ispunct(c)) && (!std::isspace(c)) ) tempcopy.push_back(std::tolower(c));
  21. ++iter;
  22. }
  23.  
  24. if (IsMirrorPalindrome(tempcopy.c_str(),tempcopy.length()))
  25. return 1;
  26. else
  27. return 0;
  28. }
  29.  
  30.  
  31. int main()
  32. {
  33. std::string test;
  34. std::cout<<" enter string.... ";
  35. std::getline(std::cin,test);
  36. if (IsPalindrome(test))
  37. std::cout<<std::endl<<"palindrome"<<std::endl;
  38. else
  39. std::cout<<std::endl<<" not palindrome"<<std::endl;
  40. getchar();
  41. }
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Sep 13th, 2005
0

Re: stack palindrome problem?

ok lets go thru your code....

your exception classes will need default constructors or arguments passed to them.

destructor doesnt need if(s). just the delete[]s will do nicely.

throw Stack_Full_Exception();
should be
throw Stack_Full_Exception;
or
throw Stack_Full_Exception(args);
depending on available constructors. see point 1.


You cant nest other functions inside main!!!!
Thats illegal.
Your funcs to check if a palindrome is never called from main.
Fix those and repost if that dont do it.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Sep 14th, 2005
0

Re: stack palindrome problem?

I have to use a stack, will make those corrections and let you know what happens.

thanks
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
djbsabkcb is offline Offline
92 posts
since Jun 2005
Sep 14th, 2005
0

Re: stack palindrome problem?

Okay, the problem I am having now is that once compiled and I will input the I/O file names. It will state that the file cannot be opened. Why?




C++ Syntax (Toggle Plain Text)
  1.  
  2. //____________________________________________________________________________________________________________________
  3.  
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <cmath>
  8. #include <iomanip>
  9. #include <fstream>
  10. #include <cmath>
  11. #include <vector>
  12. #include <cstdlib>
  13. //____________________________________________________________________________________
  14.  
  15. using namespace std;
  16. //____________________________________________________________________________________
  17.  
  18. class CommandLineException
  19. {
  20. public:
  21.  
  22. CommandLineException(int max, int actual)
  23. {
  24. cout << " Too many command line arguments. " << endl;
  25. }
  26. };
  27.  
  28. //____________________________________________________________________________________
  29.  
  30.  
  31. class FileException
  32. {
  33. public:
  34.  
  35. FileException(char* fn)
  36. {
  37. cout << " File " << fn << " could not be opened. " << endl;
  38. }
  39. };
  40.  
  41. //____________________________________________________________________________________
  42.  
  43. class Stack
  44. {
  45. public:
  46. Stack();
  47. ~Stack();
  48. bool isFull(void);
  49. bool isEmpty(void);
  50. void Push(char v);
  51. char Pop(void);
  52. class Stack_Empty_Exception{public:Stack_Empty_Exception();};
  53. class Stack_Full_Exception{public:Stack_Full_Exception();};
  54.  
  55. private:
  56.  
  57. int size;
  58. int top_of_stack;
  59. char* s;
  60.  
  61. };
  62. //____________________________________________________________________________________
  63. Stack::Stack_Empty_Exception::Stack_Empty_Exception()
  64. {
  65.  
  66. cout << " The stack is empty. " << endl;
  67. }
  68. //____________________________________________________________________________________
  69. Stack::Stack_Full_Exception::Stack_Full_Exception()
  70. {
  71.  
  72. cout << " The stack is full. " << endl;
  73. }
  74. //____________________________________________________________________________________
  75. Stack::Stack()
  76. {
  77. int sz = 100;
  78. size = sz;
  79. top_of_stack = -1;
  80. s = new char[size];
  81. }
  82. //____________________________________________________________________________________
  83.  
  84. Stack::~Stack()
  85. {
  86. if(s)
  87.  
  88. delete[]s;
  89.  
  90. }
  91.  
  92. //____________________________________________________________________________________
  93.  
  94. bool Stack::isFull(void)
  95. {
  96. return top_of_stack >= size - 1;
  97. }
  98.  
  99. //____________________________________________________________________________________
  100. bool Stack::isEmpty(void)
  101. {
  102. return top_of_stack < 0;
  103. }
  104. //____________________________________________________________________________________
  105. void Stack::Push( char v)
  106. {
  107. if (isFull())
  108. throw Stack_Full_Exception();
  109. s[++top_of_stack] = v;
  110. }
  111. //____________________________________________________________________________________
  112. char Stack::Pop(void)
  113. {
  114. if (isEmpty())
  115. throw Stack_Empty_Exception();
  116.  
  117. return s[top_of_stack--];
  118. }
  119. //____________________________________________________________________________________
  120.  
  121. bool is_Palindrome(ifstream& i, ofstream& o);
  122. void Palindrome_mgr(string str1, ofstream& o);
  123. //____________________________________________________________________________________
  124.  
  125. int main(int argc, char* argv[])
  126. {
  127.  
  128. try
  129. {
  130. char infile[255]; // input file
  131. char outfile[255]; // output file
  132.  
  133.  
  134. if (argc == 1)
  135. {
  136. cout << " Enter the input file name. " << endl;
  137. cin >> infile;
  138. cout << " Enter the output file name. " << endl;
  139. cin >> outfile;
  140. cout << endl;
  141. }
  142.  
  143.  
  144. else if (argc == 2)
  145. {
  146. strcpy(infile, argv[1]);
  147. cout << " Enter the output file name. " << endl;
  148. cin >> outfile;
  149. cout << endl;
  150.  
  151. }
  152. else if ( argc == 3)
  153. {
  154. strcpy(infile, argv[1]);
  155. strcpy(outfile, argv[2]);
  156. }
  157. else
  158. {
  159. throw CommandLineException(2,argc -1);
  160. }
  161.  
  162. ifstream i(infile);
  163. if(!i)
  164. throw FileException(infile);
  165.  
  166. ofstream o(outfile);
  167. if(!o)
  168. throw FileException(outfile);
  169.  
  170. string word;
  171. for(;;)
  172. {
  173. if(i.eof())
  174. break;
  175.  
  176. i >> word;
  177.  
  178.  
  179. }
  180. Palindrome_mgr(word, o);
  181.  
  182.  
  183.  
  184.  
  185. o.close();
  186. i.close();
  187. } catch(...)
  188. {
  189. cout << " Program Terminated. " << endl;
  190. exit(EXIT_FAILURE);
  191. }
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200. return 0;
  201. }
  202.  
  203.  
  204.  
  205. bool is_Palindrome(string check1, string check2 )
  206. {
  207. if (check1 == check2)
  208. return true;
  209. else
  210. return false;
  211.  
  212. } // end of is_Palindrome function.
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219. void Palindrome_mgr(string str1, ofstream& o)
  220. {
  221. string str2;
  222. bool swapped;
  223. Stack s;
  224. for ( int i = 0; i < str1.length(); i++)
  225. {
  226. s.Push(str1[i]);
  227. str2 += s.Pop();
  228.  
  229.  
  230. }
  231. swapped = is_Palindrome(str1,str2);
  232.  
  233. if (swapped)
  234. cout << str1 << " is a palindrome." << endl;
  235. else
  236. cout<< str1 << " is not a palindrome. " << endl;
  237.  
  238. } // end of Palindrome_mgr.
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
djbsabkcb is offline Offline
92 posts
since Jun 2005
Sep 14th, 2005
0

Re: stack palindrome problem?

You have included cmath twice, but that's not likely to cause the problem you describe. When programs compile, but then don't run appropriately, it is harder to debug them. In this case the syntax of obtaining file names and associating them with files, etc. seem appropriate. Therefore, I would try printing the file names to the screen using cout statements to be sure they have been input appropriately. IF that doesn't work, then posting the exact output that is unexpected and/or error statements you encounter would be helpful. If that still doesn't work I'd start commenting out stuff until I got back to a bare bones program without all the fluff of try/catch etc. to confound the issue and proceed until I got the program running. Then I'd add back sections, one at time and testing each one as they are reintroduced until I tracked the problem down to a (given set of) line(s).
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Sep 14th, 2005
0

Re: stack palindrome problem?

okay i figured it out. the file names were different. However, when it reads in the file it only reads in the last word and checks for palindrome.
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
djbsabkcb is offline Offline
92 posts
since Jun 2005
Sep 14th, 2005
0

Re: stack palindrome problem?

C++ Syntax (Toggle Plain Text)
  1. string word; //declare a single string
  2. for(;;)
  3. {
  4. if(i.eof()) //if find EOF
  5. break; //stop
  6.  
  7. i >> word; ///else read in each word, but don't do anything with it.
  8. }
  9. Palindrome_mgr(word, o); //send last word read in to this function.

Do you see any problem with logic in the comments I wrote?
You will do yourself a big favor by testing code as you write it, one
step at a time, and not as one big bolus.

BTW: You shouldn't be testing for eof() to break out of the loop as
it will likely cause an overread by one. But that would be then next
problem you encounter if you fix the more obvious logic problem.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Sep 14th, 2005
0

Re: stack palindrome problem?

I got it figured out. Good looking out everyone.

thanks
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
djbsabkcb is offline Offline
92 posts
since Jun 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: Project Ideas?
Next Thread in C++ Forum Timeline: Array problems





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


Follow us on Twitter


© 2011 DaniWeb® LLC