-
Help again pls
I m once again having problems. I need to get a sentence into an array that I can work with I need to check it for being a palindrome. I ve got it all set up and debugged(or did before playing with it) but cant find the right way to get this sentence put into the stack and que like I need it. Here is the code pls help if you can tks in advance for anything you can give.
Code:
#include <iostream>
const int maxsize = 50;
class stackchar
{
public: stackchar();
void enqueue(char ch);
char front();
void dequeue();
bool isfull();
bool isempty();
bool isempty2();
bool isfull2();
void push(char newchar);
void pop();
char topelement();
private:
int frontelement;
int rear;
char s1[maxsize];
char s2[maxsize];
char s3[maxsize];
int top;
int vecsize;
int top2;
};
-------------------------------------------------------------------------
#include "s2p1.h"
#include <iostream>
int vecsize = maxsize;
stackchar::stackchar()
{
rear = frontelement = 0;
}
bool stackchar::isempty()
{
return(rear == frontelement);
}
bool stackchar::isfull()
{
return(rear+1%vecsize==frontelement);
}
void stackchar::enqueue(char ch)
{
rear =(ch)%vecsize;
}
char stackchar::front()
{
return s1[frontelement+1%vecsize];
}
void stackchar::dequeue()
{
frontelement=(frontelement+1)%vecsize;
}
bool stackchar::isfull2()
{
return(top==maxsize);
}
bool stackchar::isempty2()
{
return(top==-1);
}
void stackchar::push(char newchar)
{
s2[++top]=newchar;
}
char stackchar::topelement()
{
return s2[top];
}
void stackchar::pop()
{
top--;
}
}
-------------------------------------------------------------
#include "s2p1.cpp"
#include<iostream>
#include<fstream.h>
#include<string>
#include <istream>
int main()
{
stackchar s1;
stackchar s2;
stackchar s3;
int frontelement;
int rear;
char ch[50];
int top2;
cout<<"Please enter a sentence to be tested $ to exit: ";
cin.getline(ch, 50);
while (!s2.isfull())
{
s2.push(ch);
s1.enqueue(ch);
}
while (!s2.isempty2() && s2.topelement() == s1.front())
{
s2.pop();
s1.dequeue();
}
if (!s2.isempty2())
{
cout<<"Sentence is not a palindrome";
}
else
{
cout<<"Sentence is a palindrome when applying stack and queue";
}
return 0;
}
I haven't worked much with char. and that is where my problem lies I think.
-
1 - Please dont post threads with titles like this one. Choose something that is meaningful. Remember, most people need help, that's why they post ;)
2 - When you post a message, the foum software sometimes changes characters to smilies, like it has in the middle of your code. You can turn this feature off by ticking "Disable Smilies in This Post" before submitting the post.
3 - The real help... This:
>>void stackchar :: push (char newchar)
takes a single char as a parameter, whereas this:
>>s2.push(ch);
is passing an array (a pointer to a list of chars).
I suggest you research the char array concept before going any further.
-
sorry I will try to be more helpfull in my message title. I looked around a little and hope I learned a bit I changed the code to look like this. will this take a sentence and store it into an array called s1 for me? it compiles fine but crashed when run.
Code:
cout<<"Please enter a sentence to be tested $ to exit: ";
while( (c=getchar( )) != '\n' )
{
if( n < 100 )
ch[n] = c;
n++;
}
while (!s2.isfull())
{
s1.enqueue(ch[n]);
s2.push(ch[n]);
n--;
}
the rest of the program is the same.
-
Lookup cin.getline(). That will read a complete line of data from the user, without the need for the loop.
I haven't looked at the rest of your code...
-
the line:
cin.getline(ch, 50);
wasn't the real problem, although it might be safer to use:
cin.getline(ch, 49);
but that's another issue. The problem was use of the array ch as a single char when passed to s2.push() or s1.enqueue(). Your revised code went further that it needed to in it's revisions. If you to retain cin.getline() as in your first version then you could do something like this instead to keep lines of code to a minimum:
for(i = 0; i < strlen(ch); ++i)
{
s2.push(ch[i]);
s1.enqueue(ch[i]);
}
-
Well I cant tell if either is working both ways compile but both also crash when run. The getline gives a warning of compairing signed and unsigned values in the for loop. The crashing is now the real problem I cant do much else till I get that fixed.
Tks everyone for their help so far.
-
Do you really have to do this with a stack?
You know there is a much easier way to check palindromes in about 3 lines with pointers?
-
Yeah, Stoned_Coder is right. It can be done much easier without the stack- but it is a good exercise using the stack ADT.
Mr. c.
-
to find out where it is crashing add statements to display results and hold screen so you can see results as you work your way through each loop, or use your debugger if you have one.
Code:
cin.getline(ch, 50);
cout << ch;//check input obtained appropriately
char temp;
cin >> temp;
while (!s2.isfull())
{
for(i = 0; i < strlen(ch); ++i)
{
cout << ch[i];
s2.push(ch[i]);
cout << s2.topelement();//check on push() and topelement()
s1.enqueue(ch[i]);
cout << s1.topelement();/check on enfqueue and topelement();
cin >> temp;
}
}
If you do that I think you'll find a several problems. First, you never initialize top in the class. therefore when you try to increment top in the push() function, it is junk and crashes your program.
Second, what do think happens when you do try to use modulo on a char variable as in enqueue()? Check it out by putting a
cout << rear;
char temp2;
cin >> temp2;
in the enqueue function.
Third, check each of the loops and each of the class methods by similar technique to look for other problems.
Fourth, I would encourage you not to use the same variable name for different variables in your program. It is a recipe for disaster. In particular you use ch for both a string in main() and as a single char in a number of class methods and you use s1, s2, and s3 as array names/data members in the class and as instances of stackchar in main(). Don't do it!!
-
Well guys I m working on totally redoing this code. I dont have a lot of choice in this matter it is an assignment and the teacher didn't give us much room to chose our own variables etc. many of the variable names I cant even seem to get to work because they are the same as the function names. I m redoing it a bit hope to have something not so worthless written soon. Man I m not starting this week right.
-
PS I really do appreciate the help this assignment really has me messed up and it's due tomarrow.