string palindrome

closed account (4wpL6Up4)
Hi,

I am having a problem with the following palindrome code,
only the first input seems to work fine, but when I repeat the calculation
without exiting the prompt, problems arise.
The message after "try again" automatically prints "is a palindrome" and no chance is given to input another string.
Any help/hint would be highly appreciated.
Thanks

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  #include "pch.h"


#include <stdlib.h>
#include<iostream>
#include<cstdlib>
#include<cctype>
#include <sstream>
#include <string>
#include<cmath>
#include <algorithm>
#include <iterator>
#include <ctype.h>
#include <iomanip>
#include <limits>


using namespace std;

 


int main()
{
	


	string input;
	char answer;
	do
	{


		
		
		cout << "Please enter a string: ";


		getline(cin, input);
			
		
		

input.erase(remove_if(input.begin(), input.end(), isspace), input.end());

transform(input.begin(), input.end(), input.begin(), ::tolower);

		
			if (input != string(input.rbegin(), input.rend()))
			{

				cout << input << "is not a palindrome" << endl;

				

			}



	else if (input == string(input.rbegin(), input.rend()))
			{

				
		cout << input << " is a palindrome" << endl;
					
			}	

			
			
				cout << "try again (Y/N)?" << endl;
				cin >> answer;
			} while (answer == 'Y' || answer == 'y');
		
				
		

			
		

		return 0;
	}
Thanks for using [code][/code] tags.
However your formatting is unreadable. Don't give so many newlines, in fact it wouldn't hurt to give absolutely no newlines.
Also make a habit of only using the headers you need.
1
2
3
#include <iostream>
#include <string>
#include <algorithm> 


Anyways,
input.erase(remove_if(input.begin(), input.end(), isspace), input.end());
should be
remove_if(input.begin(), input.end(), isspace);
remove_if takes care of the 'removing' part for you so you don't need to write erase.
closed account (4wpL6Up4)
What do you mean exactly by 'new lines'?
What makes my code unreadable?

Also, I have changed my erasing space code line to your suggestion
but I am still dealing with the same problem.
Do you perhaps know what is causing the program not to run smoothly?
Is also to be taken into consideration that if I remove


Char answer

do}

cout << "try again (Y/N)?" << endl;
cin >> answer;
} while (answer == 'Y' || answer == 'y');

The program gives me no issue at all, but I must be able to perform new trials without exiting the prompt.


this


                                      is


how


                                      your


code


looks


Your indentations are just everywhere. You must indent when using braces (or when using a construct)
https://en.wikipedia.org/wiki/Indentation_style

Ah so you're asking why you're not able to retry.
This has to do with the cin vs getline problem that every person has faced at least once in their life.

after cin>>answer, write cin.ignore();
1
2
		cin >> answer;
		cin.ignore();


Why you're facing this problem is because cin leaves a '\n' in the input stream (it leaves all trailing whitespaces in the stream), and getline reads this '\n' and stops input.

If you're expecting the user to type a space or some other whitespace other than '\n' then write something like:
cin.ignore(INT_MAX, '\n'); which will clear the stream until '\n' is found.

Most people tell to use: cin.ignore(numeric_limits<streamsize>::max(), '\n');
But you have to include <limits> for that.

Here's how your program should look when it's indented properly:
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
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
	string input;
	char answer;
	do {
		cout << "Please enter a string: ";
		getline(cin, input);

		remove_if(input.begin(), input.end(), isspace);
		transform(input.begin(), input.end(), input.begin(), ::tolower);

		if (input != string(input.rbegin(), input.rend()))
		{
			cout << input << "is not a palindrome" << endl;
		}

		else if (input == string(input.rbegin(), input.rend()))
		{
			cout << input << " is a palindrome" << endl;
		}

		cout << "try again (Y/N)?" << endl;
		cin >> answer;
		cin.ignore();

	} while (answer == 'Y' || answer == 'y');

	return 0;
}


I left some lines because I space that way, how you space your lines is up to you but never use more than 1 line.



Hi Grime,

Just was playing with your code, and found that it won't compile unless this line (15):

remove_if(input.begin(), input.end(), isspace);

is coded as:

remove_if(input.begin(), input.end(), ::isspace);

I was just curious, as I'd never seen remove_if before, so I got to playing around with it.
Last edited on
Ah I forgot #include <cctype> , sorry. I didn't notice as it compiled for me, probably the ctype got included from some other header in my compiler (VS2017).

I'm curious though, why did putting :: make it work? I used to think the third parameter is an identifier of a bool returning function (that doesn't change arguments).

And :: is the scope resolving operator, so what does this have to do with the identifier of the function?
Well, can't say I'm proficient enough to elaborate any, as I'm only kinda beginning with C++ myself; I just wondered why it wouldn't compile (on cpp.sh), and found a similar error message elsewhere, which suggested using the global namespace explicitly ... so that's what I did.
Last edited on
Hmmm. I thought it was some kind of glitch because I hadn't included ctype, but cpp.sh insists to use :: even still.

I didn't get that error on the compiler I use (VS2017).

The examples I found online didn't have ::, maybe somebody can elaborate what's happening and what's that syntax.
Topic archived. No new replies allowed.