I've been studying C++ (my first language) for about two months with books and Udemy courses. I would like some comments about my first real project that I put together for my teacher.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class Caesar {
private:
int userChoice;
string encrypt;
char temp;
public:
Caesar() {};
int caesarInterface(Caesar *obj)
{
system("CLS");
cout << "===================================================" << endl;
cout << "| Welcome to Caesar's cipher |" << endl;
cout << "===================================================" << endl;
cout << "| |" << endl;
cout << "| 1. Encrypt a .txt file. |" << endl;
cout << "| 2. Decrypt a .txt file. |" << endl;
cout << "| 3. Exit program. |" << endl;
cout << "| |" << endl;
cout << "| |" << endl;
cout << "| |" << endl;
cout << "| |" << endl;
cout << "| |" << endl;
cout << "| A software writen by Ricardo Martins Doberstein |" << endl;
cout << "===================================================" << endl;
try
{
cin >> userChoice;
if (obj->userChoice == 1) return obj->userChoice;
if (obj->userChoice == 2) return obj->userChoice;
if (obj->userChoice == 3) return obj->userChoice;
else throw 199;
}
catch (int a)
{
cout << "No such choice... error: 199" << endl;
cout << "Please try again." << endl;
obj->userChoice = 3;
system("pause");
return obj->userChoice;
}
}
void cesarEncrypt(Caesar *obj, string file)
{
string tempString;
vector<string> vec;
ifstream encrypt;
ofstream result;
result.open("encrypted.txt");
encrypt.open(file);
try
{
if (encrypt.is_open() && result.is_open())
{
while (!encrypt.eof())
{
getline(encrypt, tempString);
vec.push_back(tempString);
}
vector<string>::iterator it = vec.begin();
for (it; it != vec.end(); it++)
{
tempString = *it;
for (int i = 0; i < tempString.length(); i++)
{
if (tempString[i] == ' ') tempString[i] = ' ';
else if (tempString[i] == '!') tempString[i] = '!';
else if (tempString[i] == '.') tempString[i] = '.';
else if (tempString[i] == ',') tempString[i] = ',';
else if (tempString[i] == 'x') tempString[i] = 'a';
else if (tempString[i] == 'y') tempString[i] = 'b';
else if (tempString[i] == 'z') tempString[i] = 'c';
else if (tempString[i] == 'X') tempString[i] = 'A';
else if (tempString[i] == 'Y') tempString[i] = 'B';
else if (tempString[i] == 'Z') tempString[i] = 'C';
else tempString[i] += 3;
}
result << tempString + '\n';
}
encrypt.close();
result.close();
cout << "A file (encrypted.txt) has been saved in this program's directory successfully" << endl;
}
else throw 299;
}
catch (int error)
{
cout << "Couldn't open files. Error: 299" << endl;
}
}
void caesarDecrypt(Caesar *obj, string file)
{
string tempString;
vector<string> vec;
ifstream decrypt;
ofstream result;
result.open("decrypted.txt");
decrypt.open(file);
try
{
if (decrypt.is_open() && result.is_open())
{
while (!decrypt.eof())
{
getline(decrypt, tempString);
vec.push_back(tempString);
}
vector<string>::iterator it = vec.begin();
for (it; it != vec.end(); it++)
{
tempString = *it;
for (int i = 0; i < tempString.length(); i++)
{
if (tempString[i] == ' ') tempString[i] = ' ';
else if (tempString[i] == '!') tempString[i] = '!';
else if (tempString[i] == '.') tempString[i] = '.';
else if (tempString[i] == ',') tempString[i] = ',';
else if (tempString[i] == 'a') tempString[i] = 'x';
else if (tempString[i] == 'b') tempString[i] = 'y';
else if (tempString[i] == 'c') tempString[i] = 'z';
else if (tempString[i] == 'A') tempString[i] = 'X';
else if (tempString[i] == 'B') tempString[i] = 'Y';
else if (tempString[i] == 'C') tempString[i] = 'Z';
else tempString[i] -= 3;
}
result << tempString + '\n';
}
decrypt.close();
result.close();
cout << "A file (decrypted.txt) has been saved in this program's directory successfully" << endl;
}
else throw 399;
}
catch (int error)
{
cout << "Couldn't open files. Error: 399" << endl;
}
}
~Caesar() {};
};
int main()
{
string filePath;
bool isRunning = true;
int userChoice;
Caesar *caesar = new Caesar;
while (isRunning)
{
userChoice = caesar->caesarInterface(caesar);
if (userChoice == 1)
{
cout << "Type the .txt file path." << endl;
cin >> filePath;
caesar->cesarEncrypt(caesar, filePath);
system("pause");
isRunning = false;
}
if (userChoice == 2)
{
cout << "Type the .txt file path." << endl;
cin >> filePath;
caesar->caesarDecrypt(caesar, filePath);
system("pause");
isRunning = false;
}
if (userChoice == 3) isRunning = false;
}
return 0;
}