Thread: Crashing...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    36

    Crashing...

    Hello,
    I have created a basic palinedrome program that works fine when obtaining words using commands like
    Code:
    cin >> input;
    , but I would like to elaborate and gets full sentences as inputs. I tried using the getline command, but my program crashes tremendously when in use. Maybe I am using it improperly? Some of my code is posted below:

    Code:
     getline(cin, input);
    
    	while (input[0] != '0') {
    		front = 0;
    	    back = input.length() - 1;
    		palindrome = true;
    
    		while (done == false) {
    		    input[front] = tolower(input[front]);
    			input[back] = tolower(input[back]);
    
                if (input[front] == input[back]) {
                    front++;
    				back--;
    			} else {
    				palindrome = false;
                    done = true;
    			}
    			if (front >= back) {
    				done = true;
    			}
    
    		}
    
        if (palindrome == true) {
    	   cout << "\"" << input << "\"" << " is a palindrome."  << endl;
    	} else {
    	   cout << "\"" << input << "\"" << " is not a palindrome." << endl;
    	}
    
    	done = false;
    	getline(cin, input);
    	}
    Any suggestions or ideas would be greatly appreciated. Take care.

  2. #2
    Registered Trademark
    Join Date
    Apr 2005
    Posts
    19
    just by looking at that, i'm almost sure it's in your first while statement.

    when does your input include 0 in your first element? probably never. so your while loop is an infinite loop. I don't know if you're trying to see if something is tin an element or not, so if it is, try putting a whitespace in between the ' ' instead of '0'. If your getting an element, try out pointers for a change.

    also, is that ALL of your code?
    pardon the double post. My browser didn't react quickly for DSL and i thought i didn't click the button properly

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    The first while loop is there so if the user enters a zero, I want the program to end. If you want my entire coding, here it is:

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    
    using namespace std;
    
    void header();
    
    int main() {
    
    	int front;
    	bool done = false;
    	bool palindrome = true;
    	string::size_type back;
    	string input;
        header();
    	cout << "Enter your word or sentence that is less than 20 characters." << endl;
    	cout << "Let your sentences only consist of letters and spaces." << endl;
    	cout << "When finished, press 0." << endl << endl;
    
        getline(cin, input);
    
    	while (input[0] != '0') {
    		front = 0;
    	    back = input.length() - 1;
    		palindrome = true;
    
    		while (done == false) {
    		    input[front] = tolower(input[front]);
    			input[back] = tolower(input[back]);
    			string input;
    
                if (input[front] == input[back]) {
                    front++;
    				back--;
    			} else {
    				palindrome = false;
                    done = true;
    			}
    			if (front >= back) {
    				done = true;
    			}
    
    		}
    
        if (palindrome == true) {
    	   cout << "\"" << input << "\"" << " is a palindrome."  << endl;
    	} else {
    	   cout << "\"" << input << "\"" << " is not a palindrome." << endl;
    	}
    
    	done = false;
    	getline(cin, input);
    	}
    
        cout << "Ending program..." << endl;
    
    
    	return 0;
    }
    
    void header() {
    	 cout << "************************" << endl;
    	 cout << "  Palindrome Detection "  << endl;
    	 cout << "************************" << endl << endl;
    }
    Apparently, the getline command has some bugs in the actual library file: http://people.msoe.edu/~welch/courses/string.html

    If no one has any ideas on how to use the getline in my program without the continual crashing, is there an easy alternative to getline that serves the same purpose?

    Thanks.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. Turn OFF TABS in your text editor. It might look OK for you, but when you come to post code to the board, it's a unholy mess. Try pressing the review button to make sure your post is presentable.
    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    
    using namespace std;
    
    void header();
    
    int main()
    {
    
        int front;
        bool done = false;
        bool palindrome = true;
        string::size_type back;
        string input;
        header();
        cout << "Enter your word or sentence that is less than 20 characters." << endl;
        cout << "Let your sentences only consist of letters and spaces." << endl;
        cout << "When finished, press 0." << endl << endl;
    
        getline(cin, input);
    
        while (input[0] != '0') {
            front = 0;
            back = input.length() - 1;
            palindrome = true;
    
            while (done == false) {
                input[front] = tolower(input[front]);
                input[back] = tolower(input[back]);
                string input;  // GET RID OF THIS!!!!
    
                if (input[front] == input[back]) {
                    front++;
                    back--;
                } else {
                    palindrome = false;
                    done = true;
                }
                if (front >= back) {
                    done = true;
                }
    
            }
    
            if (palindrome == true) {
                cout << "\"" << input << "\"" << " is a palindrome." << endl;
            } else {
                cout << "\"" << input << "\"" << " is not a palindrome." << endl;
            }
    
            done = false;
            getline(cin, input);
        }
    
        cout << "Ending program..." << endl;
    
        return 0;
    }
    
    void header()
    {
        cout << "************************" << endl;
        cout << "  Palindrome Detection " << endl;
        cout << "************************" << endl << endl;
    }
    Well you created header();, so why not create some more functions.
    Then your main loop might look like this
    Code:
        while (input[0] != '0') {
            palindrome = checkForPalindrome(input);
            if (palindrome == true) {
                cout << "\"" << input << "\"" << " is a palindrome." << endl;
            } else {
                cout << "\"" << input << "\"" << " is not a palindrome." << endl;
            }
            getline(cin, input);
        }
    Functions mean you can test things without having the rest of the code there
    Code:
    int main ( ) {
        bool result1 = checkForPalindrome("hello");
        bool result2 = checkForPalindrome("abba");
    }
    See, a simple main and your function, and that's it. Now how easy would that be to test?

    See the comment in the code?
    Well what you've done here is create a shadow for your input. Your actual input is now invisible to the rest of the code in this block, and the shadow input is just some uninitialised variable which you then try and do if (input[front] == input[back])

    > Apparently, the getline command has some bugs in the actual library file
    So, if your compiler is VC++ 6, then do as it says.
    Actually, getting a better C++ compiler would be a better idea - VC++6 sucks at being a C++ compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange windows STL crashing problem
    By kdmiller in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2010, 02:25 PM
  2. std::map::find() crashing
    By noobcpp in forum C++ Programming
    Replies: 13
    Last Post: 12-28-2008, 11:12 PM
  3. Program crashing, Need help!
    By Obsidian_179 in forum C Programming
    Replies: 3
    Last Post: 06-19-2008, 05:26 PM
  4. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  5. Replies: 5
    Last Post: 06-03-2005, 10:41 PM