Solved

stack and palimdrome: Problem with my codec

Posted on 2011-04-28
422 Views
Hi, could you please fix my code?
We just covered stacks in class and we have to write a code for a palimdrome(a word that reads the same from both ends) The teacher wants us to use: A Toyota

I would greatly appreciate anyone who could fix the pop and push functions.
Thanks.
``````#include<iostream>

using namespace std;

template<class T, int n>
class Stack
{private: T x[n];int counter;
public: void ClearStack(){counter =0;}
bool FullStack(){if(counter ==n) return true;
else return false;}
bool EmptyStack(){if (counter == 0) return true;
else return false;}
void Push(T a){x[counter]=a; ++counter;}
void Pop(T a){counter--; return x[counter];}
};

int main()
{
Stack<char,80> LR;
Stack<char,80> RL;
Stack<char,80> Temp;

LR.ClearStack(); RL.ClearStack(); Temp.ClearStack();

char c,c1,c2;

cout<<"Enter a sentence:";
while(cin.get(c), c!='\n')
{if(isalpha(c))
{c=toupper(c); LR.Push(c);Temp.Push(c);}
}

while(!Temp.EmptyStack())
{Temp.Pop(c);RL.Push(c);}

while(!LR.EmptyStack())
{c1=LR.Pop(c); c2=RL.Pop(c);
if(c1 != c2) break;}

if(LR.EmptyStack() == true) cout<<"It's a palindrome"<<endl;
else cout<<"It's not a palimdrom"<<endl;

return 0;}
``````
0
Question by:pgmerLA

LVL 33

Expert Comment

What's wrong with'em?
0

Author Comment

it doesn't compile.
0

LVL 33

Expert Comment

Why not?

I could do this back & forth all day long, but if you want an answer in the near future some more details would be useful. ;)
0

Author Comment

Sorry guys.
My compiler(MS VS2008) says:
error C2440: '=' : cannot convert from 'void' to 'char'
1>        Expressions of type void cannot be converted to other types
1>c:\documents and settings\massoud\my documents\visual studio 2008\projects\proj\proj\main.cpp(38) : error C2440: '=' : cannot convert from 'void' to 'char'
1>        Expressions of type void cannot be converted to other types

Line(38): I am trying to compare the letters that are being poped by the 2 stacks to see if they are similar and if they are not, then it's not a palimdrome. But I don't know how to write the pop function to let me do the char comparison.
0

LVL 33

Expert Comment

Okay, the error points to line 38, which is in your main() function, {c1=LR.Pop(c); c2=RL.Pop(c);; so this is taking the return value of LR.Pop(...) and assigning it to c1, which is a char (and the same thing for RL.Pop(...)/c2).  So, according to the function declaration in your code above, what is the return type of Stack::Pop()?
0

LVL 33

Expert Comment

Hint: the error message pretty much says all you need to know. ;)
0

Author Comment

@tgerbert: How would you fix it? I  am running short on time....it's almost due and I can't think anymore.
0

Author Comment

I changed the pop function to:

char Pop(T a){counter--; return x[counter];

and when I type Hannah, the program tells me it's not a palindrome.
0

LVL 33

Expert Comment

Does it work with all lower case "hannah"?
0

Author Comment

No, it doesn't. lower or upper case.
0

LVL 33

Expert Comment

Hmm, I copy/pasted your code & it worked for me, I don't I changed anything other than what you just did, but I'm not in front of my computer right now - will take a closer look as soon as I'm not driving. ;)

In the mean time can you re-post your whole code as it is now?
0

Author Comment

here is the new code(not working)
I typed: a toyota (prof example)
and hannah
and none worked :(
``````#include<iostream>

using namespace std;

template<class T, int n>
class Stack
{private: T x[n];int counter;
public: void ClearStack(){counter =0;}
bool FullStack(){if(counter ==n) return true;
else return false;}
bool EmptyStack(){if (counter == 0) return true;
else return false;}
void Push(T a){x[counter]=a; ++counter;}
char Pop(T a){counter--; return x[counter];}
};

int main()
{
Stack<char,80> LR;
Stack<char,80> RL;
Stack<char,80> Temp;

LR.ClearStack(); RL.ClearStack(); Temp.ClearStack();

char c,c1,c2;

cout<<"Enter a sentence:";
while(cin.get(c), c!='\n')
{if(isalpha(c))
{c=toupper(c); LR.Push(c);Temp.Push(c);}
}

while(!Temp.EmptyStack())
{Temp.Pop(c);RL.Push(c);}

while(!LR.EmptyStack())
{c1=LR.Pop(c); c2=RL.Pop(c);
if(c1 != c2) break;}

if(LR.EmptyStack() == true) cout<<"It's a palindrome"<<endl;
else cout<<"It's not a palimdrom"<<endl;

return 0;}
``````
0

LVL 33

Expert Comment

Okay, so your program basically consists of three steps:

1) Push each character onto the LR and Temp stacks.

2) Pop each character off the Temp stack and push it onto the RL stack (thus making RL the reverse of LR).

