Question

In: Computer Science

hi everyone.i have to perform rot13 and program should also print missing command if there is...

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;
}

Solutions

Expert Solution

//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;
}


Related Solutions

Write a perl program that replicates the actions of a pez dispenser. The program should print...
Write a perl program that replicates the actions of a pez dispenser. The program should print out the contents of the dispenser showing that it is empty, prompt a user for 10 pez candy flavors/colors, print out the contents of the dispenser showing that it is full, dispense the candies one at a time, and then print out the contents of the dispenser showing that it is empty. The dispenser should only take in 10 candies and should load and...
1a .Write a program that perform insertion sort (ascending order) on an input array and print...
1a .Write a program that perform insertion sort (ascending order) on an input array and print the total number of comparisons that have been made. You can implement by using any programming languages such as C/C++. For example, in the case of array B = [ 30 , 10 , 20 , 40 ], there are 4 needed comparisons to perform insertion sort (30 vs 10, 20 vs 30, 20 vs 10, and 40 vs 30). 1b. Write a program...
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
USING a LOOP for C++ In this lab the completed program should print the numbers 0...
USING a LOOP for C++ In this lab the completed program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. You should accomplish this using a for loop instead of a counter-controlled while loop. Instructions Write a for loop that uses the loop control variable to take on the values 0 through 10. In the body of the loop, multiply the value of the loop control variable by 2 and by 10....
Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this...
Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this radius, the circumference of the circle, the area of the circle, the surface area of a sphere with that radius and the area of a sphere with that radius. Each of its computation functions returns its answers in call by reference parameters. Here are the formulas for computation: Circumference = 2 • π • r Circle Area = π • r2 Sphere Surface Area...
The program should be able to do the following: In Java accepts one command line parameter....
The program should be able to do the following: In Java accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated by a single whitespace. reads the integers from the text file in part a into an array of integers. sort the integers...
**Need to use awk command in putty (should be a ONE LINE COMMAND) Write the command...
**Need to use awk command in putty (should be a ONE LINE COMMAND) Write the command that would find all lines that have an email address and place a label email = before the line in the file longfile output will multiple lines similar to this one : using a good awk command the output would be something like this email = From: "Linder, Jann/WDC" <[email protected]> email = To: Mr Arlington Hewes <[email protected]> email = > From: Mr Arlington Hewes...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
c++ Write a program that print stars, Max and Min values. It should use the following...
c++ Write a program that print stars, Max and Min values. It should use the following functions: (2 pts) int getNum ( ) should ask the user for a number and return the number. This function should be called by main once for each number to be entered. Input Validation: Do not accept numbers less than -100. (2 pts) void printStars ( int n ) should print n number of stars. If n is less than 0, display "Invalid" message...
Summary In this lab the completed program should print the numbers 0 through 10, along with...
Summary In this lab the completed program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. You should accomplish this using a for loop instead of a counter-controlled while loop. Instructions Write a for loop that uses the loop control variable to take on the values 0 through 10. In the body of the loop, multiply the value of the loop control variable by 2 and by 10. Execute the program. Is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT