Maybe I'm just tired from it being late, but I keep getting errors when trying to pop a stack into a string (so I can compare it to another string).
for (i=0; i < s1.size(); i++)
{
a.push(s1[i]);
b.push(s1[i]);
}
while (!(a.empty_stack()))
{
a.pop(n);
cout << n;
}
cout << " " << endl << endl;
while (!(b.empty_stack()))
{
b.pop(s2[j]);
}
for (j=0; j < s1.size(); j++)
{
cout << s2[j] << endl;
}
When I run it and enter a word, I get "string subscript out of range". It only happens with the last for loop so obviously I'm doing something wrong there. I'm having a really hard time figuring out how to pop from one stack to another, or pop from a stack to a string. Anyone have any example code or links? Thanks.
In your last for loop, you are accessing s2, but checking your index against the size of s1.
Also, note that even with that mistake corrected your present code will output the null terminator character at the end of the string. While this doesn't seem to make a difference in practise, when I compiled your sample, you could check instead your iterator against string::length().
Can we see a bit more code; I don't see why that should cause you a problem once you change s1.size() to s2.length().
Also, I would update your code to s2.length(). Regardless of whether it fixes the problem, I don't see why you are iterating through one string while checking the iterator against another string's length.
PS: You should use [code] tags rather than [quote], to get syntax highlighting :)
Hey thanks for checking back. Basically I'm doing the standard "use stacks to see if a word is a palindrome" problem, but I was never taught the proper syntax for using stacks. I know logically how to solve the problem, I cant translate that logic into proper syntax and that's what's making the whole thing a big deal for me. Here is the entire code so far.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
constint maxstack = 30;
class stack_type
{
public:
void clear_stack();
bool empty_stack();
bool full_stack();
void push(char word);
void pop(char& word);
int stack[maxstack];
int top;
};
//-------------------------------------------------
int main()
{
int i = 0;
int j = 0;
char n = 0;
char m = 0;
stack_type a, b;
a.clear_stack();
b.clear_stack();
cout << "Stacks have been cleared." << endl;
cout << a.top << " is the top of stack A." << endl;
cout << b.top << " is the top of stack B." << endl;
cout << "Enter a word, sentence or number. The program will see if it is palindromic." << endl;
cout << "Enter your selection here =====> ";
std::string s1;
std::string s2;
getline (cin,s1);
cout << "The size of your input is " << s1.size() << " characters.\n" << endl;
for (i=0; i < s1.size(); i++)
{
a.push(s1[i]);
b.push(s1[i]);
}
while (!(a.empty_stack()))
{
a.pop(n);
cout << n;
}
cout << " " << endl << endl;
while (!(b.empty_stack()))
{
b.pop(s2[j]);
}
for (j=0; j < s2.length(); j++)
{
cout << s2[j] << endl;
}
}
//-------------------------------------------------
void stack_type::clear_stack()
{
top = 0;
}
//-------------------------------------------------
bool stack_type::empty_stack()
{
if (top==0)
returntrue;
elsereturnfalse;
}
//-------------------------------------------------
bool stack_type::full_stack()
{
if (top==maxstack-1)
returntrue;
elsereturnfalse;
}
//-------------------------------------------------
void stack_type::push(char numb)
{
top = top + 1;
stack[top]=numb;
}
//-------------------------------------------------
void stack_type::pop(char& numb)
{
numb = stack[top];
top = top - 1;
}
//-------------------------------------------------
I don't think the error is in that last loop on lines 61-64.
I think it is lines 56-59 causing the problem.
You attempt to change s2[j], i.e. s2[0] (is this eve what you mean to do?), but s2 is of length 0, so it has no first character to change. Hence your subscript problem.
Consider allocating your string first: string s2(4, ' ');
would construct the string as four ' ' characters, i.e. " ".
This should solve your problem; you just need to think about how many characters your string s2 needs to have.
int i = 0; //counter for characters
int j = 0;
char n = 0;
stack_type a;
a.clear_stack();
cout << "Stacks have been cleared." << endl;
cout << a.top << " is the top of stack A." << endl << endl;
cout << "Enter a word, sentence or number. The program will see if it is palindromic." << endl;
cout << "Enter your selection here =====> ";
string s1(5, ' ');
string s2(5, ' ');
getline (cin,s1);
cout << "The size of your input is " << s1.size() << " characters.\n" << endl;
for (i=0; i < s1.size(); i++)
{
a.push(s1[i]);
}
for (j=0; j < s2.size(); j++)
{
a.pop(s2[j]);
}
cout << "String 1 is : " << s1 << endl;
cout << "String 2 is : " << s2 << endl;
cout << " " << endl << endl;
if (s1 == s2)
{
cout << "Your number or expression is a palindrome." << endl;
}
else
cout << "Your number or expression is not a palindrome." << endl;
This is working great (except for the fact that I need to remove spaces, punctuation, and change capital to lowercase letters) except for one thing.
string xx(#, ' '); works great, except that if the word or number entered isn't exactly that long, the program obviously won't work correctly. How can I get the string length there to match the length of the input (since I don't know how long the input will be)?
Hey, thanks for your response yet again. I actually got the program running excellently about an hour ago with your tips. Now I'm just working on removing spaces from the string. I converted punctuation to spaces, but now I need to remove the spaces. I should probably convert capital letters to lowercase also, but the space issue is more important.
If you run into any lines of code before I do that will do that for me, don't be afraid to share them =)
edit
got it
1 2 3 4 5 6 7 8 9 10 11 12
for ( unsignedint i = 0, j ; i < s1.length( ) ; ++ i )
{
if ( s1 [i] == ' ' )
{
for ( j = i + 1; j < s1.length ( ) ; ++j )
{
if ( s1 [j] != ' ' )
break ;
}
s1 = s1.erase ( i, (j - i) ) ;
}
}
I'm just updating this in case anyone ever needs it as a reference.