In: Computer Science
hi everyone.i have to perform rot13 and program should also print missing command if there is no command or arguments and bad argument.it should also print file can not open if there if no file.i am very confuse,what should i do.here is my coding,please tell me if i am missing something.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
char Rot13(char to_be_translated)
{
if(to_be_translated>='A' &&
to_be_translated<='Z')
{
return (to_be_translated-'A'+13)%26+'A';
}
else if(to_be_translated>='a' &&
to_be_translated<='z')
{
return (to_be_translated-'a'+13)%26+'a';
}
return to_be_translated;
}
void WriteTranslatedCharacter(char translated_character,
ofstream& output)
{
output << translated_character;
}
int main(int argc,char*argv[])
{
string input_file;
cout <<"Enter the name of input file:";
cin >> input_file;
cout << endl;
ifstream infile(input_file.c_str());
ofstream outfile("output.rot13");
if(!infile)
{
cout <<"Unable to open file" << endl;
return 0;
}
char char_from_file = infile.get();;
while(!infile.eof())
{
WriteTranslatedCharacter(Rot13(char_from_file), outfile);
char_from_file = infile.get();
}
infile.close();
outfile.close();
return 0;
}
//Modified C++ program
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
char Rot13(char to_be_translated)
{
if(to_be_translated>='A' &&
to_be_translated<='Z')
{
return (to_be_translated-'A'+13)%26+'A';
}
else if(to_be_translated>='a' &&
to_be_translated<='z')
{
return (to_be_translated-'a'+13)%26+'a';
}
return to_be_translated;
}
void WriteTranslatedCharacter(char translated_character,
ofstream& output)
{
translated_character = Rot13(translated_character);
output << translated_character;
}
int main(int argc,char*argv[])
{
if(argc==1)
cout<<"No Extra Command Line Argument Passed Other Than
Program Name\n";
string input_file;
cout <<"Enter the name of input file:";
cin >> input_file;
cout << endl;
ifstream infile(input_file.c_str());
ofstream outfile("output.txt");
if(!infile)
{
cout <<"Unable to open input file" << endl;
return 0;
}
if(!outfile)
{
cout <<"Unable to open output file" << endl;
return 0;
}
char char_from_file = infile.get();
while(!infile.eof())
{
WriteTranslatedCharacter(char_from_file, outfile);
char_from_file = infile.get();
}
infile.close();
outfile.close();
return 0;
}