Need help writing a password checker.

so I have 2 files that have passwords, one is the passwords that Im checking and one is the passwords im checking against, ive done everything up until the point where I actually have to check them, (my professor suggested a bool return function) but I cant seem to get it to work, ive been messing around for a while now but i cant even think on how to start it.


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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

bool compare(&string[], &string[], &int);

int main(){
    
    //define ALL variables
    int passed, failed, sizeP, sizeC;
    
    string *commList, *passList, passName, commName, numPass, numCheck, temp;
    
    //prompt for names of files that will be opened
    cout << "Please enter the name of the file: ";
    cin >> passName;
    
    cout << "Please enter the name of the common passwords file: ";
    cin >> commName;
    
    
    //open files and check if they are open
    ifstream passwords;
    passwords.open(passName);
    if(!passwords){
        cout << "Error opening file" << endl;
        return 1;
    }
    
    ifstream commPass;
    commPass.open(commName);
    if(!commPass){
      cout << "Error opening file" << endl;
      return 1;
    }
    
    //get the number of passwords by seing the first value of the array
    getline(passwords, numPass);
    
    //convert it to a int
    sizeP = stoi(numPass);
    
    // make it the size of a new array
    passList = new string[sizeP];
    
    //Do the same for the other array of common passwords
    getline(commPass, numCheck);
    
    sizeC = stoi(numCheck);
    
    commList = new string[sizeC];
    
    cout << "Checked " << sizeP << " passwords in file: " << passName << endl;
    
    //enter both of these text files into the arrays
    for(int count = 0; count < sizeP; count++){
      getline(passwords, passList[count]);
      
      for(int increment = 0 && count < sizeP){
        temp = passList[increment];
        temp[increment]
      }
      
    };
    
    for(int count = 0; count < sizeC; count++){
      getline(commPass, commList[count]);
    };
    
}

bool compare (&commList[], &currentpass[], &sizep){
 
}
It is hard for us to test the code without having the password files as well.

You don't need to post the entire contents of the files if the passwords are several dozen or more. About 10 or so from each file would help us to be able to help you.

A suggestion: use the output code tags for the password data files.
Sorry for the hard to read variables, I guess im not used to doing long_named_variables_with_alot_of_underscores but ill start to from now on.

Anyways I was going to make the "temp" variable into each password and then cycle through each character of the password and check if it matched with one of the passwords that was the common passwords, the reason that I didnt plug the passwords is becuase i have 50 passwords to check against a list a 100 common passwords.
If you are allowed to use the standard library:

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
#include <iostream>
#include <string>
#include <set>
#include <fstream>
#include <algorithm>
#include <iterator>

// set: https://en.cppreference.com/w/cpp/container/set
std::set<std::string> get_passwords( const std::string& file_name )
{
    if( std::ifstream file{file_name} ) // if the file is successfully opened
    {
        std::size_t expected_size = 0 ;
        file >> expected_size >> std::ws ; // get the number of passwords in the beginning and then move to the next line

        std::set<std::string> passwords ;
        std::string line ;
        while( std::getline( file, line ) ) passwords.insert(line) ; // add lines in the file to the set of passwords

        if( passwords.size() == expected_size ) return passwords ; // success!
    }

    return {} ; // read failed or bad number of passwords, return an empty set to indicate failure
}

int main()
{
    std::string master_file_name ;
    std::cout << "enter name of password master file: " ;
    std::getline( std::cin, master_file_name ) ;

    std::string candidate_file_name ;
    std::cout << "enter name of file containing passwords to check: " ;
    std::getline( std::cin, candidate_file_name ) ;

    const std::set<std::string> master = get_passwords(master_file_name) ;
    const std::set<std::string> candidates = get_passwords(candidate_file_name) ;

    if( master.empty() || candidates.empty() )
    {
        std::cout << "failed to read passwords from at least one file.\n" ;
        return 1 ; // EXIT_FAILURE
    }
    // https://en.cppreference.com/w/cpp/iterator/ostream_iterator
    using out_iter = std::ostream_iterator<std::string> ; // type of the output iterator

    // print out the list of passwords found in both candidates and master
    // https://en.cppreference.com/w/cpp/algorithm/ranges/set_intersection
    std::ranges::set_intersection( candidates, master, out_iter( std::cout << "\nmatched passwords\n---------\n","\n" ) ) ;

    // print out the list of passwords in candidates which are not found in master
    // https://en.cppreference.com/w/cpp/algorithm/ranges/set_difference
    std::ranges::set_difference( candidates, master, out_iter( std::cout << "\nunmatched passwords\n---------\n","\n" ) ) ;
}
NGL @JLBorges all I see is magic on the screen, we havent been taught anything crazy like that yet
ive been messing around for a while now but i cant even think on how to start it


You're trying to code as design - rather than design before code. Before you start coding you should first design the program. Then once you have the design, you start to code from the design. Code in small sections and compile and test frequently. Once you're coded the design, then assuming the design is correct then you should have a working program. if not then you use the debugger to see where your code deviates from that expected from the design. What you don't do is to start coding without a design. If you can't produce a program design then you don't understand the problem sufficiently and certainly can't code a solution.

Always design first and code second.

Based upon the OP (and not using standard library algorithms etc as you would) then possibly something like:

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
#include <iostream>
#include <string>
#include <fstream>

