Loop

I've been trying to put a loop inside this code but whenever I try to there seems to be an error. Whenever a whitespace is used it produces an Infinite loop of error. I tried Do while and Nested if loops but still nothing. Can Anyone help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main ()
{
		
		string word;
		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;
			}
		}
}
Last edited on
Use getline() ? ( in place of cin )

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <iostream>

int main ()
{
    std::string word;
    
    std::getline ( cin, word );
    
    std::cout << word << std::endl;
    
    return 0;
}
Last edited on
I tried using getline but now my problem is how do I use it in a loop?
Is this what you mean:

something like:
1
2
3
4
5
6
7
8
9
10
11
int main ()
{
    string word;    
    
    do {
        cin >> word;

        /** code **/


    } while ( word != "quit" );
Topic archived. No new replies allowed.