[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 427
  • Last Modified:

stack and palimdrome: Problem with my codec

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;}

Open in new window

0
pgmerLA
Asked:
pgmerLA
  • 9
  • 7
1 Solution
 
Todd GerbertIT ConsultantCommented:
What's wrong with'em?
0
 
pgmerLAAuthor Commented:
it doesn't compile.
0
 
Todd GerbertIT ConsultantCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
pgmerLAAuthor Commented:
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
 
Todd GerbertIT ConsultantCommented:
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
 
Todd GerbertIT ConsultantCommented:
Hint: the error message pretty much says all you need to know. ;)
0
 
pgmerLAAuthor Commented:
@tgerbert: How would you fix it? I  am running short on time....it's almost due and I can't think anymore.
0
 
pgmerLAAuthor Commented:
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
 
Todd GerbertIT ConsultantCommented:
Does it work with all lower case "hannah"?
0
 
pgmerLAAuthor Commented:
No, it doesn't. lower or upper case.
0
 
Todd GerbertIT ConsultantCommented:
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
 
pgmerLAAuthor Commented:
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;}

Open in new window

0
 
Todd GerbertIT ConsultantCommented:
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;
}

Open in new window

0
 
Todd GerbertIT ConsultantCommented:
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
 
pgmerLAAuthor Commented:
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
 
Todd GerbertIT ConsultantCommented:
>> 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);

Open in new window


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);
}

Open in new window

0
 
sarabandeCommented:
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 )

Open in new window


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);

Open in new window


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

Sara
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now