-
October 1st, 2010, 02:23 AM
#1
Help with Deleting White Spaces
I am working on a Palindrome program, everything works except when it comes to spaces. The program should delete spaces in words. For example if the user enters bo b, the program should treat it as bob. Right now the program would treat it as bo. Can someone show me what I am doing wrong?
Code:
bool operator==(const stack<char>&, const queue<char>&);
bool operator!=(const stack<char>&, const queue<char>&);
int main(int argc, char *argv[]) {
stack<char> *s;
queue<char> *q;
string input;
char answer='y';
string::iterator i;
while (answer=='y') {
s = new stack<char>;
q = new queue<char>;
cout << "Is your word a palindrome? Enter the word. "<<endl;
cin>>input;
for (int i = 0; input[i] != '\0'; i++){
if(input[i]==' '){
input.erase(i,1);
input[i] = (char)tolower(input[i]);
}
else{
input[i] = (char)tolower(input[i]);
}
}
for (i = input.begin(); i != input.end(); i++) {
s->push(*i);
q->push(*i);
}
cout << input << " is ";
if (*s != *q) {
cout << "not ";
}
cout << "a palindrome." << endl;
delete s;
delete q;
cout<<"Would you like to try another word? Enter y for yes and n for no"<<endl;
cin>>answer;
}
return 0;
}
bool operator==(const stack<char> &s1, const queue<char> &q1) {
bool eq = true;
stack<char> s = s1;
queue<char> q = q1;
if (s.size() == q.size()) {
while ((s.empty() == false) && (eq == true)) {
eq = (s.top() == q.front());
s.pop();
q.pop();
}
} else {
eq = false;
}
return eq;
}
bool operator!=(const stack<char> &s, const queue<char> &q) {
return !(s == q);
}
Last edited by bananasplitkids; October 1st, 2010 at 02:25 AM.
-
October 1st, 2010, 02:50 AM
#2
Re: Help with Deleting White Spaces
The problem is with cin >> input;
That statement doesn't allow to enter space char.
Use getline(cin, input); instead.
-
October 1st, 2010, 05:57 AM
#3
Re: Help with Deleting White Spaces
1) As mentioned in the previous post, operator >> stops at whitespaces. Use getline()
2) The following loop is incorrect:
Code:
for (int i = 0; input[i] != '\0'; i++){
It is not guarenteed that std::string variables have a trailing NULL. You would check using:
Code:
for (int i = 0; i<input.size(); i++){
3) Even with the above check, your loop would not work correctly. Use the remove
algorithm followed by erase (#include <algorithm>)
Code:
input.erase( remove(input.begin(),input.end(),' ') , input.end() );
-
October 1st, 2010, 07:57 AM
#4
Re: Help with Deleting White Spaces
Code:
int main(int argc, char *argv[]) {
stack<char> *s;
queue<char> *q;
...
while (answer=='y') {
s = new stack<char>;
q = new queue<char>;
...
delete s;
delete q;
}
return 0;
}
Why are you allocating the stack and queue on the heap? There's absolutely no reason to do so. Declaring them on the stack local to the while loop would have precisely the same effect without the dynamic allocation penalty. In fact you could declare them on the stack local to all of main, and just .clear() them at the start of each loop, which might have some slight additional efficiency gains.
-
October 1st, 2010, 08:52 AM
#5
Re: Help with Deleting White Spaces
Thanks. I am almost done but I have one small problem. The user can run the program again by pressing y or n. If the user presses n, the program ends correctly. However when the user enters y, the program does goes back through the loop, but it doesnt get the user input. What could cause this because it does reach that line but for some reason it isnt asking for input.
Here is the output
Is your word a palindrome? Enter the word.
bo B
bob is a palindrome.
Would you like to try another word? Enter y for yes and n for no
y
Is your word a palindrome? Enter the word.
is a palindrome.
Would you like to try another word? Enter y for yes and n for no
n
-
October 1st, 2010, 09:36 AM
#6
Re: Help with Deleting White Spaces
This is a well-known subtlety of mixing >> with getline(). Do a Google search for "skipping getline".
-
October 1st, 2010, 10:51 AM
#7
Re: Help with Deleting White Spaces
I see two definition for i. It's not a problem, but it's confusing, use j or k.
-
October 1st, 2010, 11:57 AM
#8
Re: Help with Deleting White Spaces
Since you are only dealing with character input, I recommend using getline for all inputs within the program. there is no reason that you can't use it even when the user might enter y/n. Then use operator== for std::string to determine if the correct answer was given. That's how I would do it in this case.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|