Ok, so you're given 3 options. 2 is to ask the user for a word or phrase and then it gets converted to morse code and added to a file. The 1st Option is to ask the user for a new word or phrase and then it converts to morse code and checks the file to see if that word or phrase has been added to the file.
So far I have most everything working. The problem is when it checks if the new word has been added and is the same it says that the words do not match even if they do. As I tested it, it seems like it's not reading the file or storing the word in a string. I've been working at it for a while and I can't seem to figure it out. Any help is appreciated.
#include <string>
#include <cstdlib>
#include <iostream>
#include <stream>
usingnamespace std;
int main()
{
while (true)
{
const string fileName = "masterFile.txt";
//Menu of choices
do
{
cout << "Would you like to test a password, or add a password?" << endl;
cout << " 1 : for test a password" << endl;
cout << " 2 : for add a password" << endl;
cout << " 3 : to quit" << endl;
cout << " >>> ";
cin >> option;
} while (option < 1 || option > 3);
//Checks if there is password
if (option == 1)
{
checkFile(fileName);
}
//Adds password
elseif (option == 2)
{
addToFile(fileName);
}
//Leaves
elseif (option == 3)
{
cout << "Thank you, have a good day." << endl;
return 0;
}
else
{
// This is an error state, this should never happen.
cerr << "ERROR invalid option" << endl;
return 99;
}
} // End of the while(true);
// The program did not leave correctly.
cerr << "ERROR illegal loop exit." << endl;
return 100;
}
void addToFile(string fname)
{
bool valid = false;
string password;
ofstream encryptFile;
encryptFile.open(fname, std::ios_base::app);
cout << "Enter a password to add: ";
cin.ignore();
getline(cin, password);
valid = passwordValidation(password);//Takes password and validates it
if (valid == true)
{
string encryptedWord = encryptToMorse(password); //If validation is goood then encrypts the password to the file.
encryptFile << encryptedWord << endl;
cout << "Password Added and Encrypted" << endl;
}
elseif (valid != true) //If not true then return
return;
encryptFile.close(); //Close file
}
void checkFile(string fname)
{
bool valid = false;
string password;
string fileWord;
ifstream encryptedFile;
//Password to check
cout << "Enter a password to check: ";
cin.ignore();
getline(cin, password);
valid = passwordValidation(password);//Takes password and validates it
if (valid == true)
{
string encryptedWord = encryptToMorse(password); //If validation is goood then encrypts the password
encryptedFile.open(fname, std::ios_base::app); //Open File
if (!encryptedFile)
{
cout << "File did not open";
}
while (encryptedFile) //While its reading
{
cin.ignore();
getline(encryptedFile, fileWord);
//Takes the line from file and stores into string word
makeTrim(fileWord); //Trims
if (fileWord==encryptedWord) //takes the string from file with the new encrypted password and sees if the same.
{
cout << "Password matches " << endl << endl;
return;
}
elseif (fileWord!=encryptedWord) //takes the new encrypted password and sees if the same.
{
cout << "Password Does not match" << endl <<endl;
return;
}
}
}
elseif (valid != true)
{
cout << "The entered password is not acceptable";
return;
}
}
}
#include <string>
#include <iostream>
#include <fstream>
usingnamespace std;
void addToFile(const string& fname);
void checkFile(const string& fname);
// Test functions as not provided!!
bool passwordValidation(const string&) { returntrue; }
string encryptToMorse(const string&str ) { return str; }
void makeTrim(string&) {}
int main()
{
const string fileName {"masterFile.txt"};
while (true) {
int option {};
//Menu of choices
do {
cout << "Would you like to test a password, or add a password?" << endl;
cout << " 1 : for test a password" << endl;
cout << " 2 : for add a password" << endl;
cout << " 3 : to quit" << endl;
cout << " >>> ";
cin >> option;
} while (option < 1 || option > 3);
//Checks if there is password
if (option == 1)
checkFile(fileName);
//Adds password
elseif (option == 2)
addToFile(fileName);
//Leaves
elseif (option == 3) {
cout << "Thank you, have a good day." << endl;
return 0;
} else {
// This is an error state, this should never happen.
cerr << "ERROR invalid option" << endl;
return 99;
}
} // End of the while(true);
// The program did not leave correctly.
cerr << "ERROR illegal loop exit." << endl;
return 100;
}
void addToFile(const string& fname)
{
ofstream encryptFile(fname, std::ios_base::app);
if (!encryptFile) {
cout << "Cannot open file\n";
return;
}
string password;
cout << "Enter a password to add: ";
cin.ignore();
getline(cin, password);
constauto valid {passwordValidation(password)}; //Takes password and validates it
if (valid) {
string encryptedWord {encryptToMorse(password)}; //If validation is good then encrypts the password to the file.
encryptFile << encryptedWord << '\n';
cout << "Password Added and Encrypted" << endl;
}
}
void checkFile(const string& fname)
{
string password;
//Password to check
cout << "Enter a password to check: ";
cin.ignore();
getline(cin, password);
constauto valid {passwordValidation(password)}; //Takes password and validates it
if (valid) {
string encryptedWord {encryptToMorse(password)}; //If validation is good then encrypts the password
ifstream encryptedFile(fname, std::ios_base::app); //Open File
if (!encryptedFile)
cout << "File did not open\n";
else {
for (string fileWord; getline(encryptedFile, fileWord); ) {
//cin.ignore();
//getline(encryptedFile, fileWord);
//Takes the line from file and stores into string word
makeTrim(fileWord); //Trims
if (fileWord == encryptedWord) { //takes the string from file with the new encrypted password and sees if the same.
cout << "Password matches\n\n";
return;
}
}
cout << "Password Does not match\n\n";
return;
}
}
cout << "The entered password is not acceptable";
}