Help Please

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;

bool isPalin(string& palin);
string cleanUp(string& palin);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 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])
			return false;

	return true;
}

// 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;
}
Last edited on
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



Also, the insert key fixed my problem. Thanks
Here's the updated code:
Program runs and worked as inteded.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string>
#include <cstdio> //for getchar()

using namespace std;

bool isPalin(string& palin);
string cleanUp(string& palin);
int main()
{
    string palin;
    // get input
    cout << "Please enter a possible palindrome: " << endl;
    getline(cin, palin);

    // run isPalin

    // output result
    if (isPalin(palin)) //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) //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])
            return false;

    return true;
}

// 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[i] == ' ')
        {
            palin.erase(i,1);
            i--;
        }
    length = palin.length();
    for (int i = 0; i < length; i++)
        palin[i] = tolower(palin[i]);
    return palin;
}

Last edited on
Inputting punctuation or whitespace results in an error message:

Debug Assertion Failed!
Program[directory]
Line:1441

Expression: string subscript out of range

A breakpoint message pops up after that one is closed.
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).
Last edited on
it seems to be working fine.

just compile using MinGW, the command is:

compile:
g++ -c -o main.o main.cpp

g++ -o main.exe main.o

execute:
main

InvaDev
http://www.invadev.com
Found one problem you should be able to fix easy.

If you type
!@#$%^ all the charcters are removed and you are left with a zero size string.
your program reports that it is a Palindrome.

I think this in the right place should fix that issue

if (palin.size() !=0 )

Topic archived. No new replies allowed.