Running Program again

I am trying to write a loop that encrypts and decrypts a string and will cycle again when you tell it to. The problem is that when there is a space in the input string it does not decrypt the rest of the code and when you input to run or stop the program again it becomes an infinite loop. How do I fix this.

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
  #include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
	
	int sel; // describes selection type for encryption or decryption
	bool r; // describes option to continue or exit

	string myStr;
	char myChar;
	int myCharASCII;

	do
	{
		cout << "Welcome to ENGR1200U Cryptographic Techniques Program." << endl;
	cout << "\nPlease select if you would like to encrypt or decrypt. \nEnter 1 for encryption and 2 for decryption: ";
	cin >> sel;

	if (sel == 1) // starts encryption
	{
		cout << "Please enter the string you would like to encrypt: ";
		cin >> myStr;
		cout << "Original message: " << myStr << endl;
		cout << "\nEncrypted message:\n" << endl;

		for (int i=0; i < myStr.length(); i++)
		{
			myChar = myStr.at(i);
			myCharASCII = (int)myChar;
			myCharASCII += 5;
			myChar = (int)myCharASCII;
			cout << myChar << endl;
		}
		cout << "\n" << endl;
	}

	if (sel == 2) // starts decryption
	{
		cout << "Please enter the sring you would like to decrypt: ";
		cin >> myStr;
		cout << "\nOriginal encrypted message: " << myStr << endl;
		cout << "\nDecrypted message:\n" << endl;

		for (int i=0; i < myStr.length(); i++)
		{
			myChar = myStr.at(i);
			myCharASCII = (int)myChar;
			myCharASCII -= 5;
			myChar = (int)myCharASCII;
			cout << myChar << endl;
		}
		cout << "\n" << endl;
	}
	
	cout << "Would you like to continue? \nEnter 'y' for yes and 'n' for no: ";
	cin >> r;

	if (r=='y')
	{
		r=true;
	}

	if (r=='n')
	{
		r=false;
	}
	
	

	}while (r);
		
	

	system("pause");
	return 0;
}
Use std::getline to read the whole line of input when you first ask for the string.
Yes this works, but I still have the problem with the program looping infinitely regardless if the user inputs y or n.
On lines 59, 61, and 66 you seem to be treading r like a character, but on lines 10, 63, 68, and 73 you're treating it like a boolean. Make up your mind or use an additional variable.
Topic archived. No new replies allowed.