So I've been tasked with creating a program that checks to see whether or not a string is a palindrome. It has to remove whitespace, punctuation, and capitalization for obvious reasons. Getting some errors which I'm not sure how to correct.
On an unrelated note, while programming in the console, my cursor has turned into a gray box and replaces characters when typing, instead of pushing them forward, etc. How do I return it to normal?
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
string palin;
// get input
cout << "Please enter a possible palindrome: " << endl;
getline(cin, palin);
// run isPalin
// output result
if (isPalin= true)
cout << "That was a palindrome!";
else
cout << "That was not a palindrome!";
getchar();
getchar();
return 0;
}
// create the function to test palindrome
bool isPalin (string& palin) {
cleanUp(palin);
int length = palin.length();
for (int i = 0; i < length/2; i++)
if (palin[i] != palin[length - 1 - i])
returnfalse;
returntrue;
}
// remove whitespace, punctuation, and capitalization
string cleanUp (string palin) {
int length = palin.length();
for (int i = 0; i < length; i++)
if (ispunct(palin[i]))
palin.erase[i];
for (int i = 0; i < length; i++)
palin[i] = tolower(palin[i]);
for (int i = 0; i < length; i++)
if (palin[i] == ' ')
palin.erase[i];
return palin;
}
Firstly use code tags when posting code: http://www.cplusplus.com/articles/jEywvCM9/
Also it'd help if you posted what the errors are that you're getting. In regards to the console you probably hit the 'insert' key on your keyboard, press it again and it should go back to normal.
My apologies, I'm new here. I've edited my original post to include code tag.
Please let me know if it isn't kosher to include my error list like this:
1>c:\users\matthew tighe\documents\visual studio 2010\projects\hw7\hw7\palindrome.cpp(23): warning C4551: function call missing argument list
1>c:\users\matthew tighe\documents\visual studio 2010\projects\hw7\hw7\palindrome.cpp(52): error C3867: 'std::basic_string<_Elem,_Traits,_Ax>::erase': function call missing argument list; use '&std::basic_string<_Elem,_Traits,_Ax>::erase' to create a pointer to member
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1>c:\users\matthew tighe\documents\visual studio 2010\projects\hw7\hw7\palindrome.cpp(52): error C2109: subscript requires array or pointer type
1>c:\users\matthew tighe\documents\visual studio 2010\projects\hw7\hw7\palindrome.cpp(59): error C3867: 'std::basic_string<_Elem,_Traits,_Ax>::erase': function call missing argument list; use '&std::basic_string<_Elem,_Traits,_Ax>::erase' to create a pointer to member
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1>c:\users\matthew tighe\documents\visual studio 2010\projects\hw7\hw7\palindrome.cpp(59): error C2109: subscript requires array or pointer type
Code updated,sorry for that.
The errors for your program were really self-explanatory (at least when i compiled it with gcc).
Next time try to do it yourself.
STL does make your life easy only when you are fluent with it,otherwise it is better to implement that funcionality yourself than to be unsure of the stl functions(here basic_string::erase() was the culprit).