During menu selection prompt, I select case 2, to convert all contents to upper case.
Contents of text file:
Quote
1 The quick brown fox jumps over the lazy dog
2 The quick brown fox jumps over the lazy dog
3 The quick brown fox jumps over the lazy dog
4 The quick brown fox jumps over the lazy dog
5 The quick brown fox jumps over the lazy dog
1 The quick brown fox jumps over the lazy dog
2 The quick brown fox jumps over the lazy dog
3 The quick brown fox jumps over the lazy dog
4 The quick brown fox jumps over the lazy dog
10th The quick brown fox jumps over the lazy dog
2 The quick brown fox jumps over the lazy dog
3 The quick brown fox jumps over the lazy dog
4 The quick brown fox jumps over the lazy dog
5 The quick brown fox jumps over the lazy dog
1 The quick brown fox jumps over the lazy dog
2 The quick brown fox jumps over the lazy dog
3 The quick brown fox jumps over the lazy dog
4 The quick brown fox jumps over the lazy dog
10th The quick brown fox jumps over the lazy dog
main program
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class FileFilter
{
//private:
//since no input is being accepted directly from the user during runtime.
public:
virtual char transform(char ch) = 0;
void doFilter(fstream &in, fstream &out)
{
vector<string> lineVector;
int iVec = 0; //for error diagnosing
//while(!in.eof())
//{
string line1;
while(getline(in, line1))
{
for(int i = 0; i < line1.length(); i++)
{
line1[i] = transform(line1[i]); // dynamic binding
cout << "-> " << line1[i] << endl; // doesn't display
}
lineVector.push_back(line1);
iVec++;
}
for (int i = 0; i < lineVector.size(); i++)
out << line1;
cout << "\n size of iVec: " << iVec << endl;
}
};
//////////////////////////////////////////////////////
class ToUpper : public FileFilter
{
public:
char transform (char ch)
{
return toupper(ch);
}
};
//////////////////////////////////////////////////////
class Encryption : public FileFilter
{
private:
int keyNum;
public:
char transform(char ch)
{
return ch + keyNum;
}
//constructor
Encryption (int encKey)
{
keyNum = encKey;
}
};
//////////////////////////////////////////////////////
class Unchanged : public FileFilter
{
char transform(char ch)
{
return ch;
}
};
//////////////////////////////////////////////////////
void menu();
string getInFname();
void currentContents(fstream&);
int main()
{
fstream inFile, outFile;
fstream toUpper, encrypt, copy;
string inF, outF; //fileName of files
string line;
int offset;
int choice;
inF = getInFname();
inFile.open(inF, ios::in);
if (inFile.fail())
{
cout << "\nFile couldn't open... shutting down.\n";
system("pause");
return 0;
}
cout << "\nCurrent file's contents :\n";
currentContents(inFile);
cout << endl;
//output file
//outFile.open("daOut.txt", ios::out);
ToUpper tuObj1; //Must be declared outside switch block.
do
{
menu();
cout << "Your selection: ";
cin >> choice;
switch(choice)
{
case 1:
int enc;
cout << "Enter an encryption number: ";
cin >> enc;
//Encryption eObj1(enc);
break;
case 2:
// to all upper case
//inFile.open(inF, ios::in); no difference
toUpper.open("upper.txt", ios::out);
tuObj1.doFilter(inFile, toUpper);
cout << "\nUpper operation completed...\n";
toUpper.close();
break;
case 3:
break;
}
} while (choice != 4);
//while (!outFile)
//
// cout << "File opening error, please re-enter name: ";
// cin >> outFileName;
//}
//ToUpper file1;
//file1.doFilter(inFile, outFile);
inFile.close();
//outFile.close();
system("pause");
return 0;
}
string getInFname()
{
string inF;
cout << "Enter the input filename: ";
cin >> inF; //da
return inF += ".txt";
}
void menu()
{
cout << "\nSelection one of the following options.";
cout << "\n1 to perform encryption.";
cout << "\n2 transform to all uppercase.";
cout << "\n3 to display unchanged copy of the file.";
cout << "\n4 to quit program.";
}
void currentContents(fstream& file)
{
string line;
//while (!file.eof())
//{
while(getline(file,line))
{
cout << line << endl;
}
//}
}

New Topic/Question
Reply



MultiQuote



|