-
Crashing...
Hello,
I have created a basic palinedrome program that works fine when obtaining words using commands like , 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.
-
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?
-
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.
-
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.