3) Compare each character of LR and RL in order, if it's a palindrome each character will match - if it's not the comparison will be false for at least one of the characters.

Is step 1 working correctly?  Since "hannah" doesn't work any better than "Hannah" it's probably safe to assume the upper- or lower-case of the characters is not an issue, at this point.  What happens when you call LR.Push(c)?

Is step 2 working correctly?  What happens when you call Temp.Pop(c)?

Is step 3 working correctly?  What happens when you call "c1 = LR.Pop(c)"?

The Push(...) function takes a parameter named "a" - what does it do with that parameter?

The Pop(...) function takes a parameter named "a" as well - what does it do with the parameter?

Stepping through your code a line at a time might help, and/or adding "cout" lines you so you can see what's going on and where.

This is your code (I just re-formatted it so it's easier to read - do you actually write code like that or did it just get mangled when you pasted it?), I didn't change it's functionality, the only thing I did is add all the "cout" lines.

``````#include <iostream>

using namespace std;

template<class T, int n>
class Stack
{
private:
T x[n];
int counter;
public:
void ClearStack()
{
counter = 0;
}

bool FullStack()
{
if(counter == n)
return true;
else
return false;
}

bool EmptyStack()
{
if (counter == 0)
return true;
else
return false;
}

void Push(T a)
{
x[counter] = a;
++counter;
}

char Pop(T a)
{
counter--;
return x[counter];
}
};

int main()
{
Stack<char, 80> LR;
Stack<char, 80> RL;
Stack<char, 80> Temp;

LR.ClearStack();
RL.ClearStack();
Temp.ClearStack();

char c, c1, c2;

cout << "Enter a sentence:";

// Step 1
while (cin.get(c), c != '\n')
{
if (isalpha(c))
{
c = toupper(c);

cout << "Pushing '" << c << "' onto LR" << endl;
LR.Push(c);
cout << "Pushing '" << c << "' onto Temp" << endl;
Temp.Push(c);
}
}

// Step 2
while (!Temp.EmptyStack())
{
Temp.Pop(c);
cout << "Popped '" << c << "' from Temp" << endl;
RL.Push(c);
cout << "Pushed '" << c << "' onto RL" << endl;
}

// Step 3
while (!LR.EmptyStack())
{
c1 = LR.Pop(c);
cout << "Popped '" << c1 << "' from LR" << endl;
c2 = RL.Pop(c);
cout << "Popped '" << c2 << "' from RL" << endl;

if (c1 != c2)
{
cout << "'" << c1 << "' and '" << c2 << "' are not equal, exiting while loop." << endl;
break;
}
else
{
cout << "'" << c1 << "' and '" << c2 << "' are equal, continuing to next char." << endl;
}
}

if (LR.EmptyStack() == true)
cout << "It's a palindrome" << endl;
else
cout << "It's not a palimdrom" << endl;

// This just pauses the program so when I hit
// F5 in Visual Studio the console window stays
// open for me
cin.get();

return 0;
}
``````
0

LVL 33

Expert Comment

Incidentally, reconsider the return type of Pop.  What if I made a Stack<float, 10> floatStack;, would Pop's return type still make sense?
0

Author Comment

do you mean changing Pop to:       T Pop(T a){counter--; return x[counter];}
the return type has to be a char so that they can be compared.
I am trying to understand your comment, but I am having a hard time. could you give me another hint?
0

LVL 33

Accepted Solution

>> do you mean changing Pop to:       T Pop(T a){counter--; return x[counter];}

Yes.  But that's not why it wasn't working (just a good idea).

In step 2 (in my code example lines 78 & 80) you write:
``````Temp.Pop(c);
RL.Push(c);
``````

But the Pop function never assigns anything to "c", so you're just repeatedly pushing whatever value "c" happens to already have (which is a newline from cin.get() in the loop in step 1).  Pop doesn't need to take a parameter, and does need to return a value.

``````// Step 2
while (!Temp.EmptyStack())
{
c = Temp.Pop();
RL.Push(c);
}
``````
0

LVL 32

Expert Comment

you could have gone with your initial code, but making the Pop the reverse of Push and make the argument an input/output argument:

``````void Pop(T & a )
``````

The T& means you pass the argument by reference such that the variable in Pop function is only an alias of the variable the calling function would supply.

if changed the argument to T& you then do the reverse of the Push code:

1. decrement the counter
2. assign x[counter] to a

because of the T& it now was returned to the caller. in the calling function a call like

``````LR.pop(c);
``````

now would fill the c variable with the (last) stack value.

Sara
0

Featured Post

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generatâ€¦
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the bâ€¦
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.