| | |
Stack Queue Fstream
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2004
Posts: 15
Reputation:
Solved Threads: 0
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:
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
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)
ifstream inside; ofstream outside; inside.open("palindromeinput.txt",ios::in); 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)
#include<iostream> #include <cstdlib> // For EXIT_FAILURE and EXIT_SUCCESS #include <fstream> #include<string> using namespace std; const int MAX=50; // initialize max string size of 50 characters typedef char StackElement; // define StackElement typedef char QueueElement; // define QueueElement class Stack { public: Stack(){top=-1;arr[MAX]=0;} // default stack constructor void push(StackElement & ch); // push function StackElement topNpop(); // top and pop functions combined bool empty() const; // empty function private: StackElement arr[MAX]; // define char array int top; // define int top }; /******************************************* FUNCTION: push() DESCRIPTION: Pushes an element onto the stack PRECONDITION: Waiting for function call POSTCONTION: New element character on top of stack *******************************************/ inline void Stack::push(StackElement & ch) { if(top<MAX) { top++; // increment top arr[top]=ch; // push onto stack } else { cout<<"Stack is full.\n"; // display stack is full } } /******************************************* FUNCTION: topNpop() DESCRIPTION: Reads and pops top element off the stack PRECONDIION: Waiting for function call POSTCONDITION: One element read and removed fromt he stack RETURN: Top element from stack ********************************************/ inline StackElement Stack::topNpop() { if(top>-1) { return(arr[top]); // returns top element top--; // remove froms stack } else { cout<<"Stack is empty.\n"; // display stack is empty return(0); } } /******************************************* FUNCTION: empty() DESCRIPTION: returns result value if stack is empty PRECONDITION: result=false POSTCONDITION: result may be true or remain false RETURN: result if true or false ********************************************/ inline bool Stack::empty() const { bool result=false; // initialize bool as false if (top==-1) { result=true; // if top is -1 return result true return(result); } else { return(result); // else return false } } class Queue // Queue class { public: Queue(){front=0, back=0;arr[MAX]=0;} // Queue default constructor void addq(QueueElement & ch); // define addq QueueElement frontNremoveq(); // define frontNremove private: QueueElement arr[MAX]; // initialize QueueElement array int front, back; // initialize int front and back }; /******************************************* FUNCTION: addq() DESCRIPTION: adds an element onto the queue PRECONDITION: Waiting for element to add POSTCONDITION: New element now on the queue ********************************************/ inline void Queue::addq(QueueElement &ch) { if(front!=(back+1)%MAX) { arr[back]=ch; // add element to back of queue back=(back+1)%MAX; } else { cerr<<"Error Queue is full\n"; // display queue is full } } /******************************************* FUNCTION: frontNremoveq() DESCRIPTION: reads and removes front element from queue PRECONDITION: front pointing to front of queue POSTCONDITION: front element is returned and then incremented ********************************************/ inline QueueElement Queue::frontNremoveq() { if(front!=back) { return(arr[front]); // return front element front++; // remove front element } else { cout<<"Queue is empty.\n"; // display queue is empty return(0); } } /***************************MAIN******************************/ int main() { Stack S; // initialize stack Queue Q; // initialize queue string s; int i=0; // initialze int 'i' char string[MAX]; // initialize char string bool RESULT=false; // initilize bool RESULT to false ifstream inside; ofstream outside; inside.open("palindromeinput.txt",ios::in); outside.open("palindromeoutput.txt",ios::out); cout<<string; while(string[i]!=NULL) { S.push(string[i]); // push chars individually from string to Q.addq(string[i]); // stack and queue i++; // next char } while(i>0) { if(S.topNpop()==Q.frontNremoveq()) // compare each element from { // stack and queue RESULT=true; // if same for all chars return true } else { RESULT=false; // if not same for any char break and return false break; } i--; } if(RESULT==true) { cout<<" is a palindrome\n"; // display if true } else { cout<<" is not a palindrome\n"; // display if false } return 0; // end of program }
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:
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.
Since you're using fstream go for:
C++ Syntax (Toggle Plain Text)
fstream in("...",ios::in); fstream out("...".ios::out); char *s=new char[...]; 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.
•
•
Join Date: Jul 2004
Posts: 15
Reputation:
Solved Threads: 0
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
Thanks
C++ Syntax (Toggle Plain Text)
#include<iostream> #include <cstdlib> // For EXIT_FAILURE and EXIT_SUCCESS #include <fstream> #include<string> using namespace std; const int MAX=50; // initialize max string size of 50 characters typedef char StackElement; // define StackElement typedef char QueueElement; // define QueueElement class Stack { public: Stack(){top=-1;arr[MAX]=0;} // default stack constructor void push(StackElement & ch); // push function StackElement topNpop(); // top and pop functions combined bool empty() const; // empty function private: StackElement arr[MAX]; // define char array int top; // define int top }; /******************************************* FUNCTION: push() DESCRIPTION: Pushes an element onto the stack PRECONDITION: Waiting for function call POSTCONTION: New element character on top of stack *******************************************/ inline void Stack::push(StackElement & ch) { if(top<MAX) { top++; // increment top arr[top]=ch; // push onto stack } else { cout<<"Stack is full.\n"; // display stack is full } } /******************************************* FUNCTION: topNpop() DESCRIPTION: Reads and pops top element off the stack PRECONDIION: Waiting for function call POSTCONDITION: One element read and removed fromt he stack RETURN: Top element from stack ********************************************/ inline StackElement Stack::topNpop() { if(top>-1) { return(arr[top]); // returns top element top--; // remove froms stack } else { cout<<"Stack is empty.\n"; // display stack is empty return(0); } } /******************************************* FUNCTION: empty() DESCRIPTION: returns result value if stack is empty PRECONDITION: result=false POSTCONDITION: result may be true or remain false RETURN: result if true or false ********************************************/ inline bool Stack::empty() const { bool result=false; // initialize bool as false if (top==-1) { result=true; // if top is -1 return result true return(result); } else { return(result); // else return false } } class Queue // Queue class { public: Queue(){front=0, back=0;arr[MAX]=0;} // Queue default constructor void addq(QueueElement & ch); // define addq QueueElement frontNremoveq(); // define frontNremove private: QueueElement arr[MAX]; // initialize QueueElement array int front, back; // initialize int front and back }; /******************************************* FUNCTION: addq() DESCRIPTION: adds an element onto the queue PRECONDITION: Waiting for element to add POSTCONDITION: New element now on the queue ********************************************/ inline void Queue::addq(QueueElement &ch) { if(front!=(back+1)%MAX) { arr[back]=ch; // add element to back of queue back=(back+1)%MAX; } else { cerr<<"Error Queue is full\n"; // display queue is full } } /******************************************* FUNCTION: frontNremoveq() DESCRIPTION: reads and removes front element from queue PRECONDITION: front pointing to front of queue POSTCONDITION: front element is returned and then incremented ********************************************/ inline QueueElement Queue::frontNremoveq() { if(front!=back) { return(arr[front]); // return front element front++; // remove front element } else { cout<<"Queue is empty.\n"; // display queue is empty return(0); } } /***************************MAIN******************************/ int main() { Stack S; // initialize stack Queue Q; // initialize queue string s; int i=0; // initialze int 'i' char string[MAX]; // initialize char string bool RESULT=false; // initilize bool RESULT to false ifstream inside ("palindromeinput.txt"); if (! inside.is_open()) { cout << "Error opening file"; exit (1); } while (! inside.eof() ) { inside.getline (string,100); cout << string << endl; } while(string[i]!=NULL) { S.push(string[i]); // push chars individually from string to Q.addq(string[i]); // stack and queue i++; // next char } while(i>0) { if(S.topNpop()==Q.frontNremoveq()) // compare each element from { // stack and queue RESULT=true; // if same for all chars return true } else { RESULT=false; // if not same for any char break and return false break; } i--; } if(RESULT==true) { cout<<string<<" is a palindrome\n"; // display if true } else { cout<<string<<" is not a palindrome\n"; // display if false } ofstream outside ("palindromeoutput.txt"); if (outside.is_open()) { outside << string; outside<< string; outside.close(); } return 0; }
![]() |
Similar Threads
- stack/queue program for long math operation - need help (C++)
- Help on stack , queue, palindrome program... (C++)
- at the moment this is a stack of queue, i am trying to make it into first in first ou (C++)
Other Threads in the C++ Forum
- Previous Thread: in need of some hw help
- Next Thread: Using x86 Assembly Language with Microsoft Visual C++
Views: 7072 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





