CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2008
    Posts
    43

    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.

  2. #2
    Join Date
    Oct 2009
    Posts
    577

    Smile 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.

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

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

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  5. #5
    Join Date
    Mar 2008
    Posts
    43

    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

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help with Deleting White Spaces

    This is a well-known subtlety of mixing >> with getline(). Do a Google search for "skipping getline".

  7. #7
    Join Date
    Jan 2009
    Posts
    1,689

    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.

  8. #8
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    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
  •  





Click Here to Expand Forum to Full Width

Featured