Reading file not working?

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <string>
#include <cstdlib>
#include <iostream>
#include <stream>

using namespace 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
        else if (option == 2)
        {
            addToFile(fileName);
        }
        //Leaves
        else if (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; 
        
    }
    else if (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;
               
            }
            else if (fileWord!=encryptedWord) //takes the new encrypted password and sees if the same.
            {
                cout << "Password Does not match" << endl <<endl;
                return;
                
            }



        }

    }
    else if (valid != true)
    {
        cout << "The entered password is not acceptable";
        return;

    }
}
 }
Last edited on
The issue is how you're reading the file contents. The getline() should be the condition test. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

void addToFile(const string& fname);
void checkFile(const string& fname);

// Test functions as not provided!!
bool passwordValidation(const string&) { return true; }
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
		else if (option == 2)
			addToFile(fileName);

		//Leaves
		else if (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);

	const auto 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);

	const auto 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";
}

Topic archived. No new replies allowed.