Palindrome Loop Error

My loop keeps getting an error and I don't know why?
I'm still new to C++ so please help...I'm just a college freshman student...
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
  #include<iostream>
#include<string>
#include<algorithm>
using namespace 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);
}
It keeps printing the same thing over and over.
closed account (o3hC5Di1)
Hi there,

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.

All the best,
NwN
Last edited on

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?
Topic archived. No new replies allowed.