Code stuck in infinite loop

Hello! I'm having an issue where I'm designing a palindrome checker. I have no problem with single words, but every time when I type in a phrase that has multiple words with spaces, my program loses its mind and goes into an infinite loop. Perhaps it has something to do with the loops I put inside? I've been step by step debugging with no success. Any help would be appreciated.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  //Preprocessor directives
#include <iostream>
#include <cstdlib>
#include <string>
#include <iterator>
#include <vector>
#include <stdlib.h>

using namespace std;

//Function declarations
string input();
bool check(string a);

int main()
{
	//Repeater variable
	bool rep = 1;
	bool result = 0;

	//Declares string to be checked
	string palin;
	while (rep == 1)
	{
		//Creates string and calls input function
		palin = input();

		//Close function if stopped midway
		if (palin == "2")
		return 0;

		result = check(palin);

		//Displays the results
		if (result == 1)
			cout << palin << " is a palindrome." << endl;
		else
			cout << palin << " is not a palindrome." << endl;

		//Ask if the user wants to enter another Palindrome
		cout << "Continue? (1 for yes, 0 for no): ";
		cin >> rep;
	}

	cout << "Closing program..." << endl;
	system("pause");
	return 0;
}

string input()
{
	//Asks for and receives input string
	string temp;
	cout << "Please enter a string (type zero or 0 to quit): ";
	cin >> temp;

	//Close program if user enters 0 or zero
	if (temp == "0" || temp == "zero")
	{
		cout << "Exiting program..." << endl;
		system("pause");
		return "2";
	}

	//Check if string is null, then ask for input again
	if (temp.empty())
	{
		cout << "There is nothing entered. Please enter a string: ";
		cin >> temp;
	}

	//Check if string is too long, ask for input again
	if (temp.length() >= 80)
	{
		while (temp.length() > 80)
		{
			cout << "The string is too long. Please enter a smaller string: ";
			cin >> temp;
		}
	}
	return temp;
}

bool check(string a)
{
	//Creates 2 iterators that traverse the string
	string::iterator test1;
	string::reverse_iterator test2;

	test1 = a.begin();
	test2 = a.rbegin();

	//Continue until the end of either side of the string
	while (test1 != a.end() && test2 != a.rend())
	{
		//Check if the current symbol is alphanumeric
		while (test2 != a.rend() && !isalnum(*test2))
			++test2;
		while (test1 != a.end() && !isalnum(*test1))
			++test1;
		//Break loop when you hit the end
		if (test1 == a.end() || test2 == a.rend())
			break;
		//If they're not the same it's not a palindrome, exit function
		if (tolower(*test1) != tolower(*test2))
			return 0;

		++test1;
		++test2;
	}
	return 1;
}
Hi,

std::cin only reads up to the first white space, that is one word. Consider using std::getline instead.

I've been step by step debugging with no success.


You should have seen that temp in the function input always contains one word.
Topic archived. No new replies allowed.