Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Help! The following Program Works, but I am unsure of how to replace every l

ID: 3708558 • Letter: C

Question

C++

Help! The following Program Works, but I am unsure of how to replace every line break in a file with a single space. I need to add a LineBreak.h Thank You!

//FileFilter.H

//adding required header files

#include

#include

using namespace std;

#ifndef FileFilter_H

#define FileFilter_H

class FileFilter {

private:

int key;

public:

FileFilter(){}; //function should be defined ....

FileFilter(int key) {

this->key = key;

}

//transforming letter by encrypted key in tranform() method

char transform(char ch) const{

if (ch >= 'a' && ch <= 'z')

return ((ch- 'a') + key) % 26 + 'a';

else if (ch >='A' && ch <= 'Z')

return (((ch- 'A') + key) % 26 + 'A');//'A' is character

}

//function should be defined ....

virtual void doFilter(ifstream &in, ofstream &out)const
{

};

};

#endif

//Copy.h

//adding required header files

#include"FileFilter.h"


#include

#include

using namespace std;

#ifndef COPY_H

#define COPY_H

class Copy : public FileFilter

{

public:

//copying input file data into output file

void doFilter(ifstream &in, ofstream & out)const

{

char ch;

while (!in.eof()) {

in >> ch;

out << ch;

}

}

};

#endif

//Encryption.h

//adding required header files

#include"FileFilter.h"

#include

#include

#ifndef Encryption_H

#define Encryption_H

class Encryption : public FileFilter

{

public:

Encryption()
{

};

Encryption(int key) : FileFilter(key) {};

void doFilter(ifstream &in, ofstream &out)const

{

char ch;

while (!in.eof()) {

in >> ch;

if (isalpha(ch))

//calling transform() method to encrypt given file data

out << transform(ch);

else

out << ch;

}

}

};

#endif

//UpperCase.h

//adding required header files

#include

#include"FileFilter.h"

#include

#include

using namespace std;

#ifndef UpperCase_H

#define UpperCase_H

class UpperCase :public FileFilter

{

public:

//converting each character into uppercase by toupper() method

void doFilter(ifstream &in, ofstream &out) const

{

char ch;

while (!in.eof()) {

in >> ch;

if (isalpha(ch))

out << (char)toupper(ch);

else

out << (char)ch;

}

}

};

#endif

//Main.cpp

//adding required header files

#include"FileFilter.H"

#include"UpperCase.h"

#include"Encryption.h"

#include"Copy.h"


#include

#include

#include


using namespace std;

int main() {

string name, out1, out2, out3;

//reading input file name from user

cout << "Please enter file name: ";

cin >> name;

ofstream outFile1, outFile2, outFile3;

ifstream inFile;

inFile.open(name);

if (!inFile)

{

cout << "File cannot be oppened: ";

exit(0);

}

Encryption e;

UpperCase up;

Copy c;

do {

//menu displaying

cout << "1. Encription " << endl;

cout << "2. Transform to upper case "<< endl;

cout << "3. Copy to another file "<< endl;

cout << "4. Exit"<< endl;

int choice;

cin >> choice;

int key;

switch (choice) {

case 1:

cout << "Enter Encryption Key from 2-25: ";

cin >> key;

cout << "Please enter output file name: ";

cin >> out1;

outFile1.open(out1);

if (!outFile1) {

cout << "The file cannot be created";

exit(0);

}

e = Encryption(key);

e.doFilter(inFile, outFile1);

cout << "Encrypted Succesfully ";

outFile1.close();

break;

case 2:

//it shouldnot be open file again to avoid logical errors..beacuse input file already opened by inFile

//inFile.open(name);

cout << "Enter output file name: ";

cin >> out2;

outFile2.open(out2);

if (!outFile2) {

cout << "The file cannot be created ";

exit(0);

}

up.doFilter(inFile, outFile2);

cout << "File Transformed Succesfully ";

outFile2.close();

break;

case 3://should be gap between case and its value

//it shouldnot be open file again to avoid logical errors..beacuse input file already opened by inFile

//inFile.open(name);

cout << "enter output file Name: ";

cin >> out3;

outFile3.open(out3);

if (!outFile3) {

cout << "file cannot be created";

exit(0);

}

c.doFilter(inFile, outFile3);

cout << "File Copied Succesfully";

outFile3.close();

break;

case 4:

system("pause");

exit(0);//should include stdlib.h header file to exit() method usage

default:

cout << "Invalid Choice ";

}

system("pause");

} while (1);

inFile.close();//should be closed before exiting program
return 0;//return should be at the end of the main method

}

Explanation / Answer


Basically you want to replace a “LINE ” with “LINE ”. Intead of reading character by character, read file line by line and then replace entire line by contents and space. You need to do something similar to this:

string newstr = line;
while (std::getline(file, line))
{
newstr += line;
newstr += “ ”;
}

Now write newstr to the file, it has all the contents of the file. You can also write this newstr inside for loop into another file and then in the end copy that file to original file.