ROT13 Cipher C++ Need help intergrating two codes Reading Data from the .txt fil
ID: 3789224 • Letter: R
Question
ROT13 Cipher C++
Need help intergrating two codes Reading Data from the .txt file and converting it, displaying the results on screen
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
ifstream inFile; //Declares an file stream object
string fileName;
string name;
string str1;
string input;
int count = 0, length;
cout<<"Insert the name of the file you want to open: "<<endl;
cin>>fileName;
str1 = "/Users/nehaniphadkar/Desktop/";
str1 = str1 + fileName;
inFile.open(str1);
//Reading from that file
if (inFile.fail())
{
cout<<"Your file doesn't exist, Please enter a valid filename"<<endl;
}
else{
string s;
while(getline(inFile, s))
{
cout<<s<<endl;
}
}
}
__________________________________________________
#Code that runs ROT13
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
int count = 0, length;
cout << "Enter the phrase: ";
getline(cin,input);
length = (int)input.length();
for (count = 0; count < length; count++)
{
if (isalpha(input[count]))
{
input[count] = tolower(input[count]);
for (int i=0; i < 13; i++)
if(input[count] == 'z')
input[count] = 'a';
else
input[count]++;
}
}
cout<< " Results "<<input<<endl;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
ifstream inFile; //Declares an file stream object
string fileName;
string name;
string str1;
string input;
int count = 0, length;
cout<<"Insert the name of the file you want to open: "<<endl;
cin>>fileName;
str1 = "/Users/nehaniphadkar/Desktop/";
str1 = str1 + fileName;
inFile.open(str1);
//Reading from that file
if (inFile.fail())
{
cout<<"Your file doesn't exist, Please enter a valid filename"<<endl;
}
else{
string input;
while(getline(inFile, input))
{
int length = (int)input.length();
for (int count = 0; count < length; count++)
{
if(isalpha(input[count]))
{
input[count] = tolower(input[count]);
for (int i=0; i < 13; i++)
if(input[count] == 'z')
input[count] = 'a';
else
input[count]++;
}
}
cout<<input<<endl;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.