#include<iostream>
#include<string>
#include<algorithm>
usingnamespace std;
int main ()
{
int loop;
string word;
cout<<"Enter 1 if you want to ask if a sentence or word is palindrome and 2 if you want to end the program: ";
cin>>loop;
do
{
cout<<"Enter the word or sentence: ";
cin>>word;
remove(word.begin(), word.end(), ' ');
if ( word == string(word.rbegin(), word.rend()))
{
cout<<"It is a Palindrome"<<endl;
cout<<endl;
}
else
{
cout<<"It is not a Palindrome"<<endl;
cout<<endl;
}
}
while (loop == 1);
}
Welcome to the forums. Could you please be a little bit more specific? Do you get compilation errors? Does the program crash? Which error messages do you get?
As you only ask once for the value of loop and don't alter it afterwards, it's quite likely that your loop will run infinitely. To solve this, move the question asking the user to enter 1 or 2 inside the do/while loop.
int loop;
string word;
do
{
cout<<"Enter 1 if you want to ask if a sentence or word is palindrome and 2 if you want to end the program: ";
cin>>loop;
cout<<"Enter the word or sentence: ";
cin>>word;
remove(word.begin(), word.end(), ' ');
if ( word == string(word.rbegin(), word.rend()))
{
cout<<"It is a Palindrome"<<endl;
cout<<endl;
}
else
{
cout<<"It is not a Palindrome"<<endl;
cout<<endl;
}
}
while (loop == 1);
}
I tried putting it inside the loop and it loops infinitely...What am I still doing wrong? Shouldn't the program ask the questions again?