int main() {
	std::string passName, commName;

	std::cout << "Please enter the name of the file: ";
	std::getline(std::cin, passName);

	std::cout << "Please enter the name of the common passwords file: ";
	std::getline(std::cin, commName);

	std::ifstream passwords(passName);
	std::ifstream commPass(commName);

	if (!passwords || !commPass) {
		std::cout << "Error opening file\n";
		return 1;
	}

	size_t sizeP {}, sizeC {};

	passwords >> sizeP >> std::ws;
	commPass >> sizeC >> std::ws;

	const auto passList { new std::string[sizeP] };
	const auto commList { new std::string[sizeC] };

	std::cout << sizeP << " passwords in file: " << passName << '\n';
	std::cout << sizeC << " passwords in file: " << commName << '\n';

	for (size_t count {}; count < sizeP; ++count)
		getline(passwords, passList[count]);

	for (size_t count {}; count < sizeC; ++count)
		getline(commPass, commList[count]);

	for (size_t p {}; p < sizeP; ++p) {
		bool fnd {};

		for (size_t c {}; !fnd && c < sizeC; fnd = commList[c++] == passList[p]);

		std::cout << passList[p] << (!fnd ? " not" : "") << " found\n";
	}

	delete[] passList;
	delete[] commList;
}


Giving:


Please enter the name of the file: passname.txt
Please enter the name of the common passwords file: commname.txt
5 passwords in file: passname.txt
6 passwords in file: commname.txt
qwerty found
zxcvbn found
jhgfds not found
oiuytr not found
mnbvcx found


with the files:

passname.txt

5
qwerty
zxcvbn
jhgfds
oiuytr
mnbvcx


commname.txt

6
qwerty
asdfgh
zxcvbn
poiuyt
lkjhgf
mnbvcx

Last edited on
And with using a function to open/read a file and with a basic check that lines read are the number requested, then possibly something like:

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
#include <iostream>
#include <string>
#include <fstream>

std::string* readFile(const std::string& nam, size_t& sze) {
	std::string* list {};
	sze = 0;

	if (std::ifstream ifs { nam })
		if ((ifs >> sze >> std::ws) && sze > 0) {
			list = new std::string[sze];
			std::cout << sze << " passwords in file: " << nam << '\n';

			size_t cnt {};

			while (cnt < sze && getline(ifs, list[cnt++]));

			if (cnt != sze) {
				delete[] list;
				list = nullptr;
				sze = 0;
			}
		}

	return list;
}

int main() {
	std::string passName;
	size_t sizeP {};

	std::cout << "Please enter the name of the file: ";
	std::getline(std::cin, passName);

	const auto passList { readFile(passName, sizeP) };

	if (sizeP) {
		std::string commName;
		size_t sizeC {};

		std::cout << "Please enter the name of the common passwords file: ";
		std::getline(std::cin, commName);

		const auto commList { readFile(commName, sizeC) };

		if (sizeC) {
			for (size_t p {}; p < sizeP; ++p) {
				bool fnd {};

				for (size_t c {}; !fnd && c < sizeC; fnd = commList[c++] == passList[p]);

				std::cout << passList[p] << (!fnd ? " not" : "") << " found\n";
			}
			delete[] commList;
		} else
			std::cout << "Cannot open common password file\n";
	} else
		std::cout << "Cannot open password file\n";

	delete[] passList;
}


What do you want to happen if a password matches or not matches?? This code simply states whether it's found or not found in the common password file.
Last edited on
Thanks so much for the help everyone im looking over what yall sent right now
since ther is 50 passwords, im just supposed to say: passed: x
failed: y
OK. Perhaps:

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
#include <iostream>
#include <string>
#include <fstream>

std::string* readFile(const std::string& nam, size_t& sze) {
	std::string* list {};
	sze = 0;

	if (std::ifstream ifs { nam })
		if ((ifs >> sze >> std::ws) && sze > 0) {
			list = new std::string[sze];
			//std::cout << sze << " passwords in file: " << nam << '\n';

			size_t cnt {};

			while (cnt < sze && getline(ifs, list[cnt++]));

			if (cnt != sze) {
				delete[] list;
				list = nullptr;
				sze = 0;
			}
		}

	return list;
}

int main() {
	std::string passName;
	size_t sizeP {};

	std::cout << "Please enter the name of the file: ";
	std::getline(std::cin, passName);

	const auto passList { readFile(passName, sizeP) };

	if (sizeP) {
		std::string commName;
		size_t sizeC {};

		std::cout << "Please enter the name of the common passwords file: ";
		std::getline(std::cin, commName);

		const auto commList { readFile(commName, sizeC) };

		if (sizeC) {
			size_t good {};

			for (size_t p {}; p < sizeP; ++p) {
				bool fnd {};

				for (size_t c {}; !fnd && c < sizeC; fnd = commList[c++] == passList[p]);

				//std::cout << passList[p] << (!fnd ? " not" : "") << " found\n";
				good += fnd;
			}

			std::cout << "Passed: " << good << '\n';
			std::cout << "Failed: " << sizeP - good << '\n';
			delete[] commList;
		} else
			std::cout << "Cannot open common password file\n";
	} else
		std::cout << "Cannot open password file\n";

	delete[] passList;
}


Using the test data from above:


Please enter the name of the file: passname.txt
Please enter the name of the common passwords file: commname.txt
Passed: 3
Failed: 2

Last edited on
Topic archived. No new replies allowed.