Working on a school assignment, essentialy, write a program that will receive a word (all lowercase, no spaces, terminated by a period), and determine if it is a palindrome. The program compiles fine, with no errors or warnings, but when run it crashes. I have compiled it with Microsoft Visual C++ 6.0 and Microsoft Visual C++ 2010 express and experienced the same problem.
EDIT: The exact error message that I got on my home computer is "Expression: string subscript out of range"
#include <iostream>
#include <string>
using namespace std;
void checkpal(int, string);
int main()
{ char ans;
do
{
string word1, worddelete;
int length, i;
cout << "This program checks palindromes\n.Enter your word, terminated by a period: ";
cin >> word1;
length = word1.length();
for(i = 0; i <= length - 1; i++)
{
worddelete[i] = word1[i];
}
length = length - 1;
cout << endl << "Your word is " << length << " characters long.\n";
checkpal(length, worddelete);
cout << "Would you like to check another word? 'y' for yes 'n' for no: ";
cin >> ans;
}
while(ans == 'y' || ans == 'Y');
return 0;
}
It is because you don't initialize them (thus making them empty strings), then try to access their elements with [], iterating over length, which is not their actual length (it's 0, because the are "").
In order to fill the strings fronthalf and backhalf. But when i try it that way, it STILL wont run. Am I missing something else?
EDIT: I get the exact same error as previously.
EDIT2: Actualy, the prgram RUNS either way, but it crashes after I type in a word and hit enter.
When you declare worddelete or fronthalf or backhalf you don't assign a specific length to it so it is treated as null string (it has no space for a word to be assign to it)
and as a result you can't access individually each character of it because it simply doesn't have
For example:
So, what I think I need is some way of determining how long the word is. Probably by searching the string for '.'? In that case should I declare the strings as some arbitrarily high number? That seems like a crude way to go though.
If they are std::strings, just use .size() to get the size. Also, you ought to use getline() to get data into the string instead of just cin>>, which will stop at the first space.
What is the difference between using .size() and .length()? But I see what you mean with the
getline(), I suspect that was the problem. (Though this computer does not have a compiler, so I can't check.)
And that should just be
1 2
cout << "This program checks palindromes\n.Enter your word, terminated by a period: ";
getline (cin,word1);
#include <iostream>
#include <string>
usingnamespace std;
void checkpal(int length, char worddelete[]);
int main()
{ char ans;
do
{
string word1;
char worddelete[100];
int length, i;
cout << "This program checks palindromes\n.Enter your word, terminated by a period: ";
getline(cin, word1);
length = word1.length();
cout << endl << "Your word is " << (length - 1) << " characters long.\n";
length = length - 1;
for(i = 0; i < length; i++)
{
worddelete[i] = word1[i];
}
checkpal(length, worddelete);
cout << "Would you like to check another word? 'y' for yes 'n' for no: ";
cin >> ans;
}
while(ans == 'y' || ans == 'Y');
return 0;
}
void checkpal(int length, char worddelete[])
{
int i;
string fronthalf2, backhalf2;
char fronthalf[100], backhalf[100];
for(i = 0; i < length / 2; i++)
{
fronthalf[i] = worddelete[i];
backhalf[i] = worddelete[length-i];
}
if(fronthalf2.compare(backhalf2) != 0)
cout << "This word is not a palindrome!";
else
cout << "This word is a palindrome!";
}
At the moment this is what I have.
Problem 1: Trying to figure out how to convert from type array to type string. Should figure this out on my own shortly(assuming it is possible).
Problem 2: Past the second loop it reports length as = -1. I have no idea why.
Problem 3: The program seems to have about a 50/50 chance of crashing instantly when typing in 'y' to initialize 'ans'. Not when you press enter, simply when you TYPE it. The window will just up and disappear.