Receiving wrong output results

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

Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Receiving wrong output results

 
0
  #1
23 Days Ago
I am trying to determine if a word is a palindrome or not using queues, but I am receiving the wrong results. I have tried several things to get it to work using the boolean variable isPalin. Will someone lead me to the right direction please? I have been trying for hours, and I feel that it is something small that is holding me back.


Current Output:

mom is a Palindrome
cat is a Palindrome
noon is a Palindrome
mouse is a Palindrome


Expected Output


mom is a Palindrome
cat is NOT a Palindrome
noon is a Palindrome
mouse is NOT a Palindrome




main.cpp


  1. #include <fstream>
  2. #include <string>
  3. #include <sstream>
  4. #include <stdlib.h>
  5. #include "queue.h"
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.  
  12. stringstream ss;
  13. string line; // name of my identifier used to read in data
  14. string word; // individual characters
  15. queueType<string> u; // declares queue u
  16. queueType<string> r; // declare queue r
  17. queueType<string> s; // declare queue s
  18.  
  19.  
  20. ifstream inData("input.txt"); // declaration of inData
  21. ofstream outData("output.txt"); // declaration of outData
  22.  
  23.  
  24. while ( getline( inData, line ) ) // reads words in file using getline to capture all spaces
  25. { /*something to break up line into individual character*/
  26.  
  27. ss << line;
  28.  
  29. while ( ss >> word )/*there are still characters*/
  30. {
  31. u.addQueue(word);
  32. s.addQueue(word); /*adds characters of word into queues u and s*/
  33. }
  34.  
  35. while (!u.isEmptyQueue()) /*u is not empty*/
  36. {
  37. r.addQueue(u.front());
  38. u.deleteQueue();
  39. /*adds to the front of u into r and then delete from u*/
  40. }
  41.  
  42. bool isPalin;
  43. isPalin = true;
  44.  
  45. while (!s.isEmptyQueue())/*s is not empty*/
  46. {
  47. s.deleteQueue();
  48. u.addQueue(word);
  49.  
  50. if(!(u.front() == r.front())) /*compares front elements, does something if the two not equal, delete elements*/
  51. {
  52. isPalin = false;
  53. u.deleteQueue();
  54. r.deleteQueue();
  55. break;
  56. }
  57. else
  58. {
  59. u.deleteQueue();
  60. r.deleteQueue();
  61. }
  62.  
  63. }
  64.  
  65. if (isPalin == true)
  66. {
  67. outData<<word<<" is a Palindrome"<<endl;
  68. }
  69. if (isPalin == false)
  70. outData<<word<<" is NOT a Palindrome"<<endl;
  71.  
  72. ss.clear();
  73.  
  74. }
  75. inData.close(); // closes inData
  76. outData.close(); // closes outData
  77.  
  78. system("PAUSE");
  79. return 0;
  80. }



