Program to swap letters.

Hello,

I've written a program that asks a user to choose a letter to swap from a text file, and then what letter to swap it with, and then output the results into a different text file. The program works, but I was wondering how to program it so that this can be done over an over until the user choses to quit. As it it is, the program ends after each swap, and then has to be re-run to swap another letter.

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
//Program to allow user to choose a which letters to be swapped.

#include <iostream>
#include <fstream>


using namespace std;

int main()
{
	ifstream inputFile("swapped.txt");
	ofstream outputFile("decrypted.txt");


	char  x;
	char  y;
	char letter;

	
	
	cout << "Enter the letter that you want replaced" << endl;
	cin >> x;
	cout << "Enter the letter that you want to replace it with" << endl;
	cin >> y;



	if (inputFile.is_open())
		{
		while (inputFile.good())
		{
			inputFile.get(letter);
			if (letter == x)
			{
				outputFile << y;
			}
			else if (letter == y)
			{
				outputFile << x;
			}
			else
			{
				outputFile << letter;
			}
		}
	}
	else
	{
		cout << "Program did not open" << endl;
	}

	system("pause");
	return 0;
}


Any help would be appreciated.
Last edited on
Add a loop such as:

PS, I didn't run this to make sure it worked but should give you a idea.

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


using namespace std;

int main()
{
	ifstream inputFile("swapped.txt");
	ofstream outputFile("decrypted.txt");


	char  x;
	char  y;
	char letter;

	
	
	cout << "Enter the letter that you want replaced" << endl;
	cin >> x;
	cout << "Enter the letter that you want to replace it with" << endl;
	cin >> y;



	if (inputFile.is_open())
		{
		char RunAgain='y'; // New
		while (RunAgain=='y')// New
		{
			while (inputFile.good())
			{
				inputFile.get(letter);
				if (letter == x)
				{
					outputFile << y;
				}
				else if (letter == y)
				{
					outputFile << x;
				}
				else
				{
					outputFile << letter;
				}
			}
		cout << "Run Again y/n ? "; // New
		cin >> RunAgain; // New
		}
		}
		else
		{
			cout << "Program did not open" << endl;
		}
		
// 	system("pause");
	return 0;
}


*Edit, added a // New comment to highlight changes
Last edited on
I wanted to comment and point out that SamuelAdams code comments out your system("pause"); and adds in return 0;. The use of system("pause") is highly discouraged. Please do not use any system(). To answer any questions of why, please refer to this nicely made article by Duoas.

http://www.cplusplus.com/articles/j3wTURfi/
Thanks very much Sam Adams. I've run the program and and it works for 'n' i.e. it closes, but when 'y' is inputed it just outputs 'Run Again y/n ?'.

I'll play around with it to try and get it working. Thanks again though.
Topic archived. No new replies allowed.