write a C++ program given below?

Your task is to write a rot13.cpp program. The program will first prompt the user to enter the name of an input file. (inputMessage.txt as attached in email)Next it prompts the user to enter the name of the output file. It then reads the input file character-by-character, rotates it(shifts each alphabetic character in the message by thirteen places to the right, but leaves all other characters unchanged. By "thirteen places to the right" I mean that each 'a' is made into an 'n', each 'b' is transformed to a 'o', etc. Characters greater than 'm' are "rotated" or wrapped around to the beginning, so 'n' is transformed to 'a', 'o' is changed into 'b', etc. The same transformation is applied to characters between 'A' and 'Z', that is, each 'A' is made into an 'N', each 'C' is transformed to a 'P', etc. Characters greater than 'M' are wrapped around to the beginning, so 'N' is transformed to 'A', 'P' is changed into 'C', etc. ) and outputs the rotated message to the output file. The rotated message should also be printed to the console.

The code I made works for first 2 line only. How can i make the code work for the entire document? The code and the input file is given below.

CODE:

#include<iostream>

#include<fstream>

#include<string>

#include<cstring>

using namespace std;

int main()

{

char strin[20],strout[20];

ifstream a;

cout<<"Enter input file name"<<endl;

cin>>strin;

a.open(strin);

if(a.fail())

{

cout<<"Error input"<<endl;

return 0;

}

ofstream b;

cout<<"Enter output filename"<<endl;

cin>>strout;

b.open(strout);

if(b.fail())

{

cout<<"Error output"<<endl;

return 0;

}

char q[26],r[26];

char ch,y,x;

int s,e,g,j;

while(!a.eof())

{

for(int i=0;i<26;i++)

{a.get(ch);

q[i]=ch;

}

a.get(y);

a.get(x);

for(int j=0;j<26;j++)

{a.get(ch);

r[j]=ch;

}

for(int k=0;k<26;k++)

{

s=q[k];

s=s+13;

if(s>122)

{e=s-122;

s=e+96;

}

q[k]=s;

b<<q[k];

cout<<q[k];

}

cout<<endl;

b<<endl;

for(int l=0;l<26;l++)

{g=r[l];

g=g+13;

if(g>90)

{j=g-90;

g=j+64;

}

r[l]=g;

b<<r[l];

cout<<r[l];

}

}

return 0;

}

Input File:

abcdefghijklmnopqrstuvwxyz

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Four score and seven years ago our fathers brought forth on this continent,

a new nation, conceived in Liberty, and dedicated to the proposition that

all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any

nation so conceived and so dedicated, can long endure. We are met on a great

battle-field of that war. We have come to dedicate a portion of that field,

as a final resting place for those who here gave their lives that that nation

might live. It is altogether fitting and proper that we should do this.

But, in a larger sense, we can not dedicate -- we can not consecrate -- we

can not hallow -- this ground. The brave men, living and dead, who struggled

here, have consecrated it, far above our poor power to add or detract. The

world will little note, nor long remember what we say here, but it can never

forget what they did here. It is for us the living, rather, to be dedicated

here to the unfinished work which they who fought here have thus far so nobly

advanced. It is rather for us to be here dedicated to the great task remaining

before us -- that from these honored dead we take increased devotion to that

cause for which they gave the last full measure of devotion -- that we here

highly resolve that these dead shall not have died in vain -- that this

nation, under God, shall have a new birth of freedom -- and that government

of the people, by the people, for the people, shall not perish from the earth.

Kindly re post the correct code. I shall make your answer the best answer. Thank you! :)

Comments

  • You should walk through your code, explain to yourself what's happening, then ask yourself if that's what you want to do. E.g., "read 26 characters from the input file". What if the file is smaller, or bigger? Don't you want to read the entire file? Anyway, your code needs some work. See below for one way to do it.

    #include <iostream>

    #include <vector>

    #include <fstream>

    #include <iterator>

    #include <cctype>

    using namespace std;

    char rot13AlphaEncode(char);

    int main(int argc, char *argv[]) {

        string inFilename, outFilename;

        vector<char> plaintext, ciphertext;

        cout << "Enter input filename: ";

        getline(cin, inFilename);

        ifstream inFile(inFilename.c_str());

        if (inFile) {

            cout << "Enter output filename: ";

            getline(cin, outFilename);

            ofstream outFile(outFilename.c_str());

            // Get plaintext from input file

            inFile >> noskipws;

            copy(istream_iterator<char>(inFile), istream_iterator<char>(),

                      back_inserter(plaintext));

            // Encode and write to output file

            ciphertext.resize(plaintext.size());

            transform(plaintext.begin(), plaintext.end(), ciphertext.begin(),

                                rot13AlphaEncode);

            copy(ciphertext.begin(), ciphertext.end(), ostream_iterator<char>(outFile, ""));

            inFile.close();

            outFile.close();

        } else {

            cout << inFilename << " not found." << endl;

            return -1;

        }

        return 0;

    }

    char rot13AlphaEncode(char c) {

        int c1 = c;

        if (isalpha(c)) {

            char last = isupper(c) ? 'Z' : 'z';

            if ((c1 += 13) > last) c1 -= 26;

        }

        return static_cast<char>(c1);

    }

Sign In or Register to comment.