943,473 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8415
  • C++ RSS
Jul 14th, 2004
0

Stack Queue Fstream

Expand Post »
Hi everyone, I've recently been having a problem with my current program. I've gotten it to work so that I can enter a word in and it will check the word to see whether or not it is a palindrome by methods of stack/queue.
eg. dad
"Dad is a palindrome"
Now I am trying to make is so that the program reads from a text file and outputs the results to another. I've read a little about fstream and understand somewhat how it works but am still having problems. I stopped modifying my program after the code below:

C++ Syntax (Toggle Plain Text)
  1. ifstream inside;
  2. ofstream outside;
  3. inside.open("palindromeinput.txt",ios::in);
  4. outside.open("palindromeoutput.txt",ios::out);

because this is where I became lost. Please let me know if you have any suggestions. I've posted the entire code below. It produces no errors but is still not reading any information placed in the text file (dad, pen)

Thanks an advance


C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include <cstdlib> // For EXIT_FAILURE and EXIT_SUCCESS
  3. #include <fstream>
  4. #include<string>
  5. using namespace std;
  6. const int MAX=50; // initialize max string size of 50 characters
  7. typedef char StackElement; // define StackElement
  8. typedef char QueueElement; // define QueueElement
  9. class Stack
  10. {
  11. public:
  12. Stack(){top=-1;arr[MAX]=0;} // default stack constructor
  13. void push(StackElement & ch); // push function
  14. StackElement topNpop(); // top and pop functions combined
  15. bool empty() const; // empty function
  16. private:
  17. StackElement arr[MAX]; // define char array
  18. int top; // define int top
  19.  
  20. };
  21. /*******************************************
  22. FUNCTION: push()
  23. DESCRIPTION: Pushes an element onto the stack
  24. PRECONDITION: Waiting for function call
  25. POSTCONTION: New element character on top of stack
  26. *******************************************/
  27. inline void Stack::push(StackElement & ch)
  28. {
  29. if(top<MAX)
  30. {
  31. top++; // increment top
  32. arr[top]=ch; // push onto stack
  33. }
  34. else
  35. {
  36. cout<<"Stack is full.\n"; // display stack is full
  37. }
  38.  
  39.  
  40. }
  41. /*******************************************
  42. FUNCTION: topNpop()
  43. DESCRIPTION: Reads and pops top element off the stack
  44. PRECONDIION: Waiting for function call
  45. POSTCONDITION: One element read and removed fromt he stack
  46. RETURN: Top element from stack
  47. ********************************************/
  48.  
  49. inline StackElement Stack::topNpop()
  50. {
  51. if(top>-1)
  52. {
  53. return(arr[top]); // returns top element
  54. top--; // remove froms stack
  55. }
  56. else
  57. {
  58. cout<<"Stack is empty.\n"; // display stack is empty
  59. return(0);
  60.  
  61. }
  62.  
  63. }
  64. /*******************************************
  65. FUNCTION: empty()
  66. DESCRIPTION: returns result value if stack is empty
  67. PRECONDITION: result=false
  68. POSTCONDITION: result may be true or remain false
  69. RETURN: result if true or false
  70. ********************************************/
  71. inline bool Stack::empty() const
  72. {
  73. bool result=false; // initialize bool as false
  74. if (top==-1)
  75. {
  76. result=true; // if top is -1 return result true
  77. return(result);
  78. }
  79. else
  80. {
  81. return(result); // else return false
  82. }
  83. }
  84. class Queue // Queue class
  85. {
  86. public:
  87. Queue(){front=0, back=0;arr[MAX]=0;} // Queue default constructor
  88. void addq(QueueElement & ch); // define addq
  89. QueueElement frontNremoveq(); // define frontNremove
  90. private:
  91. QueueElement arr[MAX]; // initialize QueueElement array
  92. int front, back; // initialize int front and back
  93.  
  94. };
  95. /*******************************************
  96. FUNCTION: addq()
  97. DESCRIPTION: adds an element onto the queue
  98. PRECONDITION: Waiting for element to add
  99. POSTCONDITION: New element now on the queue
  100. ********************************************/
  101. inline void Queue::addq(QueueElement &ch)
  102. {
  103. if(front!=(back+1)%MAX)
  104. {
  105. arr[back]=ch; // add element to back of queue
  106. back=(back+1)%MAX;
  107. }
  108. else
  109. {
  110. cerr<<"Error Queue is full\n"; // display queue is full
  111. }
  112. }
  113. /*******************************************
  114. FUNCTION: frontNremoveq()
  115. DESCRIPTION: reads and removes front element from queue
  116. PRECONDITION: front pointing to front of queue
  117. POSTCONDITION: front element is returned and then incremented
  118. ********************************************/
  119. inline QueueElement Queue::frontNremoveq()
  120. {
  121. if(front!=back)
  122. {
  123. return(arr[front]); // return front element
  124. front++; // remove front element
  125. }
  126. else
  127. {
  128. cout<<"Queue is empty.\n"; // display queue is empty
  129. return(0);
  130. }
  131. }
  132.  
  133. /***************************MAIN******************************/
  134. int main()
  135. {
  136. Stack S; // initialize stack
  137. Queue Q; // initialize queue
  138. string s;
  139. int i=0; // initialze int 'i'
  140. char string[MAX]; // initialize char string
  141. bool RESULT=false; // initilize bool RESULT to false
  142.  
  143.  
  144. ifstream inside;
  145. ofstream outside;
  146. inside.open("palindromeinput.txt",ios::in);
  147. outside.open("palindromeoutput.txt",ios::out);
  148.  
  149. cout<<string;
  150. while(string[i]!=NULL)
  151.  
  152. {
  153. S.push(string[i]); // push chars individually from string to
  154. Q.addq(string[i]); // stack and queue
  155. i++; // next char
  156. }
  157. while(i>0)
  158. {
  159. if(S.topNpop()==Q.frontNremoveq()) // compare each element from
  160. { // stack and queue
  161. RESULT=true; // if same for all chars return true
  162. }
  163. else
  164. {
  165. RESULT=false; // if not same for any char break and return false
  166. break;
  167. }
  168. i--;
  169. }
  170. if(RESULT==true)
  171. {
  172. cout<<" is a palindrome\n"; // display if true
  173. }
  174. else
  175. {
  176. cout<<" is not a palindrome\n"; // display if false
  177. }
  178.  
  179. return 0; // end of program
  180. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
silicon is offline Offline
15 posts
since Jul 2004
Jul 14th, 2004
0

Re: Stack Queue Fstream

Well it's obvious you're using some sort of template or something I on't understand. To read text from a file use fgets(char *s,int nrchr,FILE *inpfile);
Since you're using fstream go for:
C++ Syntax (Toggle Plain Text)
  1. fstream in("...",ios::in);
  2. fstream out("...".ios::out);
  3. char *s=new char[...];
  4. in.getline(s,100,'\n');

The getline function is a method for istream and ifstream. It's paramters are:
.getline(char* buffer,int nrchars,char='\n');

IT WORKS FOR BORLAND C++ FOR DOS


I nearly forgot:
For palindrome check people USUALLY use: strrev(char *s); WARNING strrev alters s as it gives back the reverse string and also places it in s.
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004
Jul 14th, 2004
0

OFstream output for Stack Queue problem

Hi everyone, I've gotten up to this point but am still having problems. I now have it so that the program is reading from the text file successfully but I cannot get it to successfully write to the output file. Also I believe that it is not checking correctly for palindrome but I am more worried about getting it to write to the output file. Am I missing something small

Thanks

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include <cstdlib> // For EXIT_FAILURE and EXIT_SUCCESS
  3. #include <fstream>
  4. #include<string>
  5. using namespace std;
  6. const int MAX=50; // initialize max string size of 50 characters
  7. typedef char StackElement; // define StackElement
  8. typedef char QueueElement; // define QueueElement
  9. class Stack
  10. {
  11. public:
  12. Stack(){top=-1;arr[MAX]=0;} // default stack constructor
  13. void push(StackElement & ch); // push function
  14. StackElement topNpop(); // top and pop functions combined
  15. bool empty() const; // empty function
  16. private:
  17. StackElement arr[MAX]; // define char array
  18. int top; // define int top
  19.  
  20. };
  21. /*******************************************
  22. FUNCTION: push()
  23. DESCRIPTION: Pushes an element onto the stack
  24. PRECONDITION: Waiting for function call
  25. POSTCONTION: New element character on top of stack
  26. *******************************************/
  27. inline void Stack::push(StackElement & ch)
  28. {
  29. if(top<MAX)
  30. {
  31. top++; // increment top
  32. arr[top]=ch; // push onto stack
  33. }
  34. else
  35. {
  36. cout<<"Stack is full.\n"; // display stack is full
  37. }
  38.  
  39.  
  40. }
  41. /*******************************************
  42. FUNCTION: topNpop()
  43. DESCRIPTION: Reads and pops top element off the stack
  44. PRECONDIION: Waiting for function call
  45. POSTCONDITION: One element read and removed fromt he stack
  46. RETURN: Top element from stack
  47. ********************************************/
  48.  
  49. inline StackElement Stack::topNpop()
  50. {
  51. if(top>-1)
  52. {
  53. return(arr[top]); // returns top element
  54. top--; // remove froms stack
  55. }
  56. else
  57. {
  58. cout<<"Stack is empty.\n"; // display stack is empty
  59. return(0);
  60.  
  61. }
  62.  
  63. }
  64. /*******************************************
  65. FUNCTION: empty()
  66. DESCRIPTION: returns result value if stack is empty
  67. PRECONDITION: result=false
  68. POSTCONDITION: result may be true or remain false
  69. RETURN: result if true or false
  70. ********************************************/
  71. inline bool Stack::empty() const
  72. {
  73. bool result=false; // initialize bool as false
  74. if (top==-1)
  75. {
  76. result=true; // if top is -1 return result true
  77. return(result);
  78. }
  79. else
  80. {
  81. return(result); // else return false
  82. }
  83. }
  84. class Queue // Queue class
  85. {
  86. public:
  87. Queue(){front=0, back=0;arr[MAX]=0;} // Queue default constructor
  88. void addq(QueueElement & ch); // define addq
  89. QueueElement frontNremoveq(); // define frontNremove
  90. private:
  91. QueueElement arr[MAX]; // initialize QueueElement array
  92. int front, back; // initialize int front and back
  93.  
  94. };
  95. /*******************************************
  96. FUNCTION: addq()
  97. DESCRIPTION: adds an element onto the queue
  98. PRECONDITION: Waiting for element to add
  99. POSTCONDITION: New element now on the queue
  100. ********************************************/
  101. inline void Queue::addq(QueueElement &ch)
  102. {
  103. if(front!=(back+1)%MAX)
  104. {
  105. arr[back]=ch; // add element to back of queue
  106. back=(back+1)%MAX;
  107. }
  108. else
  109. {
  110. cerr<<"Error Queue is full\n"; // display queue is full
  111. }
  112. }
  113. /*******************************************
  114. FUNCTION: frontNremoveq()
  115. DESCRIPTION: reads and removes front element from queue
  116. PRECONDITION: front pointing to front of queue
  117. POSTCONDITION: front element is returned and then incremented
  118. ********************************************/
  119. inline QueueElement Queue::frontNremoveq()
  120. {
  121. if(front!=back)
  122. {
  123. return(arr[front]); // return front element
  124. front++; // remove front element
  125. }
  126. else
  127. {
  128. cout<<"Queue is empty.\n"; // display queue is empty
  129. return(0);
  130. }
  131. }
  132.  
  133. /***************************MAIN******************************/
  134. int main()
  135. {
  136. Stack S; // initialize stack
  137. Queue Q; // initialize queue
  138. string s;
  139. int i=0; // initialze int 'i'
  140. char string[MAX]; // initialize char string
  141. bool RESULT=false; // initilize bool RESULT to false
  142.  
  143.  
  144.  
  145. ifstream inside ("palindromeinput.txt");
  146. if (! inside.is_open())
  147. { cout << "Error opening file"; exit (1); }
  148.  
  149. while (! inside.eof() )
  150. {
  151. inside.getline (string,100);
  152. cout << string << endl;
  153. }
  154.  
  155. while(string[i]!=NULL)
  156. {
  157. S.push(string[i]); // push chars individually from string to
  158. Q.addq(string[i]); // stack and queue
  159. i++; // next char
  160. }
  161. while(i>0)
  162. {
  163. if(S.topNpop()==Q.frontNremoveq()) // compare each element from
  164. { // stack and queue
  165. RESULT=true; // if same for all chars return true
  166. }
  167. else
  168. {
  169. RESULT=false; // if not same for any char break and return false
  170. break;
  171. }
  172. i--;
  173. }
  174.  
  175.  
  176. if(RESULT==true)
  177. {
  178. cout<<string<<" is a palindrome\n"; // display if true
  179. }
  180. else
  181. {
  182. cout<<string<<" is not a palindrome\n"; // display if false
  183. }
  184.  
  185.  
  186.  
  187. ofstream outside ("palindromeoutput.txt");
  188. if (outside.is_open())
  189. {
  190.  
  191. outside << string;
  192. outside<< string;
  193. outside.close();
  194. }
  195. return 0;
  196. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
silicon is offline Offline
15 posts
since Jul 2004

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: in need of some hw help
Next Thread in C++ Forum Timeline: Using x86 Assembly Language with Microsoft Visual C++





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


Follow us on Twitter


© 2011 DaniWeb® LLC