queue.h


  1. #ifndef H_queueType
  2. #define H_queueType
  3.  
  4. #include <iostream>
  5. #include <cassert>
  6.  
  7. using namespace std;
  8.  
  9. template<class Type>
  10. class queueType // public queueADT<Type>
  11. {
  12. public:
  13. const queueType<Type>& operator=(const queueType<Type>&);
  14. bool isEmptyQueue() const;
  15. bool isFullQueue() const;
  16. void initializeQueue();
  17. Type front() const;
  18. Type back() const;
  19. void addQueue(const Type& queueElement);
  20. void deleteQueue();
  21. queueType(int queueSize = 100);
  22. queueType(const queueType<Type>& otherQueue);
  23. ~queueType();
  24. void printQueue();
  25. bool operator== (const queueType<Type>&);
  26. bool operator!= (const queueType<Type>&);
  27.  
  28. private:
  29. int maxQueueSize;
  30. int count;
  31. int queueFront;
  32. int queueRear;
  33. Type *list;
  34. bool isEqual(const queueType<Type>&);
  35. };
  36.  
  37. template<class Type>
  38. bool queueType<Type>::isEmptyQueue() const
  39. {
  40. return (count == 0);
  41. }
  42.  
  43. template<class Type>
  44. bool queueType<Type>::isFullQueue() const
  45. {
  46. return (count == maxQueueSize);
  47. }
  48.  
  49. template<class Type>
  50. void queueType<Type>::initializeQueue()
  51. {
  52. queueFront = 0;
  53. queueRear = maxQueueSize - 1;
  54. count = 0;
  55. }
  56.  
  57. template<class Type>
  58. Type queueType<Type>::front() const
  59. {
  60. assert(!isEmptyQueue());
  61. return list[queueFront];
  62. }
  63.  
  64. template<class Type>
  65. Type queueType<Type>::back() const
  66. {
  67. assert(!isEmptyQueue());
  68. return list[queueRear];
  69. }
  70.  
  71. template<class Type>
  72. void queueType<Type>::addQueue(const Type& newElement)
  73. {
  74. if (!isFullQueue())
  75. {
  76. queueRear = (queueRear + 1) % maxQueueSize;
  77.  
  78. count++;
  79. list[queueRear] = newElement;
  80. }
  81.  
  82. else
  83. cout<< "Cannot add to a full queue."<<endl;
  84. }
  85.  
  86. template<class Type>
  87. void queueType<Type>::deleteQueue()
  88. {
  89. if (!isEmptyQueue())
  90. {
  91. count--;
  92. queueFront = (queueFront + 1) % maxQueueSize;
  93. }
  94.  
  95. else
  96. cout <<"Cannot remove from an empty queue"<<endl;
  97.  
  98. }
  99.  
  100. template<class Type>
  101. queueType<Type>::queueType(int queueSize)
  102. {
  103. if (queueSize <= 0)
  104. {
  105. cout<<"Size of the array to hold the queue must "
  106. <<"be positive."<<endl;
  107. cout<<"Creating an array of size 100."<<endl;
  108.  
  109. maxQueueSize = 100;
  110. }
  111.  
  112. else
  113.  
  114. maxQueueSize = queueSize;
  115.  
  116. queueFront = 0;
  117. queueRear = maxQueueSize - 1;
  118. count = 0;
  119. list = new Type[maxQueueSize];
  120. }
  121.  
  122. template<class Type>
  123. queueType<Type>::~queueType()
  124. {
  125. delete [] list;
  126. }
  127.  
  128.  
  129. template<class Type>
  130. bool queueType<Type>::isEqual(const queueType<Type>& otherQueue)
  131. {
  132. bool bRet = false;
  133.  
  134. if (otherQueue.maxQueueSize == maxQueueSize && otherQueue.queueFront == queueFront)
  135. {
  136. bRet = true;
  137. for (int j = 0; j < queueFront; ++j)
  138. {
  139. if (otherQueue.list[j] != list[j])
  140. {
  141. // cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j];
  142. bRet = false;
  143. break;
  144. }
  145. }
  146. }
  147. return bRet;
  148. }
  149.  
  150. template<class Type>
  151. bool queueType<Type>::operator==(const queueType<Type>& otherQueue)
  152. {
  153. return isEqual(otherQueue);
  154. }
  155.  
  156. template<class Type>
  157. bool queueType<Type>::operator!=(const queueType<Type>& otherQueue)
  158. {
  159. return !isEqual(otherQueue);
  160. }
  161.  
  162.  
  163. template<class Type>
  164. void queueType<Type>::printQueue()
  165. {
  166.  
  167. for (int x = queueFront-1; x >= 0; --x)
  168. {
  169. cout << list[x] << '\n';
  170. }
  171.  
  172.  
  173. }
  174.  
  175.  
  176.  
  177. #endif
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 327
Reputation: JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice 
Solved Threads: 59
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz
 
0
  #2
22 Days Ago
It seems to me that you are over complicating things. Wouldn't it be a lot simpler to just use a double ended queue, otherwise known as a deque?
Modifying your code from one of your previous posts in another thread:
  1. //Implement the palindrome-recognition algorithm described in "Simple Applications of the ADT Queue"
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <deque>
  6.  
  7. using namespace std;
  8.  
  9. void isPal(string);
  10.  
  11. void isPal(string s)
  12. {
  13. deque<char> aDeque;
  14.  
  15. for(unsigned i = 0; i < s.length(); i++)
  16. {
  17. aDeque.push_back(s[i]);
  18. }
  19.  
  20. bool charEqual = true;
  21.  
  22. while (!aDeque.empty() && charEqual==true)
  23. {
  24. if(aDeque.front() == aDeque.back())
  25. {
  26. aDeque.pop_front();
  27. // as long as the pop_front() didn't empty the deque..
  28. if(!aDeque.empty())
  29. aDeque.pop_back(); // do a pop_back()
  30. }
  31. else
  32. charEqual=false;
  33. }
  34.  
  35. cout << word;
  36. if(charEqual == true)
  37. cout << " is a Palindrome" << endl;
  38. else
  39. cout << " is NOT a Palindrome" << endl;
  40. }
  41.  
  42. int main ()
  43. {
  44. string input;
  45. cout << "Enter the string: ";
  46. cin >> input;
  47. isPal(input);
  48.  
  49. return 0;
  50. }

Isn't that a lot cleaner and quicker than using two queues? it certainly saves you from creating that unneccessary template nonsense in queue.h!

