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...
hi! I have this code. when I run it, it works but in the print(the number...
hi! I have this code. when I run it, it works but in the print(the number we are searching for is ) it prints the number twice. ex. for 5 it prints 55 etc. apart from this, it works #include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int n,i; printf("Give me the size of the table : \n"); scanf("%d", &n); int A[n],x,y; for (i=0;i<n;i++) A[i]=rand() % n; for (i=0;i<n;i++) printf("%d\n", A[i]); srand(time(NULL)); y=rand()% n ; printf("The number we are searching...
Write a program that uses nested for loops to print a multiplication table. Your program should...
Write a program that uses nested for loops to print a multiplication table. Your program should print out the multiplication table starting a 1 and going to 12. Output should be like: 1 x 1 = .1 x 2 = 2
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...
The Java program should be able to do the following: accepts one command line parameter. The...
The Java program should be able to do the following: 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 in...
**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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT