I'm working on a program and it keeps crashing for an unknown reason, please help!

I am working on a program that is supposed to completely compress (basically eliminating punctuation and spaces and changing all letters to lower case) reverse a word or sentence that the user inputs and to determine if the reversed word is a palindrome. Additionally, the program should display the word/sentence in both compressed form and reversed form. Lastly, the program is supposed to ask the user if they would like to input another word, repeating if the answer is 'Y' or 'y' and closing the program if the answer is 'N' or 'n'.

I am currently able to reverse and display correctly individual words, but every time that I input a sentence the program closes.
Can anyone tell me what's going wrong?

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	//Declaring variable for initial Loop
	bool keepgoing;

	//Initial Loop
	do
	{
		//Declaration of Variables
		string sentence, compressed = "", reversed = "";
		char letter, choice;
		int i, howmany;

		//Gathering input from User
		cout << "Enter a sentence to check: \n";
		cin >> sentence;
		
		//Setting up the Reversing Loop
		howmany = sentence.length();
		i = 0;
		while (i < howmany)
		{
			letter = sentence[i];
			//Cutting out punctuation and Reversing the string
			if (isalpha(letter))
			{
				letter = tolower(sentence[i]);
				compressed = compressed + letter;
				reversed = letter + reversed;
			}
			else if (isspace(letter) || ispunct(letter))
			{
				compressed = compressed;
				reversed = reversed;
			}
			i++;
		}
		//Putting out the compressed and reversed word
		cout << "compressed: " << compressed << endl;
		cout << "reversed: " << reversed << endl;
		//Determining Palindrome
		if (compressed == reversed)
			cout << "= = = >Palindrome!!!\n";
		else
			cout << "= = = > not palindrome.\n";
                cout << "---------------------------------\n";

		//Asking User Input for Repeat
		cout << "Do you wish to enter a new word/sentence? [Y or N] ";
		cin >> choice;

		if (choice == 'Y' || choice == 'y')
			keepgoing = true;
		else 
			keepgoing = false;

	} while (keepgoing != false);

	return 0;
}
Last edited on
The following code will only take input until the next space character.

1
2
3
//Gathering input from User
cout << "Enter a sentence to check: \n";
cin >> sentence;


I would try using getline instead it will allow you to get data up until the end line character or one you specified.

http://www.cplusplus.com/reference/string/string/getline/
Topic archived. No new replies allowed.