EDIT:
Now modifying the above code to read words/lines from a file and then pass each word/line to the isPal() function you get this:
  1. //Implement the palindrome-recognition algotithm described in "Simple Applications of the ADT Queue"
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <string>
  7. #include <deque>
  8.  
  9. using namespace std;
  10.  
  11. bool isPal(string);
  12.  
  13. bool isPal(string s)
  14. {
  15. deque<char> aDeque;
  16.  
  17. for(unsigned i = 0; i < s.length(); i++)
  18. {
  19. aDeque.push_back(s[i]);
  20. }
  21.  
  22. while (!aDeque.empty())
  23. {
  24. if(aDeque.front() == aDeque.back())
  25. {
  26. aDeque.pop_front();
  27. // as long as the pop_front didn't empty the deque..
  28. if(!aDeque.empty())
  29. aDeque.pop_back(); // do a pop_back()
  30. }
  31. else
  32. return false;
  33. }
  34. return true;
  35. }
  36.  
  37. int main ()
  38. {
  39. ifstream inData("input.txt");
  40. ofstream outData("output.txt");
  41.  
  42. stringstream ss;
  43. string line, word;
  44.  
  45. while(getline(inData, line))
  46. {
  47. ss << line;
  48.  
  49. while(ss>>word)
  50. {
  51. if(isPal(word))
  52. outData << word << " is a palindrome" << endl;
  53. else
  54. outData << word << " is NOT a palindrome" << endl;
  55. }
  56. word.clear();
  57. ss.clear();
  58. }
  59. inData.close();
  60. outData.close();
  61.  
  62. return 0;
  63. }

Cheers for now,
Jas.
Last edited by JasonHippy; 22 Days Ago at 12:31 pm.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz
 
0
  #3
22 Days Ago
Jason, thank you very much sir.

I have a question...If I decide to use my queue.h, will I be able to accomplish the same thing? or is it no way that I can determine a palindrome with what I have?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz
 
0
  #4
22 Days Ago
I tried to use the format above using my queue class to see if it would work, but it gave the output results....

mom is a palindrome
cat is NOT a palindrome
noon is NOT a palindrome
mouse is NOT a palindrome


is there a reason why it didn't work with my queue class? Thanks



  1. #include <fstream>
  2. #include <sstream>
  3. #include <string>
  4. //#include <deque>
  5. #include "queue.h"
  6.  
  7.  
  8. using namespace std;
  9.  
  10.  
  11. bool isPal(string);
  12.  
  13.  
  14. bool isPal(string s)
  15. {
  16. queueType<char> aDeque;
  17.  
  18.  
  19. for(unsigned i = 0; i < s.length(); i++)
  20. {
  21. aDeque.addQueue(s[i]);
  22. }
  23.  
  24.  
  25. while (!aDeque.isEmptyQueue())
  26. {
  27. if(aDeque.front() == aDeque.back())
  28. {
  29. aDeque.deleteQueue();
  30. // as long as the pop_front didn't empty the deque..
  31. if(!aDeque.isEmptyQueue())
  32. aDeque.deleteQueue(); // do a pop_back()
  33. }
  34. else
  35. return false;
  36. }
  37. return true;
  38. }
  39. int main ()
  40. {
  41. ifstream inData("input.txt");
  42. ofstream outData("output.txt");
  43. stringstream ss;
  44. string line, word;
  45. while(getline(inData, line))
  46. {
  47. ss << line;
  48. while(ss>>word)
  49. {
  50. if(isPal(word))
  51. outData << word << " is a palindrome" << endl;
  52. else
  53. outData << word << " is NOT a palindrome" << endl;
  54. }
  55. word.clear();
  56. ss.clear();
  57. }
  58. inData.close();
  59. outData.close();
  60. return 0;
  61. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz
 
0
  #5
22 Days Ago
Is there a way I can use a pop back like you did with aDeque.pop_back(); that I can use in my last post using my queue.h


  1. if(aDeque.front() == aDeque.back())
  2. {
  3. aDeque.pop_front();
  4. if(!aDeque.empty())
  5. aDeque.pop_back(); // do a pop_back()
  6. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 327
Reputation: JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice 
Solved Threads: 59
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz
 
0
  #6
22 Days Ago
Oh I see you've HAD to implement your own queue class as a part of your assignment...sorry I thought you were just over complicating things for yourself.

In that case, to turn your class into a double ended queue I guess you'd need to implement a function to allow the removal of items from the back of the queue, something like this perhaps?
  1. template<class Type>
  2. void queueType<Type>::deleteBackOfQueue()
  3. {
  4. if (!isEmptyQueue())
  5. {
  6. count--;
  7. queueRear = (queueRear - 1) % maxQueueSize;
  8. }
  9.  
  10. else
  11. cout <<"Cannot remove from an empty queue"<<endl;
  12.  
  13. }

Then in the if statement:
  1. if(aDeque.front() == aDeque.back())
  2. {
  3. aDeque.deleteQueue();
  4. if(!aDeque.empty())
  5. aDeque.deleteBackOfQueue(); // pop the back()
  6. }

I've not compiled or tested the above code snippets, but I'd imagine that you would need to be doing something similar to the above!

Cheers for now,
Jas.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz
 
0
  #7
22 Days Ago
Thank you a lot. It worked. I am VERY happy...I can't express how much I am grateful. Mission completed!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 327
Reputation: JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice 
Solved Threads: 59
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz
 
0
  #8
22 Days Ago
Glad to have helped!
Last edited by JasonHippy; 22 Days Ago at 2:32 pm.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
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