Sentinel Loop

I want my loop program to quit after END is entered. However, my program ignores my condition and keeps looping. Can anyone point out what I need to fix?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
while (x != "END")
    {
    getline(cin, x);
    p = x;
    xx=x;
    x = Pal(x); 
    x = Reverse(x);
    x = Filter(x);
    p = Pal(p);
    p = Filter(p);
 
    if (  p == x  ) 
    {
        cout << xx << " is a palindrome." << endl;
    }
    
    else 
    {
        cout << xx<< " is NOT a palindrome." << endl;
    }
    }
is x a char or a string?

post the full code including all the helper functions
Last edited on
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
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <locale.h>
#include <limits.h>
#include <string>
#include <algorithm>
#include <bits/stdc++.h> 
using namespace std;


string Pal(string sent)
{
	for (int i = 0; sent[i]; i++)
		sent[i] = tolower(sent[i]);
	
	return sent;
}


string Reverse (string rev)
{ 
    string str = rev; 
    reverse(str.begin(), str.end());    
    return str; 
} 


string Filter (string clean)
{
    
  
    for (int i = 0, len = clean.size(); i < len; i++) 
    { 
        // check whether parsing character is punctuation or not 
        if (ispunct(clean[i])) 
        { 
            clean.erase(i--, 1); 
            len = clean.size(); 
        } 
    } 
     
    clean.erase(remove(clean.begin(), clean.end(), ' '), clean.end());

    return clean;
    
}

int main ()
{   
    
    string x;
    string p;
    string xx;
    
    while (x != "END")
    {
    getline(cin, x);
    p = x;
    xx=x;
    x = Pal(x); 
    x = Reverse(x);
    x = Filter(x);
    p = Pal(p);
    p = Filter(p);
 
    if (  p == x  ) 
    {
        cout << xx << " is a palindrome." << endl;
    }
    
    else 
    {
        cout << xx<< " is NOT a palindrome." << endl;
    }
    }
}
Thanks Andy,

my educated guess from reading the below code,

http://www.cplusplus.com/forum/beginner/244557/

is that when you type "END" you are then reversing "END" which becomes "DNE" so that's probably why the condition is not being met.

you then set X = to filter,

this probably isn't a very efficient solution but you could create another variable and set it equal to x WHICH I see you have done,

so check for xx != "END" not x!= "END"
Last edited on
Ohhhhhhhhhhhhhhh

Okay, I can figure it out from there I think.

Thank you.
so below,the code will do what you want it to do,(sort of)

the reason why I say sort of is because although the program will end when you enter "END" it will still print END is not a palindrome you could instead create an infinite while loop and break from the loop if xx is equal to "END"

also there is no need to include a lot of them headers in the program for trivial programs such as this they won't do much harm but having so many includes makes the executable larger,don't include them if you don't need them.

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

int main ()
{

    string x;
    string p;
    string xx;

    while (xx != "END")
    {
    getline(cin, x);
    p = x;
    xx=x;
    x = Pal(x);
    x = Reverse(x);
    x = Filter(x);
    p = Pal(p);
    p = Filter(p);

    if (  p == x  )
    {
        cout << xx << " is a palindrome." << endl;
    }

    else
    {
        cout << xx<< " is NOT a palindrome." << endl;
    }
    }

}
Last edited on
You could try something like: while (getline(cin, x) && x != "END") for your loop control statement. And remember "end" is not the same as "END" or "End", etc.
Topic archived. No new replies allowed.