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
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.
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.
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.
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:
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.