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.
//Program to allow user to choose a which letters to be swapped.
#include <iostream>
#include <fstream>
usingnamespace 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;
}
elseif (letter == y)
{
outputFile << x;
}
else
{
outputFile << letter;
}
}
}
else
{
cout << "Program did not open" << endl;
}
system("pause");
return 0;
}
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.